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
The transmission method that sends data to every device on a lan is known as a _____ transmission.
lina2011 [118]
The transmission method that sends data to every device on a lan is known as a broadcast transmission. This type of transmission is also referred toa as <span> one-to-all transmission method. The network carries a message to all devices at the same time.
</span><span>Sometimes broadcasts are a result of network devices continually announcing their presence in the network, so that other devices don't forget who is still a part of the network.</span>
4 0
4 years ago
Read 2 more answers
A goal should be___.
Luda [366]

Answer:

D

Explanation:

5 0
3 years ago
Read 2 more answers
Logic errors are easily identified when a program is compiled true or false
lord [1]
<span>A compiler executes each program statement as soon as it is translated.
</span>The statement that logic errors are easily identified when a program is compiled is false, because the compiler locates only syntax errors, but logic errors <span>can be eliminated only through careful examination of your program. </span>
Logic errors are usually more difficult to find and resolve than syntax errors.


7 0
3 years ago
Read 2 more answers
The general case in a recursive function is the case for which the solution is obtained directly.
Reika [66]

Answer: False.

Explanation:

The general case of recursive function is when the solution is obtained recursively by simplification at each step.

However, it is the base case in a recursive function when the solution is obtained directly.

The general case must be reducible to base to arrive at a solution else the recursion would be a infinite recursion.

4 0
3 years ago
The machine that stores your data and files​
Oxana [17]

Answer:

Any file you create or download saves to the computer's secondary storage. There are two types of storage device used as secondary storage in computers: HDD and SSD.

thenks and mark me barinliestt :))

7 0
3 years ago
Other questions:
  • You are the system administrator for a medium-sized Active Directory domain. Currently, the environment supports many different
    7·1 answer
  • The ____ is the configuration of a system documented at the beginning of the project and consists of all necessary system requir
    8·1 answer
  • How do u make a bored builder
    8·2 answers
  • What was the best Metal Gear Solid you enjoyed the most?​
    6·1 answer
  • What information and options can be found on the File tab?
    8·1 answer
  • Haigy Paigy is as a children's invented language which sounds exactly like English, except that "aig" is inserted before the vow
    12·1 answer
  • 1. You are designing a program that will keep track of the boxes in a doctor’s office. Each box will have three attributes: date
    10·1 answer
  • Which of the following sentences from the section "The Limits To Free Enterprise With A Mixed Economy" BEST develops a central i
    10·1 answer
  • What is range work book​
    8·1 answer
  • A _______ attack is where the input includes code that is then executed by the attacked system.
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!