Question
In C++ 9. Rewrite the following code segment and two different ways, first using a while loop and then a do...while loop. Note that both
In C++
9. Rewrite the following code segment and two different ways, first using a while loop and then a do...while loop. Note that both must the same output as the following code segment:
int limit = 4; int first = 5; int j; for(j = 1; j <= limit; j++) {
cout << first * j << endl;
first = first + (j - 1);
}
cout << endl;
10. To learn how nested for loops work, do a walk-through of the following program segments and in each case determine the exact output.
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