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
How is a Creative Commons license different from a regular copyright?
gayaneshka [121]

Answer:

Creative Commons is actually a license that is applied to a work that is protected by copyright. It's not separate from copyright, but instead is a way of easily sharing copyrighted work. ... Copyright confers some pretty heavy duty protections so that others don't use your work without your permission.

Explanation:

8 0
3 years ago
Chris needs to take the opposite of a Boolean value. Which operator does so:
alina1380 [7]
B is the best answer
5 0
3 years ago
We look for trends when we are looking at data. What is a trend?
yulyashka [42]
A subject of what people say at the time, most used, famous at the present time
4 0
3 years ago
Read 2 more answers
When referring to hard drives, access time is measured in
Phoenix [80]

when ur looking at a hard drives time is measured in milliseconds

3 0
4 years ago
The Status bar is used to control the majority of the drafting settings in AutoCAD
kifflom [539]

The answer is "true"

Hope this helps!

6 0
4 years ago
Other questions:
  • What is a task that an internet database allows you to perform
    15·2 answers
  • What is a geotag?
    10·1 answer
  • identify three of the many shared ethical standards among businesses that are incorporated in codes of ethics
    6·2 answers
  • The Internet and World Wide Web allow almost any business to be global. What are two important results of this process?
    8·1 answer
  • Can somebody please halp meeee?? I'm doing Web design.
    12·2 answers
  • What is decorator and how to use it wit Python programming language?
    15·1 answer
  • c++ Consider this data sequence: "3 11 5 5 5 2 4 6 6 7 3 -8". Any value that is the same as the immediately preceding value is c
    14·2 answers
  • Which ofthe following calls on human feelings, basing the argument onaudience needs or sympathies?
    5·1 answer
  • "The ability to assign system policies, deploy software, and assign permissions and rights to users of network resources in a ce
    7·1 answer
  • Why are digital computers more popular these days?​
    14·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!