5. To learn how nested for loops work, do a walk-through of the following progra
ID: 3924334 • Letter: 5
Question
5. 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; }
Explanation / Answer
a)
output:
11111 //Field automatically expand to fit the 5 digit value
22222
33333
44444
55555
b)
output:
-1111 //Here "-" indicates blank spaces.as setw(5) so total field width should be 5.
--222
---33
----4
c)
output:
--1
-22
333
4444 //Field automatically expand to fit the 4 digit value
55555 //Field automatically expand to fit the 5 digit value
d)
output:
12345678910 //Field automatically expand to fit the 10 digit value
11121314151617181920
21222324252627282930
31323334353637383940
41424344454647484950
51525354555657585960
61626364656667686970
71727374757677787980
81828384858687888990
919293949596979899100
e)
output:
1
12
1232
123432
12345432
1234565432
123456765432
12345678765432
1234567898765432
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.