SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
More Flow Of Control
Exercise:
Self-test Exercises
Question:37 | ISBN:9780321531346 | Edition: 7

Question

Write a loop that will read in a list of even numbers (such as 2, 24, 8, 6)

and compute the total of the numbers on the list. The list is ended with a

sentinel value. Among other things, you must decide what would be a

good sentinel value to use.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

You can use any odd number as a sentinel value.

int sum = 0, next;
cout << "Enter a list of even numbers. Place an\n"
	<< "odd number at the end of the list.\n";
cin >> next;
while ((next % 2) == 0)
{
	sum = sum + next;
	cin >> next;
}

 

0 0

Discussions

Post the discussion to improve the above solution.