Question

Write a loop that sets newScores to oldScores shifted once left, with element 0 copied to the end. Ex: If oldScores = {10, 20

2 public class StudentScores { public static void main (String I args) { Scanner scnr = new Scanner(System.in); final int SCO

0 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

SOURCE CODE:

import java.util.*;
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=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 final output this is optional
       for(i=0;i<newScores.length;++i)
       {
       System.out.print(newScores[i]+"\t");
       }
   }
}

CODE SCREENSHOT:

import java.util.*; public class Student Scores public static void main(String[] args) { Scanner scnr=new Scanner(System.in);

//printing final output for(i=0;i<newScores.length;++i) System.out.print(newScores[i]+\t);

OUTPUT:

20 30 40 10

Add a comment
Know the answer?
Add Answer to:
Write a loop that sets newScores to oldScores shifted once left, with element 0 copied to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Write three statements to print the first three elements of array runTimes. Follow each statement with...

    Write three statements to print the first three elements of array runTimes. Follow each statement with a newline. Ex: If runTime = {800, 775, 790, 805, 808}, print: 800 775 790 Note: These activities will test the code with different test values. This activity will perform two tests, both with a 5-element array. See "How to Use zyBooks". Also note: If the submitted code tries to access an invalid array element, such as runTimes[9] for a 5-element array, the test...

  • 7.2.3: Printing array elements with a for loop. Write a for loop to print all elements...

    7.2.3: Printing array elements with a for loop. Write a for loop to print all elements in courseGrades, following each element with a space (including the last). Print forwards, then backwards. End each loop with a newline. Ex: If courseGrades = {7, 9, 11, 10}, print: 7 9 11 10 10 11 9 7 Hint: Use two for loops. Second loop starts with i = courseGrades.length - 1. (Notes) Also note: If the submitted code tries to access an invalid...

  • in java Feedback? CHALLENGE ACTIVITY 5.2.2: Printing array elements. Write three statements to print the first...

    in java Feedback? CHALLENGE ACTIVITY 5.2.2: Printing array elements. Write three statements to print the first three elements of array run Times. Follow each statement with a newline. Ex: If runTime = (800,775, 790, 805, 808) print: 800 775 790 Note: These activities will test the code with different test values. This activity will perform two tests, both with a 5 element array. See 'How to Use zyBooks". Also note: If the submitted code tries to access an invalid array...

  • Write a while loop that prints userNum divided by 2 (integer division) until reaching 1. Follow...

    Write a while loop that prints userNum divided by 2 (integer division) until reaching 1. Follow each number by a space. Example output for userNum = 40: 20 10 5 2 1 Note: These activities may test code with different test values. This activity will perform four tests, with userNum = 40, then with userNum = 2, then with userNum = 0, then with userNum = -1. See "How to Use zyBooks". Also note: If the submitted code has an...

  • C programming only please 5.2.3: Printing array elements with a for loop. Write a for loop...

    C programming only please 5.2.3: Printing array elements with a for loop. Write a for loop to print all elements in courseGrades, following each element with a space (including the last). Print forwards, then backwards. End each loop with a newline. Ex: If courseGrades = {7, 9, 11, 10}, print: 7 9 11 10 10 11 9 7 Hint: Use two for loops. Second loop starts with i = NUM_VALS - 1. (Notes) Note: These activities may test code with...

  • Please do it in java thanks a lot E zyBook 21: 198: 102 home> 5.2: Arrays...

    Please do it in java thanks a lot E zyBook 21: 198: 102 home> 5.2: Arrays (int courseGrades(4), the second with a 2-element array (int courseGradesl2), See "How to Use zyBooks Also note: If the submitted code tries to access an invalid array element, such as courseGrades(9j 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 doesnt print the test case that caused...

  • Write an expression that executes the loop while the user enters a number greater than or...

    Write an expression that executes the loop while the user enters a number greater than or equal to 0. Note: These activities may test code with different test values. This activity will perform three tests, with user input of 9, 5, 2, -1, then with user input of 0, -17, then with user input of 0 1 0 -1. Also note: If the submitted code has an infinite loop, the system will stop running the code after a few seconds,...

  • Write a for loop to print all NUM_VALS elements of array hourlyTemp. Separate elements with a...

    Write a for loop to print all NUM_VALS elements of array hourlyTemp. Separate elements with a comma and space. Ex: If hourlyTemp = {90, 92, 94, 95}, print: 90, 92, 94, 95 Your code's output should end with the last element, without a subsequent comma, space, or newline. import java.util.Scanner; public class PrintWithComma {    public static void main (String [] args) {       Scanner scnr = new Scanner(System.in);       final int NUM_VALS = 4;       int[] hourlyTemp = new...

  • *This needs comments for each line of code. Thanks in advance!* import java.util.Scanner; public class LabProgram...

    *This needs comments for each line of code. Thanks in advance!* import java.util.Scanner; public class LabProgram {     public static void main(String[] args) {         Scanner scnr = new Scanner(System.in);         int numCount = scnr.nextInt();         int[] Array = new int[numCount];                for(int i = 0; i < numCount; ++i) {             Array[i] = scnr.nextInt();         }         int jasc = Array[0], gws = Array[1], numbers, tempo;         if (jasc > gws) {             tempo = jasc;             jasc...

  • 1. Assign numMatches with the number of elements in userValues that equal matchValue. userValues has NUM_VALS...

    1. Assign numMatches with the number of elements in userValues that equal matchValue. userValues has NUM_VALS elements. Ex: If userValues is {2, 1, 2, 2} and matchValue is 2 , then numMatches should be 3. Your code will be tested with the following values: matchValue: 2, userValues: {2, 1, 2, 2} (as in the example program above) matchValue: 0, userValues: {0, 0, 0, 0} matchValue: 10, userValues: {20, 50, 70, 100} What i am given: import java.util.Scanner; public class FindMatchValue...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT