This homework is intended to test your understanding of basic input/output, mathematical primitives, and the "if" statement in C++.
Complete the following exercises and turn them in via email by midnight on Monday .
I have software to read most document formats but postscript or PDF is preferred. For this assignment your answers (including the short programs) may be entered into the body of your email.
You are welcome to use the g++ compiler to solve the exercises instead of doing them by hand.
There are no typos in the C++ statements that follow so if a statement has invalid syntax make sure you say so in your answer.
1) What, if anything, does each of the following C++ statements print? Assume int x = 2; and int y = 3; and that the statements are independent (i.e. variable changes in one statement do not carry over to the next statement).
a) cout << x;
b) cout << x + x;
c) cout << x << x;
d) cout << "x";
e) cout << "x = " << x;
f) cout << x + y << " = " << y + x;
g) cout << x + y << " != " << y + x;
h) z = x + y;
i) cin >> "x + y";
j) // cout << "x + y = " << x + y;
k) cerr << "x + y\n";
l) cout << "x + y\n";
m) cout >> "x + y = " >> x + y;
n) cout << "x + y = " << endl;
a) y = a*x*x*x+7;
b) y=x* x *a* x+7;
c) y = (x*x)*x*a + 7;
d) y = (a*x * x*x)+7
e) y = (a+7)*x*x*x;
f) a*x*x*x+7 = y;
g) y = a*(xxx) + 7;
h) (a*x*x*x)+7 = y;
i) (a+7)*x*x*x = y;
j) y = (a*x*(3)) + 7;
3) State the order of evaluation of the operators in the following statements. List the operators to indicate the sequence of evaluation, i.e. **/+- is in the correct form for an answer to part a. Also give the value of x after the statement completes. Assume that the statements are independent.
Remember that the numbers in the following exercises are integers not floating point numbers (reals).
a) x = 7 + 3 * 6 * 6 / 2 - 1;
b) x = 7 + 3 % 6 * 6 / 2 - 1;
c) x = 7 + 3 * (6 * 6) / 2 - 1;
d) x = 7 + (14 % (2 * (6 / 2) - 1));
e) x = (7 + (3 * 6)) / ((6 / 2) - 1);
f) x = 7 + 3 * 6 * 6 / 2 - 1 / 2;
g) x = 7 + 3 * 6 * 6 / 2 - (1 / 2);
h) x = 7 + 3 / 2 * 6 * 6 / (2 - 1) / 2;
i) x = 7 + 3*(4/2) * 6 (* 6 / 2 - 1);
j) x = 7 + ((3*4)/2 * (6 - 7)) * (6 / (2 - 1));
k) x = (7 + 3) * 6 % 4 * 6 / 2 - 1;
4) Write a single C++ statement to accomplish each of the following. (Do not write a complete program for each).
a) Declare the variables x, y, z, and result to be integers.
b) Prompt the user to enter three integers.
c) Read three integers from the keyboard and store them in x, y, and z.
d) Compute the product of the variables x, y, and z and store the result in the variable result.
e) Print the value of result to standard output.
5) Write a program which asks the user to enter 3 integers; obtains three integers from the user; prints the sum, product, and average of those integers; and then exits. Remember to include a comment at the start of your source file describing the program.
Enter three integers:
The sum is 9, the product is 24, the average is 3.
6) Write a program which asks the user to enter two integers, obtains two integers from the user, prints the larger of the two integers, and exits. If the two integers are equal the program prints "The two integers entered are equal."
Enter two integers:
7 is larger than 4
7) What is the difference between an algorithm and a computer program?