silversk8terz
silversk8terz

Reputation: 91

How do i solve this array and loop? correct output but

My objective is to write a loop that sets newScores to oldScores shifted once left, with element 0 copied to the end.

Edit: Here's my main problem, I initialized newScores[3] = oldScores[0]; but the thing is the for loop makes i = 4 when there's no oldScores[4] or newScores[4] but since I initialized newScores[3] to oldScores[0] it compiles and runs, but the problem is that [4] is not in the array at all. How do i get rid of this problem? I'm so close but so far away it's bugging me.

Example:

If oldScores = {10, 20, 30, 40}, then newScores = {20, 30, 40, 10}

Funny thing is I have the correct output but the learning website I'm using it tells me that I have the correct output but it also displays this "Runtime error (commonly due to an invalid array/vector access, divide by 0, etc.). Tests aborted.".

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;
      newScores[3] = oldScores[0];
      for(i=0; i<SCORES_SIZE-1; i++){
         newScores[i] = oldScores[i +1];
      }

      for (i = 0; i < SCORES_SIZE; ++i) {
         System.out.print(newScores[i] + " ");
      }
      System.out.println();

      return;
   }
}

Upvotes: 1

Views: 13385

Answers (8)

TaeterTot
TaeterTot

Reputation: 1

This is it:

for (i = 0; i < newScores.length; ++i) {

    // if statement to shift numbers to the left starting with the 2nd value
    if (i > 0) {
       newScores[i + 1] = oldScores[i];
    }

    // This will make the 1st value move and become newScores last value regardless of SCORES_SIZE value
    else {
       newScores[newScores.length - 1] = oldScores[i];
    }
}

Upvotes: 0

Black Lion
Black Lion

Reputation: 9

Why re-inventing the wheel? Java API already allows doing this kind of stuff

1 - create method

    public static int[] switchScores(int[] oldScores){
      if(oldScores != null && oldScores.length > 0) {
        final int SIZE = oldScores.length;

        int[] newScores = new int[SIZE];
        newScores = Arrays.copyOfRange(oldScores, 1, SIZE+1);
        newScores[SIZE-1] = oldScores[0];

        return newScores;
      }
      else{
        throw new IllegalArgumentException("Null or Empty Array provided");
      }
   }

2 - run test

public static void main(String[] args) {
    int[] input = new int[]{10, 20, 30, 40};
    System.out.println("Old scores : " + Arrays.toString(input));

    int[] output = switchScores(input);
    System.out.println("New scores : " + Arrays.toString(output));
}   

Upvotes: 0

Fred
Fred

Reputation: 1

This is the answer that will pass all the tests:

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

  for (i = 0; i < newScores.length; ++i) {
     System.out.print(newScores[i] + " ");
  }

Upvotes: 0

vickig
vickig

Reputation: 1

In order to overcome the " Runtime error (commonly due to an invalid array/vector access, divide by 0, etc.). Tests aborted." I used an if/else statement:

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

Upvotes: 0

Doc
Doc

Reputation: 11651

Just a simple change to make your code work

oldScores[0] = 10;
      oldScores[1] = 20;
      oldScores[2] = 30;
      oldScores[3] = 40;
      newScores[3] = oldScores[0];
      for(i=0; i<SCORES_SIZE-1; i++){
         newScores[i] = oldScores[i +1];
      }
 /* REMOVE THIS PART   
          for (i = 0; i < SCORES_SIZE; ++i) {
             System.out.print(newScores[i] + " ");
          }
*/TILL HERE

// USE THE FOLLOWING
for (i = 0; i < SCORES_SIZE; i++) {
     System.out.print(newScores[i] + " ");
  }
  System.out.println();

Because you are increasing the value prior to its use which is causing the error.

Upvotes: 0

Hoang Subin
Hoang Subin

Reputation: 7450

I noticed that you are using i++ and ++i in wrong way.
First loop: newScores array index from 0 to 2 and increase 1 each time.
Second loop: you are trying to print newScores but loop using ++i. It means that newScores index will be printed from 1 to 3. => error at index 3. Solution: replace second loop by
for (i = 0; i < SCORES_SIZE - 1; i++)

Upvotes: 0

Nate
Nate

Reputation: 847

The issue is in the second loop where you have ++i. i++ and ++i have two different meanings. Here is a link to describe the meanings to you. What is the difference between ++i and i++?. If you change that, you should not get the error any more. Below is the change in your code.

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;
  newScores[3] = oldScores[0];
  for(i=0; i<SCORES_SIZE-1; i++){
     newScores[i] = oldScores[i +1];
  }

  for (i = 0; i < SCORES_SIZE; i++) {
     System.out.print(newScores[i] + " ");
  }
  System.out.println();

  return;
  }
}

Upvotes: 2

Kabir
Kabir

Reputation: 880

In your second for loop, you are incrementing i prior to using it i.e. ++i

What that means is, i will be incremented before your System.out in the loop. Since you intialized i to 0, the first index it will print is of 1 (not 0). It will try to print

newScores[1]
newScores[2]
newScores[3]
newScores[4]

The last will result in an exception (invalid index). Change the for loop to

for (i = 0; i < SCORES_SIZE; i++) {

(where i is incremented after use)

Upvotes: 0

Related Questions