Homework 1
This homework is intended to test your understanding of basic input/output, the "if" statement, and mathematical primitives 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 generate answers if you so desire. There are no typos in the C++ statements that follow so if a statement has invalid syntax make sure you take it into account when you 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; 2 b) cout << x + x; 4 c) cout << x << x; 22 d) cout << "x"; x e) cout << "x = " << x; x = 2 f) cout << x + y << " = " << y + x; 5 = 5 g) cout << x + y << " != " << y + x; 5 != 5 h) z = x + y; nothing i) cin >> "x + y"; syntax error j) // cout << "x + y = " << x + y; nothing k) cerr << "x + y\n"; x + y (with a new line) (extra credit) l) cout << "x + y\n"; x + y (with a new line) m) cout >> "x + y = " >> x + y; syntax error n) cout << "x + y = " << endl; x + y = (with a new line) 2) W a) y = a*x*x*x+7; valid b) y=x* x *a* x+7; valid c) y = (x*x)*x*a + 7; valid d) y = (a*x * x*x)+7 strictly speaking there should be a ";" at the end it is a valid expression of ax^3 + 7 but not a valid statement. Most of you got it wrong but I didn't take off any points. e) y = (a+7)*x*x*x; invalid f) a*x*x*x+7 = y; invalid, syntax error g) y = a*(xxx) + 7; invalid, syntax error h) (a*x*x*x)+7 = y; invalid, syntax error i) (a+7)*x*x*x = y; invalid, syntax error j) y = (a*x*(3)) + 7; invalid 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; **/+-=, 60 b) x = 7 + 3 % 6 * 6 / 2 - 1; %*/+-=, 15 c) x = 7 + 3 * (6 * 6) / 2 - 1; **/+-=, 60 d) x = 7 + (14 % (2 * (6 / 2) - 1)); /*-%+=, 11 e) x = (7 + (3 * 6)) / ((6 / 2) - 1); *+/-/=, 12 f) x = 7 + 3 * 6 * 6 / 2 - 1 / 2; **//+-=, 61 g) x = 7 + 3 * 6 * 6 / 2 - (1 / 2); /**/+-=, 61 h) x = 7 + 3 / 2 * 6 * 6 / (2 - 1) / 2; -/**//+=, 25 i) x = 7 + 3*(4/2) * 6 (* 6 / 2 - 1); invalid syntax j) x = 7 + ((3*4)/2 * (6 - 7)) * (6 / (2 - 1)); *-/*-/*+=, -29 k) x = (7 + 3) * 6 % 4 * 6 / 2 - 1; +*%*/-=, -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. int x = 0, y = 0, z = 0, result = 0; b) Prompt the user to enter three integers. cout << "Enter three integers: "; c) Read three integers from the keyboard and store them in x, y, and z. cin >> x, y, z; d) Compute the product of the variables x, y, and z and store the result in the variable result. result = x*y*z; e) Print the value of result to standard output. cout << result; 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. For example: Enter three integers: 2 3 4 The sum is 9, the product is 24, the average is 3. // This program will compute the sum, product, and average of three integers #include <iostream> // for I/O
int main() { int x = 0, y = 0, z = 0;
// Get three integers from the user cout << "Enter three integers:" << endl; cin >> x, y, z;
// Print sum, product and average. cout << "The sum is " << x+y+z << ", the product is " << x*y*z << ", the average is " << (x+y+z)/3.0 << "." << endl;
return 0; } 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." For example: Enter two integers: 4 7 7 is larger than 4 // This program will print the larger of two integers #include <iostream>
int main() { int x = 0, y = 0;
// Get two integers from the user cout << "Enter two integers: " << endl; cin >> x, y;
// tell the user which integer is larger
if ( x > y) { cout << x << " is larger than " << y << endl; }
if ( x < y ) { cout << y << " is larger than " << x << endl; }
if ( x == y ) { cout << "The two integers entered are equal" << endl; }
return 0; } |
7) What is the difference between an algorithm and a computer program?
A computer program is an algorithm that a computer can follow. Not all algorithms are computer programs but all computer programs are algorithms.