In: Computer Science
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.
JAVA CODE:
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();
}
/* Your solution goes here */
for (i = 0; i < bonusScores.length; ++i) {
System.out.print(bonusScores[i] + " ");
}
System.out.println();
}
}
ANSWER:-
import java.util.Scanner;
public class StudentScores
{
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
System.out.println("enter number of scores : ");
int SCORES_SIZE=scnr.nextInt();
int[] bonusScores = new int[SCORES_SIZE];
int i;
System.out.println("Enter the scores : ");
for (i = 0; i < bonusScores.length; ++i) {
bonusScores[i] =
scnr.nextInt();
}
for (i = 0; i <SCORES_SIZE-1; ++i) {
bonusScores[i] =
bonusScores[i]+bonusScores[i+1];
}
System.out.print("The new scores : ");
for (i = 0; i < bonusScores.length; ++i) {
System.out.print(bonusScores[i] + " ");
}
System.out.println();
}
}
OUTPUT:-
// I give generic solution, if you want static solution ,please comment