1. What is the output of the following program? #include <iostream> using namesp
ID: 3818161 • Letter: 1
Question
1. What is the output of the following program?
#include <iostream>
using namespace std;
int main()
{
int myNum = 10;
int yourNum = 30;
if (yourNum % myNum == 3)
{
yourNum = 3;
myNum = 1;
}
else if (yourNum % myNum == 2)
{
yourNum = 2;
myNum = 2;
}
else
{
yourNum = 1;
myNum = 3;
}
cout << myNum << " " << yourNum << endl;
return 0;
}
2. What is the output of the following statements?
1. if (10 <= 2 * 5)
cout << "10 ";
cout << "2 * 5";
cout << endl;
2. if (6 == 2 * 4 - 2)
cout << 3 * 4 / 6 – 8 << endl;
cout << "**" << endl;
3. What is the output of the following C++ code?
int x = 10;
int y = 20;
if (x < 20 && y > 20)
{
x = 2 * x;
y = y / 2;
cout << x << " " << y << " " << x - y << endl;
}
else
{
x = y / x;
cout << x << " " << y << " " << x * x + y * y << endl;
}
4. State whether the following are valid switch statements. If not, explain why.
Assume that n and digit are int variables.
switch (n <= 2)
{
case 0:
cout << "Draw." << endl;
break;
case 1:
cout << "Win." << endl;
break;
case 2:
cout << "Lose." << endl;
break;
}
switch (digit / 4)
{
case 0,
case 1:
cout << "low." << endl;
break;
case 1,
case 2:
cout << "middle." << endl;
break;
case 3:
cout << "high." << endl;
}
5. Suppose that num is an int variable. Consider the following C++ code:
cin >> num;
if (num >= 0)
switch (num)
{
case 0:
num = static_cast<int>(pow(num, 3.0));
break;
case 2:
num = ++num;
break;
case 4:
num = num – 4;
break;
case 5:
num = num * 4;
case 6:
num = num / 6;
break;
case 10:
num--;
break;
default:
num = -20;
}
else
num = num + 10;
a. What is the output if the input is 5?
b. What is the output if the input is 26?
c. What is the output if the input is 2?
d. What is the output if the input is -5?
6. Suppose that beta is an int variable. Consider the following C++ code:
cin >> beta;
switch (beta % 7)
{
case 0:
case 1:
beta = beta * beta;
break;
case 2:
beta++;
break;
case 3:
beta = sqrt(beta * 1.0);
break;
case 4:
beta = beta + 4;
case 6:
beta = beta--;
4
break;
default:
beta = -10;
}
a. What is the output if the input is 11?
b. What is the output if the input is 12?
c. What is the output if the input is 0?
d. What is the output if the input is 16?
7. In the following code, correct any errors that would prevent the program from
compiling or running:
include <iostream>
int main ()
{
int num1, num2;
bool found;
cout << "Enter two integers: ;
cin >> num1 >> num2;
cout << endl;
if (num1 >= num2) && num2 > 0
switch (num % num2)
{
case 1
found = (num / num2) >= 6;
break;
case 2:
case 3
num1 = num2 / 2;
brake;
default:
num2 = num1 * num2;
}
else
{
found = (2 * num2 < num1);
if found
cin >> num2
num 1 = num2 – num1;
temp = (num1 + num2) / 10;
if num2
{
num1 = num2;
num2 = temp;
}
}
cout << num1 << " " << num2 << endl;
return 0;
}
After correcting the code, answer the following questions. (If needed, insert
prompt
lines to inform the user for the input.)
a) What is the output if the input is 10 8 6?
b) What is the output if the input is 4 9 11?
8. The following program contains errors. Correct them so that the program will
run and output w = 21.
#include <iostream>
using namespace std;
const int SECRET = 5
int main ()
{
int x, y, w, z;
z = 9;
if z > 10
x = 12; y = 5, w = x + y + SECRET;
else
x = 12; y = 4, w = x + y + SECRET;
cout << "w = " << w << endl;
return 0;
}
9. Write a program that prompts the user to input a number. The program should
then output the number and a message saying whether the number is positive,
negative, or zero.
10. Write a program that prompts the user to input three numbers. The
program should then output the numbers in ascending order.
Explanation / Answer
1.
3 1
2.1
10 2 * 5
2.2
compilation error because cout takes string or variable or expression in terms of variables.
3.
2 20 404
4.
switch (n <= 2) is a valid switch statement
case 0:
case 1:
cout << "low." << endl;
like in question 6 ,the above code will work but switch does not take matching cases thus we cannot take "case 1" twice like we've done here.therefore,invalid switch statement.
5.
the code does not return anything in any case because there is no cout statement.
6.
there is a stray ' 4' in the code ,right under case 6.That gives a compilation error. Even if that is omitted, the code does not output anything because it does not have any cout statement.
7.
#include <iostream>
#include<stdio.h>
using namespace std;
int main ()
{
int num1,num2,temp;
bool found;
cout << "Enter two integers:";
cin >> num1 >> num2;
cout << endl;
if ((num1 >= num2) && num2 > 0)
switch (num1 % num2)
{
case 1:
found = (num1 / num2) >= 6;
break;
case 2:
case 3:
num1 = num2 / 2;
break;
default:
num2 = num1 * num2;
}
else
{
found = (2 * num2 < num1);
if(found)
{
cout<<"enter another integer";
cin >> num2;
num1 = num2 - num1;
temp = (num1 + num2) / 10;}
if (num2)
{
num1 = num2;
num2 = temp;
}
}
cout << num1 << " " << num2 << endl;
fflush(stdin);
return 0;
}
7.a
Enter two integers:10
8
4 8
7.b
Enter two integers:4
9
9 4310046
8.
#include <iostream>
using namespace std;
const int SECRET = 5;
int main ()
{
int x, y, w, z;
z = 9;
if(z>10)
{
x = 12; y = 5;
w = x + y + SECRET;
}
else
x = 12; y = 4, w = x + y + SECRET;
cout << "w = " << w << endl;
return 0;
}
9.
#include <iostream>
using namespace std;
int main ()
{
int x;
cout << "enter a number:";
cin>>x;
if(x==0)
cout<<"no. is zero";
else if(x>0)
cout<<"no. is positive";
else
cout<<"no. is negative";
return 0;
}
10.
#include <iostream>
using namespace std;
int main ()
{
int x,y,z;
cout<<"enter three numbers: ";
cin>>x>>y>>z;
if(x<y && x<z)
{
cout<<x<<" ";
if(y<z)
cout<<y<<" "<<z;
else
cout<<z<<" "<<y;
}
if(y<x && y<z)
{
cout<<y<<" ";
if(x<z)
cout<<x<<" "<<z;
else
cout<<z<<" "<<x;
}
if(z<x && z<y)
{
cout<<z<<" ";
if(y<x)
cout<<y<<" "<<x;
else
cout<<x<<" "<<y;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.