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
BARSIC [14]
4 years ago
7

Assume that you have an array of integers named arr. The following program segment is intended to sum arr [0]through arr[n−1], w

here n = arr.length: sum = 0; i = 0; n = arr.length; while (i != n) { i++; sum += arr[i]; } In order for this segment to perform as intended, which of the following modifications, if any, should be made?
Computers and Technology
1 answer:
Paladinen [302]4 years ago
7 0

Answer:

There is only one modification in the above loop i.e. while loop should be like this--

while (i != n)

     {

         sum+=arr[i];   // Line 1, In question segment it is line 2.

          i++;                // Line 2,  In question segment it is line 1.

     }

Output:

Now if the array input is 1,2,3,4,5 then the output is 15.

Explanation:

In the above question, all the line of the segment is right except the body of the loop because--

  • The First line of the loop is increment statement which increments the value of "i" variable from 1 and the value of "i" variable will be 1 in the first iteration of the loop
  • The second line starts to add the value from 1'st index position of the array. Hence the segment gives the wrong answer. It adds the arr[1] to arr[n-1].
  • So I interchanged both lines of the while loop as shown in the answer part. I make the line 1 (In question segment) as line 2(In answer part) and line 2 (In question segment) as line 1 (In answer part).

Now It gives the correct output because it can add arr[0] to arr[n-1].

You might be interested in
Modern processors today utilize _____ when fetching instructions as a means of predicting where the branch will go and even begi
ss7ja [257]

Answer:Speculative prediction

Explanation: Speculative prediction is the prediction method which can be optimizing in nature.This method usually runs in such a way that can lead ahead of time by predicting the next step or it's result.

This technique is also known as the dynamic execution which is usually found in the CPU devices and some other modern processing devices.Speculation prediction and execution works even when there is no necessity of the knowing about the next instruction.The result appearing can be correct or incorrect.

3 0
3 years ago
How to type in color on clash royale
Brut [27]
In order to chat your text in color in the famous game Clash Royale of Supercell, you have to use a template below: 
<c + “hexadecimal value of the color of your choice.”> text here </c>.
For example, to make your text in red (ff0000) : <cff0000> text here </c>
8 0
3 years ago
Read 2 more answers
Every computer consists of physical components and nonphysical components. the nonphysical components of a computer that underst
IrinaVladis [17]
Physical component means Computer Hardware which you can touch and non Physical component means Software, which you can not touch. nonphysical component designed specially for physically component. like Microsoft company designed Software according the Hardware for example Microsoft windows 64 bit could not install on which 32 bit Architecture Hardware.
7 0
3 years ago
I need help on this it's not really a subject it's drivers edd
Papessa [141]

WHICH QUESTION DO U NEED HELP WITH

5 0
3 years ago
What is the default file extension for a document created in Word 2016​
navik [9.2K]

Office Open XML (OOXML) format was introduced with Microsoft Office 2007 and became the default format of Microsoft Word ever since. Pertaining file extensions include: .docx – Word document. .docm – Word macro-enabled document; same as docx, but may contain macros and scripts.

7 0
4 years ago
Other questions:
  • This type of connection uses radio waves to connect devices on a network.
    9·2 answers
  • After earning a learners license what test must be successfully passed to earn an operators license in Florida
    14·1 answer
  • What are the three most important jobs of an operating system?
    8·1 answer
  • 6-3.1 After visiting a large number of Web sites to complete your research for a lengthy research paper, your computer begins to
    14·2 answers
  • Ignorance of policy is a legal excuse for an employee. TRUE or FALSE
    11·1 answer
  • What command would Emile use in his word processing software to insert a bar chart that will automatically adjust to changes mad
    6·2 answers
  • According to the passage, what are the goals of the<br> program? Check all that apply.
    11·1 answer
  • Write a static method reOrder56(int[] nums) that return an array that contains exactly the same numbers as the given array, but
    10·1 answer
  • 1. What are the process involved in data processing cycle?​
    11·1 answer
  • what new technology led to the chaos that spurred the establishment of the federal communications commission?
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!