%% for large print, add \huge {\raggedleft \bf CPS 6 \hfill Exam 1 %S Solutions \hfill Fall 1994} {\raggedleft \bf \hfill Dr. Rodger \hfill } \vspace{0.2in} NAME: \underbar{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ } \vspace{0.2in} DO NOT SPEND MORE THAN 10 MINUTES ON ANY QUESTION! \vspace{0.2in} Before starting, make sure your test contains ten pages. \vspace{0.2in} The last page is blank and can be used as scratch paper. None of the C++ programs or program segments should have syntax errors. If you think there is a syntax error, then ask. Include statements may not always be shown. \vfill\eject \begin{enumerate} \item (20 pts) Answer 5 of the questions on this page. Put a large X through the one question you do not want graded. If you do not do this, then the first 5 questions will be graded. \begin{enumerate} \item With respect to C++, what does OOP stand for? %S {\bf Object-Oriented Programming} \vfill \item Suppose you compile a C++ program on an IBM workstation and it works correctly. If you copy the program and corresponding executable file to a DECstation, your program won't execute until you compile it again. Why? %S {\bf When you compile a program, it gets converted into machine %S language for that particular type of machine. If you move to %S a different type of machine, you have to recompile because the %S DEC has a different machine language than the IBM.} \vfill \item Xxgdb is a useful tool for find a missing semicolon in a C++ program. (TRUE or FALSE?) Explain. %S {\bf False, Xxgdb is a tool that is applied to an executable file. If %S a semicolon is missing, the executable file is not created. } \vfill \item Emacs is which of the following: a compiler, an editor, or a debugger? %S {\bf editor} \vfill \item When is a function prototype required? %S {\bf A function prototype is required if the call to a function comes %S before the definition of the function.} \vfill \item What is a constructor? %S {\bf A constructor is a function of a class (with the same name). It %S is used to initialize the data fields of the class. %S } \vfill\eject \end{enumerate} %S \vfill\eject \item (14 pts) The following program compiles with no syntax errors. \begin{verbatim} int testit(int a, int b) { b = b + 3; if (a > b) return b; else if (a + 5 > b) return a; return a + b; } main() { int a, b, c; a = 6; b = 2; c = 4; cout << a+b*c << " " << a/c+2 << endl; cout << (a==4) << " " << a*c%5 << endl; cout << "CPS" << "6" << "is" << endl << "fun" << endl; a = 6; b = 2; c = 4; cout << testit(a,b) << " " << testit(b,a) << endl; } \end{verbatim} What is the output of this program? Denote a blank by $\sqcup$ \vfill\eject %S {\bf %S \begin{tabular}{l} %S 14$\sqcup$3 \\ %S 0$\sqcup$4 \\ %S CPS6is \\ %S fun \\ %S 5$\sqcup$11 \\ %S \end{tabular} %S } %S \vfill\eject \item (8 pts) Consider the following program segment. \begin{verbatim} String animal, sound; cout << "Enter an animal: "; cin >> animal; cout << "Enter a sound: "; cin >> sound; \end{verbatim} Rewrite this segment so that there is only one cout statement and one cin statement. Your segment should accomplish the same goal as the segment above. %S {\bf %S:\begin{verbatim} %S: String animal, sound; %S: %S: cout << "Enter an animal and a sound: "; %S: cin >> animal >> sound; %S:\end{verbatim} %S } \vfill\eject %S \vfill\eject \item (10 pts) Suppose that you are to write a C++ code fragment that computes a value for x, depending on the values of n, y and z as follows. If n is between 2 (inclusive) and 6 (exclusive), then x is computed by multiplying y by 2. If n is between 8 (inclusive) and 13 (exclusive), x is computed by multiplying z by 4, and subtracting that quantity from y. Otherwise, x is not computed. This is shown in the figure below. \begin{figure}[h] \begin{center} \input{tst1line} \end{center} %\caption{} \end{figure} For each of the following code fragments, indicate whether or not the code fragment will properly compute the value for x. If the code fragment does not work properly, explain why it fails. Assume that x, y, z, and n are all declared as double. (TIP: This may be tricky. Indentation may not be correct.) \begin{enumerate} \item \begin{verbatim} if ((n >= 2.0) && (n < 13.0)) if (n < 6.0) x = 2 * y; if (n >= 8.0) x = y - 4 * z; \end{verbatim} %S {\bf Incorrect. This modifies x for n greater than or %S equal to 13.0, since the third if is always executed. %S } \vfill \item \begin{verbatim} if (n >= 8.0) { if (n < 13.0) x = y - 4 * z; else if (n >= 2.0) if (n < 6.0) x = 2 * y; } \end{verbatim} %S {\bf Incorrect. This does not modify x when n is between 2 and 6. %S } \end{enumerate} \vfill\eject %S \vfill\eject \item (12 pts) The following program compiles with no syntax errors. \begin{verbatim} double sample(double & bug, double tst) { double num; num = 4; tst = 5.1; bug = 1.7; return bug + num; } main() { double num, bug, tst; num = 4.5; bug = 2.3; tst = 3.6; cout << sample(bug, tst) << endl; cout << "num= " << num << " bug= " << bug << endl; cout << "tst= " << tst << endl; num = 4.5; bug = 2.3; tst = 3.6; tst = sample(num, bug); cout << "num= " << num << " bug= " << bug << endl; cout << "tst= " << tst << endl; } \end{verbatim} What is the output of this program? %S {\bf %S \begin{tabular}{l} %S 5.7 \\ %S num= 4.5 bug= 1.7 \\ %S tst= 3.6 \\ %S num= 1.7 bug= 2.3 \\ %S tst= 5.7 \\ %S \end{tabular} %S } \vfill\eject %S \vfill\eject \item (10 pts) The following program calculates the volume of a rectangular swimming pool. Complete this program by filling in the main function. Do not modify the other functions. \begin{verbatim} void GetInfo(double & depth, double & length, double & width) // Ask the user to type in the dimensions of a pool { cout << "Enter the depth of the pool: "; cin >> depth; cout << "Enter the length of the pool: "; cin >> length; cout << "Enter the width of the pool: "; cin >> width; } double Volume(double depth, double length, double width) // Calculate the volume of a pool. { double vol; vol= depth*length*width; return vol; } main() { \end{verbatim} %S:\begin{verbatim} %S: %S: double dep, len, wid; %S: double vol; %S: %S: GetInfo(dep, len, wid); %S: vol = Volume(dep,len,wid); %S: cout << "Volume of pool is " << vol << endl; %S:\end{verbatim} \vfill \begin{verbatim} } \end{verbatim} Sample (and correct) output for this function: \begin{verbatim} Enter the depth of the pool: 9.6 Enter the length of the pool: 22.6 Enter the width of the pool: 15.0 Volume of pool is 3254.4 \end{verbatim} \eject %S \vfill\eject \item (12 pts) The following program reads in integers and calculates the maximum integer and the sum of the integers. This program assumes the user types Control-D to stop and that all numbers are greater than or equal to zero. \begin{verbatim} main() { int max, sum, num; cout << "Type in integers (CNTR-D to stop)" << endl; max=0; sum = 0; while (cin >> num) { sum = sum + num; if (num > max) max = num; } cout << "Sum of all numbers is " << sum << endl; cout << "Maximum number is " << max << endl; } \end{verbatim} {\bf Rewrite} this program to 1) allow negative numbers, and 2) for the user to type 0 to indicate that there is no more input. You can assume that there will be at least two valid integers typed in. For example, if the user were to type in: -10 -40 15 0 \ \ then the sum would be -35 and the max would be 15. \begin{verbatim} main() { int max, sum, num; cout << "Type in integers (0 to stop)" << endl; // Missing code here \end{verbatim} \vfill %S:\begin{verbatim} %S: cin >> num; %S: max = num; %S: sum = 0; %S: while (num != 0) %S: { %S: if (num > max) %S: max = num; %S: sum = sum + num; %S: cin >> num; %S: } %S:\end{verbatim} \begin{verbatim} cout << "Sum of all numbers is " << sum << endl; cout << "Maximum number is " << max << endl; } \end{verbatim} %S \vfill\eject %S {\bf Alternative Solution} %S:\begin{verbatim} %S:main() %S:{ %S: int max, sum, num; %S: cout << "Type in integers (0 to stop)" << endl; %S: // Missing code here %S: %S: cin >> num; %S: max = num; %S: sum = num; %S: while ((cin >> num) && (num != 0)) %S: { %S: if (num > max) %S: max = num; %S: sum = sum + num; %S: } %S: %S: cout << "Sum of all numbers is " << sum << endl; %S: cout << "Maximum number is " << max << endl; %S:} %S:\end{verbatim} \eject %S \vfill\eject \item (14 pts) Complete the program below by writing a function called CountSides. This function rolls a six-sided die a specified number of times, and returns the number of times a specified side of the die is rolled. (Sample output is on the Exam 1 Handout). \begin{verbatim} // Fill in the function CountSides here ___________ CountSides(____________________________________________________) { \end{verbatim} \vfill %S: \begin{verbatim} %S: int CountSides(int side, int numrolls) %S: { %S: Dice die(6); %S: int sidecnt = 0; %S: %S: while (numrolls > 0) { %S: if (die.Roll() == side) %S: sidecnt++; %S: numrolls--; %S: } %S: return sidecnt; %S: \end{verbatim} \begin{verbatim} } main() { int side; // a specified side of the die int loopcnt; // number of times to roll the die cout << "Enter side to count: "; cin >> side; cout << "Enter number of times to roll: "; cin >> loopcnt; cout << "The dice is rolled " << loopcnt << " times." << endl; cout << "The side " << side << " appeared " << CountSides(side, loopcnt) << " times." << endl; } \end{verbatim} \eject \end{enumerate} (extra page) %S \vfill\eject \vfill\eject