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]
3 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]3 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
The beginning statement of a loop is called a declaration.<br> True<br> False
Ilya [14]
It is true the reason why it is true is
4 0
2 years ago
Read 2 more answers
A device capable of copying a graphic, document, or other object is called a
Amiraneli [1.4K]
A printing press or printer
5 0
3 years ago
Read 2 more answers
What should you do if a reporter asks you about potentially classified information on the web
Natasha_Volkova [10]

Answer:

Neither confirm nor deny the information is classified.

Explanation:

This action is taken because if you answer that the information is classified, more questions will arise about why it is classified thus leaving you in an awkward situation to respond (since it is classified by something).

In the event that you answer that it is not classified, it also gives rise to the reason that the information is not of simple access also exposing you to answering more questions and having to release the confidential information.

By denying both confidentiality and non-confidentiality, you refer to ignorance of the subject by avoiding further questions about such information on the web.

5 0
3 years ago
How many years does it take in total to complete Bachelor’s, Master’s, and Phd in CS?
muminat

Answer:

3 years of study - Bachelor's degree

2 years of research - Master's degree

3 years of research to earn a PhD

So, On total it would take 8 years!

7 0
3 years ago
What is one similarity between low-and high-level programming languages? (5 points)
aleksandr82 [10.1K]

Answer:

Both high and low level computer languages are used to communicate directly with a computer, so the answer is D.

Explanation:

5 0
3 years ago
Other questions:
  • A potential threat to administrators’ ability to manage the correctional system is
    12·1 answer
  • What was one complication caused by the tsunami that struck japan in 2011?
    15·1 answer
  • Analyze the following code. Number[] numberArray = new Integer[2]; numberArray[0] = new Double(1.5); Question 9 options: A) Sinc
    13·1 answer
  • Who where the romanovs? What happened to them ?
    7·1 answer
  • Who developed the idea of a universal network?
    11·1 answer
  • Given an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the
    11·1 answer
  • Which of the following is a sample IPv4 address?
    14·1 answer
  • You need to replace a broken monitor on a desktop system. You decide to replace it with a spare monitor that wasn't being used.
    15·2 answers
  • What is a software program for navigating the web and displaying websites and pages?
    5·1 answer
  • What is this line called that appears and disappears in the search box<br><br> WILL MARK BRAINLIEST
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!