65.5k views
1 vote
How do I solve this and what would "count" represent?

How do I solve this and what would "count" represent?-example-1

1 Answer

7 votes

Answer:

C. I and II

Step-by-step explanation:

(I don't know if this is too late but I hope I am still of some help.)

The variable "count" represents the integer you are adding at the moment. To elaborate, count starts at 1 (because the goal is to add all the integers from 1 to 5), and then increases each time by 1 to get the next integer until count is 6 where it does not add it anymore.

Understanding what the code does (skip if you know what it does):

In "repeat until", sum is count + sum (where sum is the previous sum). Sum starts at 0, then adds count (which is 1 at the start), then count increases by 1. Right now, we have sum equal to 1 and count equal to 2. Repeat and now sum is equal to 3. Count is now equal to 3 and so is sum, repeat again. Sum is 6 and count is 4. Again, sum is 10 and count is 5. One last time, sum is 15 and count is 6. Right now, count > 5, so we now return sum.

Sum = 15

Solving the actual problem:

Max is a new variable that replaces 5, so it is easier to calculate any sum of integers greater than 1 by just changing max.

Statment I says that max should be an input parameter for the procedure sumOfInts. I agree because like I said above max is made so it is easier to set it to any number you want, so it should be an input paramter, that way it has a value an it isn't null.

Statement II says "repeat until" condition should change to include max instead of 5, so the condition becomes count > max. I also agree with this one because that's the whole point of max.

Statement III says max should be less than 5 as the "repeat until" condition, this does not solve the problem because the sum will increase forever if max < 5 and not set sum to 0 if the max > 5. I disagree with this statement.

Only I and II are good, option C.

User Erce Tilav
by
3.0k points