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/How to avoid the error "write a loop that sets newscores to oldscores shifted once left, with element 0 copied to the end"?
Next
Answered
Elina Roy
  • 23
Elina Roy
Asked: May 18, 20222022-05-18T16:06:37+00:00 2022-05-18T16:06:37+00:00In: java

How to avoid the error “write a loop that sets newscores to oldscores shifted once left, with element 0 copied to the end”?

  • 23

. Advertisement .

..3..

. Advertisement .

..4..

I’m trying to run a new project. I do a couple of things like this:

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;
 
  for (i = 0; i < SCORES_SIZE - 1; i++) {
  newScores[3] = oldScores[0];
  newScores[i] = oldScores[i + 1]; 
  }
 
  for (i = 0; i < SCORES_SIZE; ++i) {
  System.out.print(newScores[i] + " ");
  }
  System.out.println();
 
  return;
  }
  }

But in my program, I am getting the warning:

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

Can someone explain why the “ write a loop that sets newscores to oldscores shifted once left, with element 0 copied to the end.” issue happened? Where have I gone wrong? Thank you!

write a loop
  • 2 2 Answers
  • 96 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    dttutoria Expert
    2022-06-13T17:01:51+00:00Added an answer on June 13, 2022 at 5:01 pm

    The cause:  When SCORES SIZE == 1, the for loop that copies values from oldscores to newscores never runs since SCORES SIZE - 1 == 0, and 0 < 0 is false right away. This is what causes the issue to arise.

    Solution:

    Shift the newScores[SCORES_SIZE - 1] = oldScores[0]; the line which is outside the for loop:

    for (i = 0; i < SCORES_SIZE - 1; i++) {
    newScores[i] = oldScores[i + 1];
    }
    newScores[SCORES_SIZE - 1] = oldScores[0];

     

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Anna Vallet
    2022-05-25T20:29:06+00:00Added an answer on May 25, 2022 at 8:29 pm

    If you assume that the only other check is one array of length one, then simply use this

    if(oldscores.length==1)
    {newscores[0]=oldscores[0];
    System.out.println(newscores[0]);
    }

    Or, you can do it even simpler.

    if(oldscores.length==1)
    {
     System.out.println(oldscores[0]);
    }
    • 9
    • 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.