Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

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 using namespace std; int main() { int total = 0, count = 0, number; do { cin >> number; total = total + number; count++; } while (number != -1); cout << "The number of data read is " << count << endl; cout << "The sum of the numbers entered is " << total << endl; return 0; }

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 using namespace std; int main() { int x, y, z; x = 4; y = 5; z = y + 6; do { cout << z << " "; z = z + 7… # What is the output of the following program? #include using namespace std; int main() { int x, y, z; x = 4; y = 5; z = y + 6; do { cout << z << " "; z = z + 7; } while (((z - x) % 4) != 0); cout << endl; return 0; }

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?

Access millions of textbook solutions in one place

  • Access over 3 million high quality textbook solutions
  • Access our popular flashcard, quiz, mock-exam and notes features
  • Access our smart AI features to upgrade your learning
Get Vaia Premium now
Access millions of textbook solutions in one place

Recommended explanations on Computer Science Textbooks