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
What do you understand by technological depenence?
son4ous [18]

Answer:

It helps you understand stuff faster

Explanation:

7 0
3 years ago
What does rumor Mean Cause I don't know what it means
aksik [14]
Its like a fake message that people believe. for example say that jimmy went to a party. people might start making up things about what happened with him to make gossip. Rumors are not always true, and they are fake a lot of the time. 
8 0
3 years ago
Read 2 more answers
Hard disk is a sequential data access medium. true or false?​
Crazy boy [7]

My answer is TRUE

Explanation:

Hope it help!!

3 0
3 years ago
Which option will you use to show your presentation to the audience?
spin [16.1K]

Answer:

Powerpoint is a good option.

Explanation:

You can also connect the computer that has to powerpoint to a larger monitor.

Hope it helps! :)

3 0
3 years ago
Write an application that displays a series of at least five student ID numbers (that you have stored in an array) and asks the
babymother [125]

Answer:

See explaination for the program code

Explanation:

// ScoreException.java

class ScoreException extends Exception

{

ScoreException(String msg)

{

super(msg);

}

}

// TestScore.java

import java.util.Scanner;

public class TestScore

{

public static void main(String args[]) throws ScoreException

{

Scanner sc = new Scanner(System.in);

int studentId[] = { 1201, 1202, 1203, 1204, 1205 };

int scores[] = new int[5];

int score = 0;

for(int i = 0; i < studentId.length; i++)

{

try

{

System.out.print("Enter a numeric test score for the student"+(i+1)+" of Id "+studentId[i]+" : ");

score = sc.nextInt();

if(score < 0 || score > 100)

{

scores[i] = 0;

throw new ScoreException("Input should be between 1 and 100");

}

else

{

scores[i] = score;

}

}

catch(ScoreException ex)

{

System.out.println("\n"+ex.getMessage()+"\n");

}

}

//displaying student details

System.out.println("\n\nStudent Id \t Score ");

System.out.println("=============================");

for(int i = 0; i < studentId.length; i++)

{

System.out.println(studentId[i]+"\t\t"+scores[i]);

}

}

}

8 0
3 years ago
Other questions:
  • Show the array that results from the following sequence of key insertions using a hashing system under the given conditions: 5,
    12·1 answer
  • 4.2 lesson practice last one plzs help
    5·2 answers
  • NEED ANSWER ASAP. CORRECT ANSWER GETS BRAINLIEST! TY
    5·1 answer
  • What are the four different orchestral instrument families?
    6·2 answers
  • Did brainly.com go down today? i coudlnt get in it
    15·2 answers
  • You began a small mobile app company. To help market your apps, you create a social media site. The site allows users to join co
    15·1 answer
  • I'm getting pretty desperate plz help me, I'll give brainiest, and ill make a free question worth 100 points this is for coding
    10·1 answer
  • Complete main() to read dates from input, one date per line. Each date's format must be as follows: March 1, 1990. Any date not
    10·1 answer
  • What level of system and network is required for cui
    7·1 answer
  • The code to perform a binary search is below. Match the variable name with what it holds.
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!