52.0k views
2 votes
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

User Carlson
by
5.9k points

1 Answer

6 votes

Answer:

JAVA program for the given question is as below.

public class MyProgram {

public static void main(String args[]) {

int len = 5;

int[] scores = new int[len];

scores[0] = 7;

scores[1] = 10;

scores[2] = 11;

scores[3] = 9;

scores[4] = 10;

System.out.print("This program sets each element to the sum of itself and the next element except the last element.");

System.out.println();

System.out.println("The initial elements are ");

for(int i=0; i<len; i++)

{

System.out.print(scores[i]+" ");

}

// new line is inserted

System.out.println();

System.out.println("The elements after addition are ");

for(int i=0; i<len; i++)

{

// addition is not done for last element

if(i != len-1)

scores[i] = scores[i] + scores[i+1];

// elements of array printed backwards beginning from last element

System.out.print(scores[i]+" ");

}

}

}

OUTPUT

The elements of array are

7 10 11 9 10

The elements of array backwards are

10 9 11 10 7

Explanation:

This program uses for loop to display and add up the array elements.

The length of the array is determined by an integer variable, len.

The len variable is declared and initialized to 5.

int len = 5;

The array of integers is declared and initialized as given.

int[] scores = new int[len];

We take the array elements from the question and initialize them manually.

First, we print array elements in sequence using for loop.

for(int i=0; i<len; i++)

To display in sequence, we begin with first element which lies at index 0. The consecutive elements are displayed by incrementing the value of variable i.

The array element is displayed followed by space.

System.out.print(courseGrades[i]+" ");

Next, we set each array element to the sum of itself and the following element except the last element.

if(i != len-1)

scores[i] = scores[i] + scores[i+1];

We also display these elements simultaneously.

The length and elements of the array are initialized manually and can be changed for testing the program.

Answer:

JAVA program for the given question is as below.

public class MyProgram {

public static void main(String args[]) {

int len = 5;

int[] scores = new int[len];

scores[0] = 7;

scores[1] = 10;

scores[2] = 11;

scores[3] = 9;

scores[4] = 10;

System.out.print("This program sets each element to the sum of itself and the next element except the last element.");

System.out.println();

System.out.println("The initial elements are ");

for(int i=0; i<len; i++)

{

System.out.print(scores[i]+" ");

}

// new line is inserted

System.out.println();

System.out.println("The elements after addition are ");

for(int i=0; i<len; i++)

{

// addition is not done for last element

if(i != len-1)

scores[i] = scores[i] + scores[i+1];

// elements of array printed backwards beginning from last element

System.out.print(scores[i]+" ");

}

}

}

OUTPUT

The elements of array are

7 10 11 9 10

The elements of array backwards are

10 9 11 10 7

Explanation:

This program uses for loop to display and add up the array elements.

The length of the array is determined by an integer variable, len.

The len variable is declared and initialized to 5.

int len = 5;

The array of integers is declared and initialized as given.

int[] scores = new int[len];

We take the array elements from the question and initialize them manually.

First, we print array elements in sequence using for loop.

for(int i=0; i<len; i++)

To display in sequence, we begin with first element which lies at index 0. The consecutive elements are displayed by incrementing the value of variable i.

The array element is displayed followed by space.

System.out.print(courseGrades[i]+" ");

Next, we set each array element to the sum of itself and the following element except the last element.

if(i != len-1)

scores[i] = scores[i] + scores[i+1];

We also display these elements simultaneously.

The length and elements of the array are initialized manually and can be changed for testing the program.

User Deann
by
6.2k points