1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
givi [52]
3 years ago
15

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
Computers and Technology
1 answer:
sladkih [1.3K]3 years ago
8 0

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.

You might be interested in
Claire wants to use a conditional statement in JavaScript that provides a block of statement to be executed if the condition is
Illusion [34]
C because what she saying doesn’t add up
6 0
3 years ago
Read 2 more answers
Cyber vulnerabilities to dod systems may include
Ivan

Cyber vulnerabilities to dod systems may include all of the above Options.

<h3>What is Cyber vulnerabilities?</h3>

In cybersecurity, a vulnerability is known to be any kind of weakness  exist with the aim to be exploited by cybercriminals to be able to have unauthorized access to a computer system.

Note that in the case above, Cyber vulnerabilities to dod systems may include All of the above Options.

Learn more about cyber vulnerabilities from

brainly.com/question/7065536

#SPJ11

6 0
2 years ago
If you want to create header, clicker the add a header button on the ___ tab
Finger [1]
On the insert tab
------------------------
6 0
3 years ago
In an agile team who is responsible for tracking the tasks
Alik [6]

Answer:

All team members

Explanation:

In respect of the question, the or those responsible for tracking the tasks in an agile team comprises of all the team members.

Agile in relation to task or project management, can be refer to an act of of division of project or breaking down of project or tasks into smaller unit. In my opinion, these is carried out so that all team members can be duly involved in the tasks or project.

5 0
4 years ago
Help need urgently answer ​
skad [1K]

Answer:

Explanation:

b

6 0
3 years ago
Read 2 more answers
Other questions:
  • Write the definition of a function named rcopy that reads all the strings remaining to be read in standard input and displays th
    6·1 answer
  • Teachers in most school districts are paid on a schedule that provides a salary based on their number of years of teaching exper
    10·1 answer
  • Why should you use a named constant for the size of an array?
    9·1 answer
  • What is the python code for these problems (100 points): Ask the user to enter a name. If there is an ‘a’ in the name, print a m
    8·1 answer
  • What are the basic steps in creating a new program
    5·1 answer
  • Requests to access specific cylinders on a disk drive arrive in this order: 24, 20, 4, 40, 6, 38, and 12, and the seek arm is in
    10·1 answer
  • sometimes people who have been playing gta for a while are annoying, i was playing gta this weekend and was trying to escape, bu
    8·2 answers
  • Select the correct answer from each drop-down menu.
    8·1 answer
  • You cannot then move and resize the control on the form as desired with your mouse TRUE OR FALSE​
    6·2 answers
  • with the current computer development, explain 5th areas where computer is applied for the social economic dwvelopment in societ
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!