Question
Describe the output of the following C++ code. int i = 1; while(i > sum; num = sum; while (num != -1) { cin >>
Describe the output of the following C++ code.
int i = 1; while(i <= 10) { cout << i << " " << i*i << endl; i++; } |
|
Rewrite the code segment above using a for loop. Make sure it would produce the same output.
Suppose that the input is 25 9 18 16 -1. What is the output of the following code?
int num; int sum; cin >> sum; num = sum; while (num != -1) { cin >> num; sum = sum + num; } cout << "Sum = " << sum << endl; | |
Correct the following code so that it reads and finds the sum of 20 numbers:
// not quite correct: | // correct: |
int count = 0; int sum = 0; cin >> num; while(count <= 20); { cin >> num; count++; sum = sum + count; } | |
What is the output of the following program segment?
int count = 5; while(--count > 0) cout << count << " "; cout << endl; | |
Prepare variable(s) and write a for statement to add all the multiples of 3 between 1 and 100.
What is the output of the following C++ program segment?
int j; for (j = 0; j < 8; j++) { cout << j * 25 << " - "; if (j != 7) cout << (j + 1) * 25 - 1 << endl; else cout << (j + 1) * 25 << endl; } | |
How many times will each of the following loops execute? What is the output in each case?
a. | x = 5; y = 50; do x = x + 10; while (x < y); cout << x << " " << y << endl; |
|
b. | x = 5; y = 20; do x = x + 2; while (x >= y); cout << x << " " << y << endl; |
|
c. | x = 5; y = 35; while (x < y) x = x + 10; cout << x << " " << y << endl; |
|
Given the following program segment, write a while loop and a do...while loop that would have the same output.
int limit = 4; int first = 5; int j; for(j = 1; j <= limit; j++) { cout << first * j << endl; first = first + (j - 1); } cout << endl; |
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 <= i; j++) cout << setw(2) << j; cout << endl; } |
|
b. | const int M = 5; const int N = 10; int i, j; for (i = 1; i <= M; i++) { for (j = 1; j <= N; j++) cout << setw(3) << N * (i-1) + j; cout << endl; } |
|
c. | In b. above, which loop variable keeps track of each row? Which loop variable keeps track of each column? |
|
d. | int i, j; for (i = 1; i <= 9; i++) { for (j = 1; j <= (9 - i); j++) // loop B cout << " "; // loop B for (j = 1; j <= i; j++) cout << setw(1) << j; for (j = (i - 1); j >= 1; j--) // loop D cout << setw(1) << j; // loop D cout << endl; } |
|
e. | What does loop B contribute to the output from program d above? What does loop D contribute to the output from program d above? |
|
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started