1.What is the output of the following program segment? int num = 0;int count;int
ID: 3860911 • Letter: 1
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 = 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:
Explanation / Answer
(1)num = 2 * (count - 2) + y
count will have 0 , 1 , 2 , 3 , 4 and y =1
so num = 2*( 0 - 2) + 1 = -4 + 1 = -3
num = 2 * (1- 2) + 1 = -2 + 1= -1
num = 2 * (2 - 2) + 1 = 2 * 0 + 1 =1
num = 2 * (3 - 2) + 1 = 2 + 1 = 3
num = 2 * (4 - 2) + 1 = 2* 2+ 1 = 4+1 =5
so output is -3 -1 1 3 5 5(//as count)
(2) x=5 and y =50
x in increasing by 10 every time so loop will run for 15 , 25 , 35 , 45 . So 4 times .
x=4 y=70
here x is multiplying by 2 every time so the loop will run for 8 , 16 , 32 , 64 . Means 4 times .
x=3 , y=10 and x=x+2 that means now x =5
so while(x>=y) doesn't run for even once , so while loop runs 0 times .
//using while loop
int limit = 5;
int total = 1;
int j;
while(j=<limit)
{
cout << total * j << endl;
total = total – 1 + j;
j=j+1;
}
//using do while loop
int limit = 5;
int total = 1;
int j;
do
{
cout << total * j << endl;
total = total – 1 + j;
j=j+1;
}
while(j!=limit+1);
(4)
case one :
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = (i + 1); j <= 5; j++)
cout << setw(5) << j;
cout << endl;
}
The output will be
2 3 4 5 //Enters to the j loop when i is 2
3 4 5 //i was incremented by 1 so it starts from 3
4 5 //again i was incremented by 1 so it starts from 4
5 //here the end comes because i is at the highest value at the end of the loop termination
setw() determines the width between output elements .
case two:
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;
}
the output of this case would be
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
Based on the calculations
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 of the above case is 1 2 3 4 5
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;
}
This case will print a pattern as
121
12321
1234321
123454321
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.