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...
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]
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...
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.
Write a method that checks whether all elements in a two-dimensional array are identical. This is...
Write a method that checks whether all elements in a two-dimensional array are identical. This is what I have so far. int[][] arr1 = {{2, 2, 8}, {1, 6, 4}, {3, 9, 8}, {5, 6, 1}};        int[][] arr2 = {{7, 7, 7}, {7, 7, 7}, {7, 7, 7}, {7, 7, 7}};               if(matchingVal(array1)) {            System.out.println("Values in arr1 are the same.");        }               if(matchingVal(array2)) {           ...
Giving an array of integers A[1:8] ={5, 1, 3, 2, 7, 6, 8, 4}. Build a...
Giving an array of integers A[1:8] ={5, 1, 3, 2, 7, 6, 8, 4}. Build a MAX-HEAP on A. Show the steps using binary tree representation or array view. Use heap sort: show the array after the first, the second, and the third of heap sort
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an executable with gcc -Wall -DUNIT_TESTS=1 array-utils5A.c The definitions for is_reverse_sorted and all_different are both defective. Rewrite the definitions so that they are correct. The definition for is_alternating is missing. Write a correct definition for that function, and add unit tests for it, using the unit tests for is_reverse_sorted and all_different as models. Please explain the logic errors present in in the definition of is_reverse_sorted...