0

I am having problems with chapter 7.7 in zybooks with language in Java. The challenge activity is as stated:

Write a loop that sets each array element to the sum of itself and the next element, except for the last element which stays the same. Be careful not to index beyond the last element. Ex:

Initial scores: 10, 20, 30, 40

Scores after the loop: 30, 50, 70, 40

The first element is 30 or 10 + 20, the second element is 50 or 20 + 30, and the third element is 70 or 30 + 40. The last element remains the same.

So this is the code it has provided with my code in it, I will say which part is my code:

public class StudentScores {
   public static void main (String [] args) {
      final int SCORES_SIZE = 4;
      int[] bonusScores = new int[SCORES_SIZE];
      int i = 0;

      bonusScores[0] = 10;
      bonusScores[1] = 20;
      bonusScores[2] = 30;
      bonusScores[3] = 40;

      if (SCORES_SIZE==0){
         System.out.println(""); 
      }
      else if (SCORES_SIZE==1){
         bonusScores[i]=bonusScores[i];
      }
      else{
         for (i=0; i<SCORES_SIZE; ++i){
            if (i==3){
               bonusScores[i]=bonusScores[i];  
            }
            else{
               bonusScores[i]=bonusScores[i]+bonusScores[i+1];  
            }
         }
      }


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

      return;
   }
}

So the only part that I can change and is therefor my code starts after the oldscores[3] part and ends after the else statement. So my problem is this is what the program is doing with my code:

Testing for bonusScores = {10, 20, 30, 40} Your output 30 50 70 40 check

Testing for bonusScores = {199} Your output 199 check

Runtime error Program end never reached. This is commonly due to an invalid memory/array access or divide by 0.

So I have no idea what it is doing what is causing the runtime error and therefor obviously do not know what to do as a result. If anyone has passed this, please let me know. I know there is a thread out there that is this one:

Copying and modifying array elements

However, this thread is different from mine as that is actually the one I just completed without help of that thread. So please do not say possible repeat when it isn't. Thanks in advance.

5
  • " I have no idea what it is doing what is causing the runtime error and therefor obviously do not know what to do as a result" - this code produces incorrect results but it does not generate any runtime error. If you're getting any kind of error you should post it in your question and state which line in the code triggers it. Commented Oct 15, 2017 at 1:37
  • Sorry, I just realized I had the wrong code for some strange reason. Must have been the code from the wrong thread I was talking about. I am also going to remind you that I am not using eclipse, this is in zybooks if you ever heard of zybooks. So in this case it didn't say a line number because its zybooks. Commented Oct 15, 2017 at 2:05
  • I did update it with right code by the way. Every thing else I said in the original post remains constant. Only thing changed is the code, which previously was wrong but isn't now. Commented Oct 15, 2017 at 2:17
  • 1
    I solved the problem, turns out I was hard coding a little and zybooks was entering a array that was 5 numbers long, so it was breaking when I said if (i==3) so I needed to have (i==(SCORES_SIZE-1) in order to make it work. I don't know if I should close this or not as it could help someone in the future if they're working in zybooks also. Commented Oct 16, 2017 at 13:53
  • 1
    @Bob Feel free to provide your solution by answering your own question rather than leaving it in the comments Commented Nov 2, 2017 at 13:46

3 Answers 3

0
public class StudentScores {
    public static void main (String [] args) {
        final int SCORES_SIZE = 4;
        int[] bonusScores = new int[SCORES_SIZE];
        int i;

        bonusScores[0] = 10;
        bonusScores[1] = 20;
        bonusScores[2] = 30;
        bonusScores[3] = 40;

        for(i = 0; i < SCORES_SIZE -1; i++) {
            bonusScores[i] = bonusScores[i] + bonusScores[i+1];
        }
        for (i = 0; i < bonusScores.length; ++i) {
            System.out.print(bonusScores[i] + " ");
        }
        System.out.println();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

the result will be: 20 30 40 10
0

This is what I did, and it passed all tests:

import java.util.Scanner;
public class StudentScores {
   public static void main (String [] args) {
      Scanner scnr = new Scanner(System.in);
      final int SCORES_SIZE = 4;
      int[] bonusScores = new int[SCORES_SIZE];
      int i;

      for (i = 0; i < bonusScores.length; ++i) {
         bonusScores[i] = scnr.nextInt();
      }

      for (i = 0; i < 3; ++i) {
      bonusScores[i] = bonusScores[i] + bonusScores[i+1]; 
      }

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

Comments

0

Alright, most of this is assignment is prefilled except for your task, which is to modify each element in bonusScores with a Fibonacci sequence of itself, except for the last element.

One approach is using a conditional statement that allows the Fibonacci sequence to proceed until the last index in bonusScores:

// Format for the loop to iterate every element in bonusScores.
for (i = 0; i < SCORES_SIZE; ++i) {
      // Translates to: if i is not the last index in bonusScores then
      if (i != SCORES_SIZE - 1) {
         // This element in bonusScores becomes itself added with the element after it.
         bonusScores[i] += bonusScores[i + 1];
      }
   }

Because your container/array has a definite number of elements, which is represented as the constant SCORES_SIZE, you can use that in your guidelines for your for loop as well as the conditional statement.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.