1.) What output would be produced by each of the following segments of code? int
ID: 670892 • Letter: 1
Question
1.)What output would be produced by each of the following segments of code?
int x = 10;
while (x > 0)
{
cout << x << endl;
x = x = 3;
}
int x = 10;
do
{
x = x - 3;
cout << x << endl;
}
while (x > 0);
int x = - 42;
do
{
cout << x << endl;
x = x – 3;
}
while (x > 0);
int x = 10;
while (x > 0)
{
cout << x << endl;
x = x + 3;
}
2. What output will be displayed to the screen by the following code?
for (int count = 0; count < 5; count++)
cout << (2 * count) << “ “;
3. What output will be displayed to the screen by the following code?
for ( int n = 10; n > 0; n = n – 2)
{
cout << “Hello “);
cout << n << endl;
}
What output will be displayed to the screen by the following code?
for (float sample = 2; sample > 0; sample -= 0.05)
cout << sample << endl;
Rewrite the following loops as for loops and show the output produced:
int i = 1;
while (i <= 10)
{
if (i < 5 && i != 2)
cout << i << endl;
i++;
}
int i = 1;
while (i <= 10)
{
cout << i << endl;
i += 3;
}
int m = 100;
do
{
cout << m << endl;
m += 100;
}
while (m < 1000);
Predict the output to the screen of the following nested loops:
int n, m;
for (n = 1; n <= 2; n++)
for (m = 4; m >= 1; m--)
cout << n << “ times “ << m << “ = “ << (n*m) << endl;
What is the output to the screen of the following code?
int k = 0;
while (k <= 15)
{
if (k % 5 == 0)
cout << k << endl;
k++;
}
What is the output to the screen of the following code?
for (int j = -3; j < 3; j += 3)
{
cout << “ j = “ << j << “output “;
if ( j < 0)
cout << -2 * j;
else
cout << j % 2;
cout << endl;
}
What is the output from the following program fragments? Assume the appropriate declarations have been made.
int a = 5;
int b = 90;
do
{
b = (b/a) - 5;
if (b > a)
b = a + 30;
}
while (b >= 0);
cout << “a is “ << a << “ b is “ << b << endl;
for ( int k = - 4; k <= 4; k++)
{
cout << “k = “ << k << “ output “;
int a = k;
if ( k < 0)
cout << - 2 * a;
else
cout << a % 2;
cout << endl;
}
Write a loop that will print Hello to the screen 10 times.
Write a loop that will read a list of even numbers (such as 2, -4, 8, 6) and compute the total of the numbers on the list. The list is ended with a sentinel value. Among other things, you must decide what would be a good sentinel value(s) to use and declare it/them as such.
Explanation / Answer
1.)What output would be produced by each of the following segments of code?
int x = 10;
while (x > 0)
{
cout << x << endl;
x = x = 3;
}
Answer: Infinete Loop
In the Due to Assignment Operator x=x=3, the value of x is 3 and x > 0 always hence infinite loop.
int x = 10;
do
{
x = x - 3;
cout << x << endl;
}
while (x > 0);
Answer: 7 4 1 -2
x=10
x=x-3 --> x=10-3 --> x=7
7 > 0 [while condition checking, true]
x=x-3 --> x=7-3 --> x=4
4 > 0 [while condition checking,true ]
x=x-3 --> x=4-3 --> x=1
1> 0 [while condition checking, true]
x=x-3 --> x=1-3 --> x=-2
-2 > 0 [condition failes] stops the iteration.
int x = - 42;
do
{
cout << x << endl;
x = x – 3;
}
while (x > 0);
Answer: -42
Explanation:
do while loop is exit controlled loop, body of the loop is executed at least once before checking condition.
-42 displayed
-42 > 0 condition fails, stops the iteration.
int x = 10;
while (x > 0)
{
cout << x << endl;
x = x + 3;
}
Answer: Contiously print values like 10 13 16 19 and so on until x value become negative.
Explanation: x=10
x>0 --> True Always
If size of integer is 4 bytes the range of values is -2147483648 to 2147483647, once x value reaches some way around 2147483645 adding 3 results in -2147483648, 2147483646 adding 3 results in -2147483647,2147483647 adding 3 resutls -2147483646.
x becomes negative, stops the iteration.
2. What output will be displayed to the screen by the following code?
for (int count = 0; count < 5; count++)
cout << (2 * count) << “ “;
Output: 0 2 4 6 8
Explanation:
count=0;
0 < 5 True
Cout<< 2*0 << “ ”; 0
count++ count=1
1 < 5 True
Cout<< 2*1 << “ ”; 2
count++ à count=2
2 < 5 à True
Cout<< 2*2 << “ ”; 4
count++ count=3
3 < 5 True
Cout<< 2*3 << “ ”; 6
count++ count=4
4 < 5 True
Cout<< 2*4 << “ ”; 8
count++ count=5
5 < 5 False
3. What output will be displayed to the screen by the following code?
for ( int n = 10; n > 0; n = n – 2)
{
cout << “Hello “);
cout << n << endl;
}
Hello10
Hello8
Hello6
Hello4
Hello2
What output will be displayed to the screen by the following code?
for (float sample = 2; sample > 0; sample -= 0.05)
cout << sample << endl;
2
1.95
1.9
1.85
1.8
1.75
1.7
1.65
1.6
1.55
1.5
1.45
1.4
1.35
1.3
1.25
1.2
1.15
1.1
1.05
1
0.950001
0.900001
0.850001
0.800001
0.750001
0.700001
0.650001
0.600001
0.550001
0.500001
0.450001
0.400001
0.350001
0.300001
0.250001
0.200001
0.150001
0.100001
0.0500008
Rewrite the following loops as for loops and show the output produced:
int i = 1;
while (i <= 10)
{
if (i < 5 && i != 2)
cout << i << endl;
i++;
}
for(int i=1;i<=10;i++)
{
if (i < 5 && i != 2)
cout << i << endl;
}
int i = 1;
while (i <= 10)
{
cout << i << endl;
i += 3;
}
for(int i=1;i<=10;i+=3)
{
cout << i << endl;
}
int m = 100;
do
{
cout << m << endl;
m += 100;
}
while (m < 1000);
for(int m=100;m<1000;m+=100)
{
cout << m << endl;
}
Predict the output to the screen of the following nested loops:
int n, m;
for (n = 1; n <= 2; n++)
for (m = 4; m >= 1; m--)
cout << n << “ times “ << m << “ = “ << (n*m) << endl;
1times 4 = 4
1times 3 = 3
1times 2 = 2
1times 1 = 1
2times 4 = 8
2times 3 = 6
2times 2 = 4
2times 1 = 2
What is the output to the screen of the following code?
int k = 0;
while (k <= 15)
{
if (k % 5 == 0)
cout << k << endl;
k++;
}
0
5
10
15
What is the output to the screen of the following code?
for (int j = -3; j < 3; j += 3)
{
cout << “ j = “ << j << “output “;
if ( j < 0)
cout << -2 * j;
else
cout << j % 2;
cout << endl;
}
j = -3output6
j = 0output0
int a = 5;
int b = 90;
do
{
b = (b/a) - 5;
if (b > a)
b = a + 30;
}
while (b >= 0);
cout << “a is “ << a << “ b is “ << b << endl;
a is 5b is -5
for ( int k = - 4; k <= 4; k++)
{
cout << “k = “ << k << “ output “;
int a = k;
if ( k < 0)
cout << - 2 * a;
else
cout << a % 2;
cout << endl;
}
k = -4 output 8
k = -3 output 6
k = -2 output 4
k = -1 output 2
k = 0 output 0
k = 1 output 1
k = 2 output 0
k = 3 output 1
k = 4 output 0
Write a loop that will print Hello to the screen 10 times.
for ( int k = 1; k <= 10; k++)
{
cout << "Hello"<<k<< endl;
}
loop that will read a list of even numbers (such as 2, -4, 8, 6) and compute the total of the numbers on the list. The list is ended with a sentinel value.
int num;
int sum=0;
while(1) //Condition always true
{
cout<<"Enter a number";
cin>>num;
if(num%2==0) //Check for even or odd
sum+=num;
else //Whenever number is odd stop the iteration
break;
}
cout<<"sum="<<sum<<endl;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.