{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# Exercises - 2.40 Write a program that sums a sequence of...

This preview shows pages 1–2. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 2.40 Write a program that sums a sequence of integers. Assume that the first integer read speciﬁes the number of values remain— ing to be entered. Your program should read onl},I one value per input statement. A typical input sequence might be 5 100 200 300 400 500 where the 5 indicates that the subsequent 5 values are to be summed. If Exercise 2.40 solution #lnclude <iostream: using stdrxcout; using statsendl; using std: :cin; int main“ { int sum = 0; number, value; cout {x "Enter the number of values to be processed: "; cin >> number; for { int i = l; i <= number; 1++ J { cout << "Enter a value: "; cin >> value; sum + = value ; ﬂmwammwm—cﬂmwamhwm— cout <«z 'sum of the ' «< number <«z " values is " <4 sum << endl; return 0; MMMNMNM China-whi—‘D Enter the number of values to be processed: 3 Enter a value: 7 Enter a value: 8 Enter a value: 9 sum of the 3 values is 24 2.4] Write a program that calculates and prints the average of several integers. Assume the last value read is the sentinel 9 9 9 9. A typical input sequence might be 10 811? 99999 mmmmgmmmenameﬁahmvﬂwsgmaﬂg9ﬁ9ﬁmbuﬂwhmd l f! Exercise 2.41 solution 2 #include <ioetream: 3 4 using std::eout; 5 using std::endl; 6 using std::cin; T 3 int main{] 9 £ l0 int value, count = 0, total = D; II l2 cout << "Enter an integer {9999 to end]: "; l3 cin >2 value; l4 l5 while { value != 9999 ] { l6 total += value; I? ++count; l3 cout {c "Enter next integer [9999 to end}: "; l9 cin >> value; 20 l 2] 22 if { count I: 0 23 tout :< "\nThe average is: " 24 << static_cast< double : { total ] I count << endl; 25 else 26 cout << "\nNo values were entered.‘ << endl; 2? 23 return 0; 29 } Enter an integer [9999 to end}: 98 Enter next integer {9999 to end]: 65 Enter next integer {9999 to end]: T? Enter next integer {9999 to end]: 43 Enter next integer {9999 to end]: 90 Enter next integer {9999 to end]: 45 Enter next integer {9999 to end]: T6 Enter next integer {9999 to end]: 2 Enter next integer [9999 to end}: 9999 The average is: 60:?5 ...
View Full Document

{[ snackBarMessage ]}

### Page1 / 2

Exercises - 2.40 Write a program that sums a sequence of...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online