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]
4 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]4 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
Do opportunity costs only occur when making spending decisions
Naily [24]

Answer:

yes there is opportunity in every thing such as do i buy this car what am i going to miss out on if i buy this what does this offer

Explanation:

6 0
3 years ago
Drag the tiles to the correct boxes to compete the pairs.
levacccp [35]

Provides the fescription of a grammar error = Explain

Changes all occurrences of the misspelled words = Change all

Change the behavior of the grammar checker = Options

Enables you to add or remove misspelled words = Replace


3 0
3 years ago
Read 2 more answers
A network technician is setting up a web server for a small company. The company has obtained a domain name, company-a, from a d
Alja [10]

Answer:

There is Name resolution failure

Explanation:

This normally occurs when the computer can't access the host (your server). It can be caused by several factors, such as:

-Internet connectivity is down

-The client does not have DNS servers configured or is configured with the incorrect DNS server IP addresses.

-The DNS servers are failing.

3 0
4 years ago
Suppose a reference variable of type Double called myDouble has already been declared. There is also a double variable x that ha
eimsori [14]

Answer:

Double newMyDouble = new Double(x);

myDouble = newMyDouble2;

Explanation:

Here the new object newMyDouble is firstly the initial value of said 'double x'. In the second line myDouble is the reassigned the the newMyDouble, which is in fact, the value of x. This exercise helps you understand the syntax of using reference variables in JAVA and their assignment along with reassignment.

5 0
3 years ago
What visual practice provides more information about logical relations when creating an ERD
Marina86 [1]

Answer:

Logical Data Model

Explanation:

An Entity Relationship Diagram (ERD) is a model or a diagram designed to reveal the relationship between various entities such as people, places, objects, and so on within an institution. Entity relationship diagram is also known as Entity relationship model. This model is used to design relational database by various sectors such as education, engineering, business, etc.

<u>ERD is designed using conceptual data model, logical data model, and physical data model</u>.

The visual practice or model that gives more insight into the logical relations among the entity is the logical data model.

The logical data model is used to illustrate more detailed operational and relational data among the entities. It is more detailed than the conceptual data.

So, the correct answer is logical data model.

5 0
3 years ago
Other questions:
  • What are some innovations that a cell phone has undergone since its original invention?
    14·1 answer
  • Suppose that, in addition to edge capacities, a flow network has vertex capacities. That is each vertex vv has a limit l(v)l(v)
    7·1 answer
  • After typing “application letter” in the Search window in the New dialog box, Nate clicks the Education category. Three letters
    9·1 answer
  • Modern processors today utilize _____ when fetching instructions as a means of predicting where the branch will go and even begi
    13·1 answer
  • What was the first e-commerce service
    13·1 answer
  • When creating a documentary, what is not an appropriate source for footage?
    11·1 answer
  • A carver begins work on the following block of granite that weighs 2700 g. What is the density of the granite?
    13·1 answer
  • Algorithm and flowchart to find the perimeter and area of square​
    15·1 answer
  • What are some common predefined number formats used in the United States? Check all that apply.
    15·1 answer
  • I have this project and I can't do anything about it, I do really need help ASAP
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!