Problem 36
The do....while loop in the following program is supposed to read some numbers
until it reaches a sentinel (in this case, -1 ). It is supposed to add all of
the numbers except for the sentinel. If the data looks like:
12 5 30 48 -1
the program does not add the numbers correctly. Correct the program so that it
adds
the numbers correctly.
#include
Problem 39
Given the following program segment: for (number = 1; number <= 10; number++) cout << setw(3) << number; write a while loop and a do...while loop that have the same output.
Problem 40
Given the following program segment: j = 2; for (i = 1; i <= 5; i++); { cout << setw(4) << j; j = j + 5; } cout << endl; write a while loop and a do...while loop that have the same output.
Problem 41
include
Problem 42
To learn how nested for loops work, do a walk-through of the following program segments and determine, in each case, the exact output. a. int i, j; for (i = 1; i <= 5; i++) { for (j = 1; j <= 5; j++) cout << setw(3) << i; cout << endl; } b. int i, j; for (i = 1; i <= 5; i++) { for (j = (i + 1); j <= 5; j++) cout << setw(5) << j; cout << endl; } c. int i, j; for (i = 1; i <= 5; i++) { for (j = 1; j <= i; j++) cout << setw(3) << j; cout << endl; } d. const int M = 10; const int N = 10; int i, j; for (i = 1; i <= M; i++) { for (j = 1; j <= N; j++) cout << setw(3) << M * (i - 1) + j; cout << endl; } e. int i, j; for (i = 1; i <= 9; i++) { for (j = 1; j <= (9 - i); j++) cout << " "; for (j = 1; j <= i; j++) cout << setw(1) << j; for (j = (i - 1); j >= 1; j--) cout << setw(1) << j; cout << endl; }
Problem 43
What is the output of the following program segment? int count = 1; do cout << count *(count - 2) << " "; while (count++ <= 5); cout << endl;
Problem 44
What is the output of the following code? int num = 12; while (num >= 0) { if (num % 5 == 0) break; cout << num << " "; num = num - 2; } cout << endl;
Problem 46
What does a break statement do in a loop?