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.