. Advertisement .
..3..
. Advertisement .
..4..
......... ADVERTISEMENT .........
..8..

......... ADVERTISEMENT .........
..8..

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, both with a 4-element array (int oldScores[4]). 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.
public class StudentScores {
public static void main (String I 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();
}
/* Your solution goes here */
for (i=0; i<oldScores.length/2; ++i) {
newScores [i] = oldScores [i];
int tempVal = newScores[0];
newScores[0] = newScores[3];
newScores [3] = tempVal;
}
As far as I can tell, the problem is that some numbers should be set to old score from newscore.
After it has relocated, o must be substituted for the initial value by moving it to the end.
In my programme, I first printed the oldScores, then sorted the values in the next for-loop, and then printed the newScores in the next for-loop. See the code here https://kodlogs.net/18/write-loop-that-sets-newscores-to-oldscores-shifted-once-left-with-element-copied-the-end
SOURCE CODE:
import java.util. *; public-class StudentScores
{
{ public static void main(String[] args) public static void main (String[]args)
Scanner scnr=new Sccanner(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=0;i<oldScores.length-1;++i) //iterating over
oldScores array upto last but one element
{
newScores[i]=oldScores[i+1];
//storing oldScores[i+1] element in newScores[i]
if(i==oldScores.length-2) //if i is
equal to array length-2
{
newScores[i]=oldScores[i+1];
newScores[i+1]=oldScores[0]; //last
element of newScores is first element of oldScores
}
}
//Printing the final output is optional
for(i=0;i<newScores.length;++i)
{
System.out.print(newScores[i]+”\t”);
}
}
}
CODE SCREENSHOT:
OUTPUT: