Docsity

Solved Quiz 3 for Object-Oriented Programming I | CMSC 131, Quizzes of Computer Science

Toggle sidebar
bg1
Here is one possible solution. In addition to x, we use three variables:
• sum: cumulative sum of even numbers that will be output.
• number: holds the number of values that the user will input.
• curr: is a running counter of the number of input values
We begin by reading the number of input values. After this, with each current value x, we
add x to the cumulative sum if the running counter is even or if x is even. (Note that we
allowed for either of these two interpretations of the problem.) We exit the loop as soon
as we discover that we have reached the number of input values, and print the sum.
sum = 0
number = read()
curr = 0
while (curr < number) {
x = read()
curr++
if (curr is even) { **OR** if (x is even) {
sum += x
}
}
print(sum)

Partial preview of the text

Download Solved Quiz 3 for Object-Oriented Programming I | CMSC 131 and more Quizzes Computer Science in PDF only on Docsity!

Here is one possible solution. In addition to x, we use three variables:

• sum: cumulative sum of even numbers that will be output.

• number: holds the number of values that the user will input.

• curr: is a running counter of the number of input values

We begin by reading the number of input values. After this, with each current value x, we

add x to the cumulative sum if the running counter is even or if x is even. (Note that we

allowed for either of these two interpretations of the problem.) We exit the loop as soon

as we discover that we have reached the number of input values, and print the sum.

sum = 0 number = read() curr = 0 while (curr < number) { x = read() curr++ if (curr is even) { _OR_ if (x is even) { sum += x } } print(sum)