54.2k views
3 votes
Write a loop that sets newScores to oldScores shifted once left, with element 0 copied to the end. Ex: If oldScores = {10, 20, 30, 40}, then newScores = {20, 30, 40, 10}. Note: These activities may test code with different test values. This activity will perform two tests, the first with a 4-element array (newScores = {10, 20, 30, 40}), the second with a 1-element array (newScores = {199}). See "How to Use zyBooks". . Also note: If the submitted code tries to access an invalid array element, such as newScores[9] for a 4-element array, the test may generate strange results. Or the test may crash and report "Program end never reached", in which case the system doesn't print the test case that caused the reported message.

1 Answer

4 votes

Answer:

int[] newScores = new int[oldScores.length];

for (int i = 0; i < oldScores.length; i++) {

if (i==oldScores.length-1){

newScores[oldScores.length-1] = oldScores[0];

} else {

newScores[i] = oldScores[i+1];

}

}

Step-by-step explanation:

Code is written in Java

User Iqon
by
5.3k points