Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a loop that sets newScores to oldScores shifted once left, with element 0

ID: 3680361 • Letter: W

Question

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}).

public class StudentScores {
public static void main (String [] args) {
final int SCORES_SIZE = 4;
int[] oldScores = new int[SCORES_SIZE];
int[] newScores = new int[SCORES_SIZE];
int i = 0;

oldScores[0] = 10;
oldScores[1] = 20;
oldScores[2] = 30;
oldScores[3] = 40;

/* Your solution goes here */

for (i = 0; i < SCORES_SIZE; ++i) {
System.out.print(newScores[i] + " ");
}
System.out.println();

return;
}
}

Explanation / Answer

Hi, I have updated the required code changes. Please find the highlighted lines.

public class StudentScores {

   public static void main (String [] args) {
   final int SCORES_SIZE = 4;
   int[] oldScores = new int[SCORES_SIZE];
   int[] newScores = new int[SCORES_SIZE];
   int i = 0;
   oldScores[0] = 10;
   oldScores[1] = 20;
   oldScores[2] = 30;
   oldScores[3] = 40;
   /* Your solution goes here */
   int j = 0;
   for(i=0; i < SCORES_SIZE; i++){
       if(i == SCORES_SIZE - 1){
           j = 0;
       }
       else{
           j = i+1;
      }
       newScores[i] = oldScores[j];
   }
  
for (i = 0; i < SCORES_SIZE; ++i) {
   System.out.print(newScores[i] + " ");
   }
   System.out.println();
   return;
   }
   }

Output:

20 30 40 10

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote