Can you please help me answer this six questions? 1 )Suppose that the input is 3
ID: 3798791 • Letter: C
Question
Can you please help me answer this six questions?
1)Suppose that the input is 3 4 6 7 2 -1. What is the output of the
following code? (2, 3)
int num;
int sum;
cin >> num;
sum = num;
while (num != -1)
{
sum = sum + 2 * num;
cin >> num;
}
cout << "Sum = " << sum << endl;
2)Suppose that the input is 10 -6 12 -5 -4 0. What is the output of the
following code? (2, 3)
int num;
int sum = 0;
cin >> num;
while (num != 0)
{
if (num > 0)
sum = sum + num;
else
sum = sum - num;
cin >> num;
}
cout << "Sum = " << sum << endl;
3)Suppose that the input is:
58 23 46 75 98 150 12 176 145 -999
What is the output of the following program? (2, 3)
#include <iostream>
using namespace std;
int main()
{
int num;
int count = 0;
cin >> num;
while (num != -999)
{
count++;
cout << num % count << " ";
cin >> num;
}
cout << endl;
return 0;
}
4)What type of loop, such as counter-control or sentinel-control, will you use
in each of the following situations? (3)
a. Sum the following series: 1 + (2 / 1) + (3 / 2) + (4 / 3) + (5 / 4)
+ ... + (10 / 9)
b. Sumthe following numbers, except the last number: 17, 32, 62, 48, 58, -1
c. A file contains an employee’s salary. Update the employee’s salary.
5)The following program contains errors that prevent it from compiling and/
or running. Correct all such errors. (4)
#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;
}
6) To learn how nested for loops work, do a walk-through of the following
program segments and determine, in each case, the exact output. (4, 7)
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
1) The initial value of sum will be 3.
Then loop starts and sum becomes 9 after that it becomes 17,29,43,47.
The output is "Sum = 47"
2) sum starts at 0. Basically we are adding the absolute value of all the input numbers. So the value of sum will be 10,16,28,33,37. Now the loop stops.
The output is "Sum = 37"
3) count starts at 0. In each iteration it increases by 1.
Output will be:
0 (Not part of ouput -> num = 58, count=1)
1 (num = 23, count=2)
1 (num = 46, count=3)
3 (num = 75, count=4)
3 (num = 98, count=5)
0 (num = 150, count=6)
5 (num = 12, count=7)
0 (num = 176, count=8)
1 (num = 145, count=9)