Xylitol

Diamond Member
Aug 28, 2005
6,617
0
76
What would this be?



Consider the following incomplete method:

public int total(int[] scores)
// precondition: The sentinel -999 occurs
// somewhere in scores
{
int k = 0;
int sum = 0;
while (scores[k] != -999)
{
<program statements>
}
return sum;
}

Method total is intended to return the sum of the integers in parameter scores, beginning with the first integer in scores and up to, but not including the sentinel ?999 (which occurs somewhere in scores). Which of the following code segments could be used to replace <program statements> so that total will work as intended?
 

jman19

Lifer
Nov 3, 2000
11,220
653
126
Originally posted by: Xylitol
What would this be?



Consider the following incomplete method:

public int total(int[] scores)
// precondition: The sentinel -999 occurs
// somewhere in scores
{
int k = 0;
int sum = 0;
while (scores[k] != -999)
{
<program statements>
}
return sum;
}

Method total is intended to return the sum of the integers in parameter scores, beginning with the first integer in scores and up to, but not including the sentinel ?999 (which occurs somewhere in scores). Which of the following code segments could be used to replace <program statements> so that total will work as intended?

What would it be? Looks to me like an attempt to get ATOT to do your homework :roll:
 

Cooler

Diamond Member
Mar 31, 2005
3,835
0
0
sum += scores[k];
k++;

GML3G0 beat me to it.
Also if -999 is not in the array it will cause an indexou tof bound to be thrown.
 

ASK THE COMMUNITY