CS 102 * * * Sections 2–5 * * * Spring 2010 Exam I — KEY (corrected) Name: KEY Section: Use the back for scratch paper if you need it. Make sure your final answer is clear to us (put a circle or box around it). If we can’t understand your answer, it’s wrong! No calculators, cell phones, or other electronic devices in sight! 1. (8 points) Answer the following questions: a) What does N++ mean? Ans: Increment N, i.e., N = N+1; b) The following if-statement will probably not do what the programmer expects. What’s wrong with it? if (X = 0) { cout << Y << endl; } Ans: “X = 0” is an assignment statement, not an equality test ( X == 0), which is probably what the programmer intended. c) What library would you include if you wanted to use the sin, cos, and tan functions? Ans: cmath (math.h is also OK) d) What library would you include if you wanted to use pseudo-random numbers? Ans: cstdlib (stdlib.h is also OK) — page 1 of 9 — 2. (8 points) What is printed by each of the following statements: a) cout << 5.0+5 / 2 << endl; Ans: 7 (7.0 is also acceptable) b) cout << 1.0 / 5 + 2 << endl; Ans: 2.2 c) string N = "2"; cout << "1.0 / 5" + N << endl; Ans: 1.0 / 52 d) cout<< (double) 1/4 <<endl; //casting has precedence over div. Ans: 0.25 3. (5 points) Rewrite the following as a functionally equivalent while-loop: for (int N = 0; N < 5; N++) { robot.motors(.5,.5); degreeTurn(72); } Ans: int N = 0; while (N < 5) { robot.motors(.5,.5); degreeTurn(72); N++; } — page 2 of 9 — 4. (5 points) If A , B, and C are bool variables, then ((A && B) || C) is equivalent to which of the following expressions? (Circle your answer or otherwise indicate it clearly.) a) (A && (B || C)) b) ((A || C) && (B || C)) c) ((A && C) || (B && C)) d) none of the above Ans: (b) 5. (5 points) Consider the following code segment: int N; cin >> N; while (N > 1) { cout << N << "˽"; // “˽” represents a blank N = N / 4; } If the user types 55 as input, what will it print? Ans: 55˽13˽3˽ 6. (5 points) Suppose there is a felt-tip pen in the pen port of the Scribbler. Show with a drawing and describe in words what the robot draws when we execute this code: for (int side = 1; side <= 5; side++) { robot.forward(1,2); robot.turnRight(1,0.5); // assume this turns 30 degrees } Ans: It will draw five sides of a dodecagon (twelve-sided regular polygon). — page 3 of 9 — 7. (10 points) What is the output from each code segment below? a) int x = 1, y = 2; if (x > 0 && y < 0) { x = y = 3; } cout << x << "˽" << y << endl; Ans: 1˽2 b) int x = 1, y = 2; if (x > 0 || y < 0) { x = y = 3; } cout << x << "˽" << y << endl; Ans: 3˽3 c) int x = 1, y = 2; if (x >= 1) { if (y < 2) { y++; } else { y--; } } cout << x << "˽" << y << endl; Ans: 1˽1 — page 4 of 9 — d) int x = 1, y = 2; if (x >= 1) { if (y < 2) { y++; } } else { y--; } cout << x << "˽" << y << endl; Ans: 1˽2 e) In part (d) above, under what condition(s) would y be incremented? Ans: It would be incremented if x were greater than or equal to 1 and y were less than 2. 8. (5 points) Suppose that double temp holds the current average annual temperature, that double rate (>1) holds the average annual rate of temperature increase, and that int year holds the current year. That is, after one year, the temperature will be temp * rate, and so on for each year. Write a whileloop to find the first year for which the average annual temperature is at least 3º above the current temperature. That is, after the while-loop, the variable year should hold the first year for which the temperature is >= temp + 3. Ans: // Typical answer; there are many ways to code this double newTemp = temp; while (newTemp < temp + 3) { newTemp = newTemp * rate; year++; } — page 5 of 9 — 9. (4 points) If you get an error message saying that you have attempted to divide by zero, what kind of error is this? (Circle your answer or mark it clearly in some other way.) a) compile-time error b) run-time error c) logic error Ans: (b) 10. (5 points) Describe the behavior of this Braitenberg vehicle: The vehicle accelerates towards light to the left or right in front of it. See LCR, p. 146–7. 11. (5 points) In the game “Paper, Scissors, Rock!” the rule is “ paper beats rock, scissors beats paper, and rock beats scissors,” otherwise the game is a draw. Complete the following definition of a function beats (X, Y) that returns true if X beats Y. The parameters X and Y are strings, either "paper", "scissors" or "rock". To receive full credit, your function should have a single exit point. bool beats (string myChoice, string yourChoice) { // Put your code here: return ( (myChoice == "paper" && yourChoice == "rock") || (myChoice == "scissors" && yourChoice == "paper") || (myChoice == "rock" && yourChoice == "scissors") ); } There are many other ways to do this. — page 6 of 9 — 12. (5 points) State in your own words the Law of uphill analysis and downhill invention. See LCR, p. 140. 13. (5 points) Complete the definition of the following function that counts the number of blank characters in a string. int function (string S) { // Put your code here: int count = 0; for (int loc = 0; loc < S.size(); loc++) { if (S[loc] == "˽") { count++; } } return count; // } There are are many equivalent ways to write this. — page 7 of 9 — 14. (5 points) Write a C++ main() function that reads int numbers from a file called “indata.dat” until it reaches the end-of-file, and then prints the numbers on the console, one per line, in reverse order. Make sure to show any required #includes. #include <iostream> #include <fstream> int main () { ifstream infile ("indata.dat"); vector<int> data; while (!infile.eof()) { int datum; infile >> datum; data.push_back(datum); } infile.close(); // not required, but good form for (int i = data.size()-1; i >= 0; i--) { cout << data[i] << endl; } return 0; } There are other ways to do this. — page 8 of 9 — 15. (5 points) Examine the following code segment and show the output for each of the specified inputs. string a; int b; cin >> a >> b; if (cin.good()) cout << b - 2 << endl; else cout << a + "2" << endl; (a) Input: abc 123 Ans: 121 (b) Input: 123 abc Ans: 1232 16. (5 points) Extra Credit! Consider the following definition of a recursive function: void function plumb (int x) { if (x == 0) { cout << "bottom"; } else { cout << "down˽"; plumb (x – 1); cout << "˽up"; } } What gets printed if the main function calls plumb(3)? (Pay attention to space characters, “˽”.) Ans: down down down bottom up up up Give them partial credit for minor errors. — page 9 of 9 —