Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Write a loop that sets newScores to oldScores shifted once left, with element 0 copied to...
Next
Answered
James Portman
  • 30
James Portman
Asked: April 13, 20222022-04-13T12:57:55+00:00 2022-04-13T12:57:55+00:00In: Computer-Science

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

  • 30

. 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;
}
  • 3 3 Answers
  • 140 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

3 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    dhtutoria Expert
    2022-05-20T08:14:39+00:00Added an answer on May 20, 2022 at 8:14 am
    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");
    }
    }
    }
    • 20
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Sammy
    2022-06-12T09:30:30+00:00Added an answer on June 12, 2022 at 9:30 am

    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

    • 18
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  3. Rhys Thomas
    2022-04-13T14:25:12+00:00Added an answer on April 13, 2022 at 2:25 pm

    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:


    Write%20a%20loop%20that%20sets%20newScores%20to%20oldScores%20shifted%20once%20left,%20with%20element%200%20copied%20to...


    Write%20a%20loop%20that%20sets%20newScores%20to%20oldScores%20shifted%20once%20left,%20with%20element%200%20copied%20to...

    OUTPUT:


    Write%20a%20loop%20that%20sets%20newScores%20to%20oldScores%20shifted%20once%20left,%20with%20element%200%20copied%20to...

    import java.util. *; public class Student Scores public static void main(String[] args) { Scanner scnr=new Scanner(System.in); final int SCORES_SIZE=4; intl 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[@]; //last element of newScores is first element of oldScores
    //printing final output for(i=0;i<newScores.length;++i) System.out.print(newScores[i]+"\t");
    20 30 40 10
    • 11
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.