Question

In: Computer Science

Write a loop that sets each array element to the sum of itself and the next...

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();
}
}

Solutions

Expert Solution

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


Related Solutions

Write a loop that sets each array element to the sum of itself and the next...
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...
THE FOLLOWING IS CODED IN C Write a function that sets each element in an array...
THE FOLLOWING IS CODED IN C Write a function that sets each element in an array to the sum of the corresponding elements in two other arrays. That is, if array 1 has the values 2,4, 5, and 8 and array 2 has the values 1, 0, 4, and 6, the function assigns array 3 the values 3, 4, 9, and 14. The function should take three array names and an array size as arguments. Test the function in a...
Write a loop that subtracts 1 from each element in lowerScores. If the element was already...
Write a loop that subtracts 1 from each element in lowerScores. If the element was already 0 or negative, assign 0 to the element. Ex: lowerScores = {5, 0, 2, -3} becomes {4, 0, 1, 0}. please bold the solution 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[] lowerScores = new int[SCORES_SIZE]; int i; for (i = 0; i < lowerScores.length; ++i) {...
In C. Write a loop that subtracts 1 from each element in lowerScores. If the element...
In C. Write a loop that subtracts 1 from each element in lowerScores. If the element was already 0 or negative, assign 0 to the element. Ex: lowerScores = {5, 0, 2, -3} becomes {4, 0, 1, 0}.
For c++ Write the code to initialize an array such that each element gets two times...
For c++ Write the code to initialize an array such that each element gets two times the value of its index (e.g. 0, 2, 4, 6, …)
Write a program in MIPS to find the largest element of an array, the array size...
Write a program in MIPS to find the largest element of an array, the array size should be less than or equal to 10. Has to be extremely basic, cannot use stuff like move. Very basic. Here is what I already have and I am stuck. .data myarray: .word 0,0,0,0,0,0,0,0,0,0 invalid: .asciiz "Number is invalid, store a number in the array that is from 0-10.\n" large: .asciiz "The largest element is " colon: .asciiz " :\t" enter: .asciiz "Store a...
Write a Java program that creates a three-dimensional array. Populate each element with a string that...
Write a Java program that creates a three-dimensional array. Populate each element with a string that states each coordinate position in the array.
Assume an MxN array has been filled, write a function to calculate the sum of each...
Assume an MxN array has been filled, write a function to calculate the sum of each column which should be stored into a third 1D array of size N. code in C++
Write a program to reverse each integer number on array (size 3) using while loop. C++...
Write a program to reverse each integer number on array (size 3) using while loop. C++ Input: 3678 2390 1783 Output: 8763 0932 3871
Write a Java method that returns the index of the largest element in an array of...
Write a Java method that returns the index of the largest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: 
 public static int indexOfLargestElement(double[] array)
 Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the largest element, and displays the index.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT