Question

php please shiftNTimes // Modify array so it is "left shifted" n times -- so shiftNTimes(array(6,...

php please

shiftNTimes

// Modify array so it is "left shifted" n times -- so shiftNTimes(array(6, 2, 5, 3), 1)

// changes the array argument to (2, 5, 3, 6) and shiftNTimes(array(6, 2, 5, 3), 2)

// changes the array argument to (5, 3, 6, 2). You must modify the array argument by

// changing the parameter array inside method shiftNTimes. A change to the

// parameter inside the method shiftNTimes changes the argument if the

// argument is passed by reference, that means it is preceded by an ampersand &

//

// shiftNTimes( array(1, 2, 3, 4, 5, 6, 7), 3 ) modifies array to ( 4, 5, 6, 7, 1, 2, 3 )

// shiftNTimes( array(1, 2, 3), 5) modifies array to (3, 1, 2)

// shiftNTimes( array(3), 5) modifies array to (3)

//

function shiftNTimes(& $array, $numShifts) {

}

$arr = array (

1,

2,

3,

4 );

shiftNTimes ( $arr, 2 );

assert(3 == $arr[0]);

assert(4 == $arr[1]);

echo PHP_EOL . 'shiftNTimes(array(1, 2, 3, 4), 2)' . "\n";

// Use print_r to see all array elements on separate lines

print_r ( $arr );

?>

Homework Answers

Answer #1

<?php
function shiftNTimes(& $array, $numShifts) {
$temp=[];//create a temp array
$len=sizeof($array);
for($i=0; $i<$numShifts; $i++)
{
$temp[$i]=$array[$i];
}
for($i=$numShifts; $i<$len; $i++)
{
$array[$i-$numShifts]=$array[$i];
}
for($i=0; $i<$numShifts; $i++)
{
$array[$len-$numShifts+$i]=$temp[$i];
}
print_r($array);
  

}

$arr = array (

1,

2,

3,

4

);

assert(3 == $arr[0]);

assert(4 == $arr[1]);
$a = readline('Enter the digit upto which you want to leftshift: ');
echo $a;

echo PHP_EOL . 'shiftNTimes(array(1, 2, 3, 4),'. $a.' )' . "\n";

// Use print_r to see all array elements on separate lines

print_r ( $arr );

shiftNTimes ( $arr, $a );
?>
//fell free to ask any questions

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
php please numberOfPairs // Return the number of times a pair occurs in array. A pair...
php please numberOfPairs // Return the number of times a pair occurs in array. A pair is any two String values that are equal (case // sensitive) in consecutive array elements. The array may be empty or have only one element. In both of // these cases, return 0. // // numberOfPairs( array('a', 'b', 'c') ) returns 0 // numberOfPairs( array('a', 'a', 'a') ) returns 2 // assert(2 == numberOfPairs( array('a', 'a', 'b', 'b' ) ) ); // numberOfPairs( array...
Using a loop, complete the following method so that it goes through the array from left...
Using a loop, complete the following method so that it goes through the array from left to right, one element at a time and replaces that element with the sum of itself and the next element in the array (if there is a next element). For example, if the input is {1, 3, 5} the output must be {4, 8, 5} -- on the first iteration 1 was replaced by 1+3; on the second iteration 3 was replaced by 3+5....
USE PYTHON LANGUAGE PLEASE FOCUS YOU SHOULD ENTER AN ARRAY AND THEN THE PROGRAM GIVE OUTPUT(...
USE PYTHON LANGUAGE PLEASE FOCUS YOU SHOULD ENTER AN ARRAY AND THEN THE PROGRAM GIVE OUTPUT( TRUE/ FALSE) QUIZ 8 Array Challenge Have the function ArrayChallenge(arr) take the array of numbers stored in arr and return the string true if any two numbers can be multiplied so that the answer is greater than double the sum of all the elements in the array. If not, return the string false. For example: if arr is [2, 5, 6, -6, 16, 2,...
Write a PHP code that: 1- Creates an array that holds 10 random integer numbers between...
Write a PHP code that: 1- Creates an array that holds 10 random integer numbers between 1 and 100. 2- Moves all multiple of 3-numbers in the array that created in part-a into a new array. 3- Moves all multiple of 5-numbers in the array that created in part-a into a new array. 4- Find the maximum and the minimum multiple of 3-numbers, if exist. 5- Find the maximum and the minimum multiple of 5-numbers, if exist. 6- Prints the...
First, understand the Selection-sort algorithm below: Selection-sort(A: Array [1..n] of numbers) 1       for i=n down to...
First, understand the Selection-sort algorithm below: Selection-sort(A: Array [1..n] of numbers) 1       for i=n down to 2 2                position=i 3                for j=1 to (i–1) 4                          if A[j]>A[position] then position=j 5                if position ≠ i then 6                          temp=A[i] 7                          A[i]=A[position] 8                          A[position]=temp (4 points) If input A=[12,5,11,6,10,7,9,8], what will A be after the 3rd iteration of the outermost for loop of the Selection-sort algorithm completes? A=[__, __, __, __, __, __, __, __] (8 points) Modify the algorithm to solve the...
Given the following unordered array: [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]...
Given the following unordered array: [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] W X D T P N R Q K M E If the array was being sorted using the SHELL sort and the halving method, and sorting into ASCENDING order as demonstrated in the course content, list the letters in the resulting array, in order AFTER the FIRST pass. [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
We are given an array A of size n containing n positive and negative integers (the...
We are given an array A of size n containing n positive and negative integers (the array is indexed starting from 0). Our goal is to find two indices i and j such that 0 ≤ i ≤ j ≤ n and Pk=j k=i A[k] is maximized. Input sequence: [2, -4, 1, 9, -6, 7, -3] Output: [1, 9, -6, 7] or i = 2 and j = 5 Input sequence: [1, 2, 3, 4, 5, 6, -3] Output: [1,...
1.) Generate an array of 10 random numbers between 1 - 100 2.) Copy the array...
1.) Generate an array of 10 random numbers between 1 - 100 2.) Copy the array to a temp array 3.) Call each of the methods to sort (bubble, selection, insertion, quick, merge), passing it the array 4.) In-between the calls, you are going to refresh the array to the original numbers. 5.) Inside of each sorting method, you are going to obtain the nanoseconds time, before and after the method Subtract the before time from the after time to...
Javascript please show me how to do these two exercises and comment so I can understand...
Javascript please show me how to do these two exercises and comment so I can understand what's happening. Thank you. // Execise #1 /* Modify the function so that it returns the length of an array recursively, without using the .length property. Use recursion! Hint: when you index outside of an array, it returns undefined */ function getLength(array, i = 0) { } // To check if you've completed the challenge, uncomment these console.logs! // console.log(getLength([1])); // -> 1 //...
How do you take an array for example A = [4 6 7 1 2 5]...
How do you take an array for example A = [4 6 7 1 2 5] and sort it from ascending order. So it would output A = [1 2 4 5 6 7]. In MATlab without using the sort function as well.