Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello can anyone help me about my questions thanks 22. Assume that the following

ID: 3551416 • Letter: H

Question

Hello can anyone help me about my questions thanks


22. Assume that the following code is correctly inserted into a program:

int s = 0;

int i;

for (i = 0; i < 5; i++)

{

s = 2 * s + i;

cout << s << " ";

}

cout << endl;

a. What is the final value of s?

(i) 11 (ii) 4 (iii) 26 (iv) none of these

b. If a semicolon is inserted after the right parenthesis in the for loop

statement, what is the final value of s?

(i) 0 (ii) 1 (iii) 2 (iv) 5 (v) none of these

c. If the 5 is replaced with a 0 in the for loop control expression, what is

the final value of s?

(i) 0 (ii) 1 (iii) 2 (iv) none of these


24. Write a for statement to add all the multiples of 3 between 1 and 100.


26. Suppose that the input is 5 3 8. What is the output of the following code?

Assume all variables are properly declared.

cin >> a >> b >> c;

for (j = 1; j < a; j++)

{

d = b + c;

b = c;

c = d;

cout << c << " ";

}

cout << endl;


28. Suppose that the input is 3 5 7 -6 10. What is the output of the

following code?

int temp = 0;

int num;

int count;

cin >> temp;

for (count = 0; count <= 3; count++)

{

cout << temp << " ";

cin >> num;

temp = temp + num * (count - 1);

}

cout << endl;


30. The following program contains errors that prevent it from compiling and/

or running. Correct all such errors.

#include <iostream>

using namespace sdt;

const int SECRET = 111.25;

int main ()

{

int num1, num2:

double x, y;

cout >> "Enter two integers: ""

cin << num1 << num2;

cout >> endl;

for (count = 1 count > Secret; ++count)

{

x = (num1 + num2) / 2.0;

y = (num1 - num2) % 2.0;

num1 := num1 + num2;

num2 := num2 * (count - SECRET - 1)

}

cout << num1 << " " << num2 << " << x % 5

<< " " << (y % 7) << end;

return;

}


32. How many times will each of the following loops execute? What is the

output in each case?

a. x = 5; y = 50;

do

x = x + 10;

while (x < y);

cout << x << " " << y << endl;

b. x = 5; y = 80;

do

x = x * 2;

while (x < y);

cout << x << " " << y << endl;

c. x = 5; y = 20;

do

x = x + 2;

while (x >= y);

cout << x << " " << y << endl;

d. x = 5; y = 35;

while (x < y)

x = x + 10;

cout << x << " " << y << endl;

e. x = 5; y = 30;

while (x <= y)

x = x * 2;

cout << x << " " << y << endl;

f. x = 5; y = 30;

while (x > y)

x = x + 2;

cout << x << " " << y << endl;


34. Rewrite the following as a for loop:

int i = 0, value = 0;

while (i <= 20)

{

if (i % 2 == 0 && i <= 10)

value = value + i * i;

else if (i % 2 == 0 && i > 10)

value = value + i;

else

value = value - i;

i = i + 1;

}

cout << "value = " << value << endl;

What is the output of this loop?


36. The do. . .while loop in the following program is supposed to read some numbers until it reaches a sentinel (in this case, -1). It is supposed to add all of the numbers except for the sentinel. If the data looks like:

12 5 30 48 -1

the program does not add the numbers correctly. Correct the program so that it adds the numbers correctly.

#include <iostream>

using namespace std;

int main()

{

int total = 0,

count = 0,

number;

do

{

cin >> number;

total = total + number;

count++;

}

while (number != -1);

cout << "The number of data read is " << count << endl;

cout << "The sum of the numbers entered is " << total

<< endl;

return 0;

}


38. Using the same data as in Exercise 36, the following loop also fails. Correct it.

cin >> number;

while (number != -1)

{

cin >> number;

total = total + number;

}

cout << endl;

cout << total << endl;


40. Given the following program 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;

write a while loop and a do. . .while loop that have the same output.


42. 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;

}


44. What is the output of the following code?

int num = 12;

while (num >= 0)

{

if (num % 5 == 0)

break;

cout << num << " ";

num = num - 2;

}

cout << endl;


Thanks

Explanation / Answer

22)

a--->(iii)26

b--->(iv)5

c--->(i)0


24)

int sum = 0;

int i;

for (i = 3; i < 100; i=i+3)

{

sum+= i;

}

cout <<"sum = "<<sum;


26)

11 19 30 49


28)

3 -2 -2 -8


30)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote