Reputation: 7
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}.
This is my code
What am I missing in order to shift it to the left? My output is the same as the input.
public class StudentScores {
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
final int SCORES_SIZE = 4;
int[] oldScores = new int[SCORES_SIZE];
int[] newScores = new int[SCORES_SIZE];
int i;
for (i = 0; i < oldScores.length; ++i) {
oldScores[i] = scnr.nextInt();
}
for (i = oldScores.length - 1; i >= 0; i--)
{
newScores[i] = oldScores[i];
}
for (i = 0; i < newScores.length; ++i) {
System.out.print(newScores[i] + " ");
}
System.out.println();
}
}```
Upvotes: 0
Views: 745
Reputation: 11
You forgot about the first element, it should be in the last place and you don't need a second array, you can just move all the elements to the left side except the first one.
Upvotes: 1