1)State the order of evaluation of the operators in each of the following C++ st
ID: 3814366 • Letter: 1
Question
1)State the order of evaluation of the operators in each of the following C++ statements and show the value of x after each statement is performed.
a) x = 5 + 4 * 9 / 2 - 1;
b) x = 2 % 2 + 2 * 2 - 2 / 2;
c) x = (2 * 8 * (2 + (8 * 2 / (2))));
__________________________________________________________________________________________________________________________________________________________
2)Write another program. In this program you will be asking the user to enter two different numbers. Then, you will take those two numbers and subtract the second number from the first. You will then print out the result. Use unsigned ints.
__________________________________________________________________________________________________________________________________________________________
3)What, if anything, prints when each of the following C++ statements is performed? If nothing prints, then answer "nothing". Assume x = 5 and y = 2. (Don't program this. Just try to figure it out)
std::cout << "x=";
std::cout << y + y;
std::cin >> x >> y;
std::cout << y;
std::cout << "x = " << x;
// std::cout << "x + y = " << x + y;
std::cout << " ";
std::cout << x + y << " = " << y + x;
z = x + y;
Explanation / Answer
Question 1
Answers:
a) x = 5 + 4 * 9 / 2 - 1; will print 22
b) x = 2 % 2 + 2 * 2 - 2 / 2; willl print 3
c) x = (2 * 8 * (2 + (8 * 2 / (2)))); will print 160
Question 2
Answeer:
#include <iostream>
using namespace std;
int main()
{
unsigned int x, y;
cout<<"Enter x and y values: ";
cin >>x >> y;
cout<< "Diff: "<<(y-x)<<endl;
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Enter x and y values: 4 5
Diff: 1
Question 3
Answer:
std::cout << "x="; will print "x="
std::cout << y + y; will print 4
std::cin >> x >> y; it is reading the values. Nothing will print
std::cout << y; will print 2
std::cout << "x = " << x; will print "x= 2"
// std::cout << "x + y = " << x + y; nothing will print because it is commented.
std::cout << " "; will print newline
std::cout << x + y << " = " << y + x; will print 7 = 7
z = x + y; nothing will print because we are just calculating and assigning the value.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.