Question
1.What is the output of the following program segment? int num = 0;int count;int y = 1;for (count = 0; count < 5; ++count){ num
1.What is the output of the following program segment? int num = 0;int count;int y = 1;for (count = 0; count < 5; ++count){ num = 2 * (count - 2) + y; cout << num << " ";}cout << count << " " << endl;
2.How many times will each of the following loops execute? Case one: int x = 5; int y = 50; do x = x + 10; while (x < y); Answer: Case two: int x = 4; int y = 70; do x = x * 2; while (x < y); Answer: Case three: int x = 3; int y = 10; do x = x + 2; while (x >= y); Answer:
3. Write a while loop and a do...while loop that both have the same output as the following code segment: int limit = 5;int total = 1;int j;for(j = 1; j <= limit; j++){ cout << total * j << endl; total = total 1 + j; } cout << endl;
4. To learn how nested for loops work, do a walk-through of the following program segments and determine the exact output in each case. Case one: int i, j; for (i = 1; i <= 5; i++) { for (j = (i + 1); j <= 5; j++) cout << setw(5) << j; cout << endl; } Output: Case two: int i, j; for (i = 1; i <= 5; i++) { for (j = 1; j <= i; j++) cout << setw(3) << j; cout << endl; } Output: Case three: const int M = 5; const int N = 5; int i, j; for (i = 1; i <= M; i++) { for (j = 1; j <= N; j++) cout << setw(3) << M * (i - 1) + j; cout << endl; } Output: Case four: int i, j; for (i = 1; i <= 5; i++) { for (j = 1; j <= (5 - 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; } Output:
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