\documentstyle[boxoutput]{article} \input{/u/ola/lib/wide} \input{/u/ola/lib/para} \input{/u/ola/lib/macros} \title{Test 1: CPS 06} \author{Owen Astrachan \and Susan Rodger} \date{October 2, 1995} \begin{document} \maketitle Name: \underline{\hspace*{3.5in}} \vspace*{.1in} Honor code acknowledgement (signature) \underline{\hspace*{3in}} \vspace*{.25in} {%\doublespace %\large \begin{table}[h] \centering \begin{tabular}{||l|r|c||} \hline\hline & value & grade \\ \hline\hline Problem 1 & 12 pts. &\ \\ & & \\ \hline\hline Problem 2 & 10 pts. &\ \\ & & \\ \hline\hline Problem 3 & 6 pts. &\ \\ & & \\ \hline\hline Problem 4 & 10 pts. &\ \\ & & \\ \hline\hline Problem 5 & 10 pts. &\ \\ & & \\ \hline \hline Problem 6 & 10 pts. &\ \\ & & \\ \hline \hline Problem 7 & 12 pts. &\ \\ & & \\ \hline \hline Extra & 6 pts. &\ \\ & & \\ \hline \hline TOTAL: & 70 pts. &\ \\ & & \\ \hline\hline \end{tabular} %\normalsize \end{table} } \hspace*{.5in} This test has 10 pages, be sure your test has them all. Do NOT spend too much time on one question --- remember that this class lasts 75 minutes. \clearpage \problem{Output: {\em 12 points}} Indicate the output of each of the {\tt cout} statements below. Assume each statement is part of a program that compiles and runs. You can show your reasoning for partial credit. {\small\begin{verbatim} int count = 22; double value = 4.5; string word = "mars"; cout << 45 + 9/value << endl; cout << (count % 7) * 5 - 2 << endl; cout << word + "hall" << endl; cout << 2 * value + count/4 << endl; cout << word.length() << endl; cout << word < "venus" << endl; \end{verbatim}} \clearpage \problem{Heeeere's A Million Dollars: {\em 10 points}} You have been hired to write a program that will print a series of form letters to people. The two function calls below should generate the output shown. \begin{verbatim} main() { FormLetter("Bartholomew", "Green","100 Main St.",38); FormLetter("Marcia", "Brady", "22 Elm Court",42); } \end{verbatim} \vspace*{\parskip} {\hsize=4.5in \begin{boxoutput} {\leftskip=.3in \small\setlength{\parskip}{0pt} \obeylines \verb@@ \verb@Dear Bartholomew,@ \verb@@ \verb@Now that you're 38 years old you need to provide @ \verb@for the Green family. Invest for the future, Bartholomew.@ \verb@Make life at 100 Main St. happy: give us money today!@ \verb@@ \verb@Dear Marcia,@ \verb@@ \verb@Now that you're 42 years old you need to provide @ \verb@for the Brady family. Invest for the future, Marcia.@ \verb@Make life at 22 Elm Court happy: give us money today!@ \verb@@ } \end{boxoutput} } Write the function {\tt FormLetter}. \clearpage \problem{Squash those bugs : {\em 6 points}} Consider the following program with lines numbered (\#includes are not shown). The intent of the program is to generate the output shown below. \vspace*{\parskip} {\hsize=4.5in \begin{boxoutput} {\leftskip=.3in \small\setlength{\parskip}{0pt} \obeylines \verb@@ \verb@Claire Smith@ \verb@Enter amount: @{\em 25.0} \verb@Square root function works!@ \verb@@ } \end{boxoutput} } \begin{verbatim} 5: void MakeName(string name) 6: { 7: string newname = name + " Smith"; 9: } 10: 11: main () 12: { 13: double amount; 14: string name = "Claire"; 15: cout << MakeName(name) << endl; 16: 17: cout << Enter amount; 18: cin >> amount; 19: 20: if (sqrt(amount) * sqrt(amount) = amount) 21: { 22: cout << "Square root function works!" << endl; 23: } 24: } \end{verbatim} When this program is compiled, there are three errors, one on line 15, one on line 17, and one on line 20. The following compiler message appears. \begin{verbatim} buggy.cc: In function `int main()': buggy.cc:15: void value not ignored as it ought to be buggy.cc:15: no match for `operator <<(class _IO_ostream_withassign, void)' buggy.cc:17: `Enter' undeclared (first use this function) buggy.cc:17: (Each undeclared identifier is reported only once buggy.cc:17: for each function it appears in.) buggy.cc:17: parse error before `;' buggy.cc:20: non-lvalue in assignment *** Error code 1 make: Fatal error: Command failed for target `buggy' \end{verbatim} Explain what the three compiler errors are in this program, and how they should be fixed. \clearpage \problem{Triangle Cornucopia : {\em 10 points}} A triangle is, by definition, an {\em abundant} triangle if its perimeter is larger than its area. Write the function {\em IsAbundant} whose header is given below. {\em IsAbundant} returns true if the triangle with sides whose lengths are given by parameters {\tt a}, {\tt b}, and {\tt c} is abundant and returns false otherwise. You can use Heron's formula (2nd C. BC) to determine the area of a triangle. If $s$ is half the perimeter, i.e., \begin{displaymath} s = \frac{1}{2}(a + b + c) \end{displaymath} then the area of a triangle is given by the formula \begin{displaymath} \mbox{\rm area} = \sqrt{s \cdot (s - a) \cdot (s - b) \cdot (s - c)} \end{displaymath} For example, a triangle with sides 3, 4, and 5 has perimeter 12 and area $\sqrt{6 \cdot 3 \cdot 2 \cdot 1} = 6$, so is abundant. An example of use: \begin{verbatim} if (IsAbundant(3,4,5)) { cout << "triangle is abundant" << endl; } else { cout << "triangle is NOT abundant" << endl; } \end{verbatim} Complete {\em IsAbundant} below the following header. \begin{verbatim} bool IsAbundant(double a, double b, double c) // precondition: a, b, c are lengths of sides of a triangle // postcondition: returns true if triangle is abundant, otherwise // returns false \end{verbatim} \clearpage \problem{The Third Time is Jail Time : {\em 10 points}} In a board game, two six-sided dice are thrown to determine how many positions to move. Rolling doubles, or the same number twice, lets the player roll again. For example, rolling 6 and 3 yields a 9 position move. Rolling 2 and 2, followed by 3 and 5 yields a 12 position move (2 + 2 + 3 + 5) where the player rolls again because of rolling double 2's. Rolling 3 and 3, followed by 4 and 4, followed by 1 and 6 yields a 21 position move (3 + 3 + 4 + 4 + 1 + 6). There is no limit on the number of times doubles can be rolled, each time doubles are rolled the sum of the dice increases the number of positions to move and the dice are rolled again. Write the function {\em TotalMoves} whose header is given below. {\em TotalMoves} returns the total number of positions to move by rolling two six-sided dice according to the rules described above. \begin{verbatim} int TotalMoves() // postcondition: returns sum of rolling two six-sided dice // where dice are rolled until doubles are NOT thrown \end{verbatim} \clearpage \problem{Number to rebmun : {\em 10 points}} Note that if {\tt num} has the value 12, then {\tt num*10 + 3} has the value {\tt 123}. Write the function {\em Reverse} whose header is given below. {\em Reverse} returns a number whose digits are in the reverse order of the parameter {\tt val}. For example: \begin{verbatim} cout << Reverse(1235) << endl; cout << Reverse(3) << endl; cout << Reverse(90120) << endl; \end{verbatim} should generate the output below (note that trailing zeros are lost when the digits are reversed). \vspace*{\parskip} {\hsize=4.5in \begin{boxoutput} {\leftskip=.3in \small\setlength{\parskip}{0pt} \obeylines \verb@@ \verb@5321@ \verb@3@ \verb@2109@ \verb@@ } \end{boxoutput} } \begin{verbatim} int Reverse(int val) //precondition: 0 < val //postcondition: returns number whose digits are reverse of val's digits \end{verbatim} \clearpage \problem{Social Summa :{\em 12 points}} A class {\tt DukeStudent} is designed to facilitate writing programs to access data about students at Duke. A {\tt DukeStudent} variable provides access to information about a student after the {\tt Init} member function is called. {\small\begin{verbatim} DukeStudent student; student.Init("213-91-1897"); if (student.Name() != "noname") // 213-91-1897 is valid SSN { cout << student.Name() << endl; cout << "GPA = " << student.GPA() << endl; } \end{verbatim}} generates the output below. {\small\begin{verbatim} Pat Lelapin GPA = 3.678 \end{verbatim}} Part of the header file {\tt dukestudent.h} is shown below. {\small\begin{verbatim} // DukeStudent class // written 9/28/95 // // variable/object represents a student by using the // social security number with the Init() member function // // once initialized via Init(), information about the student // can be accessed via member functions. The Flex account // balanced can be changed as well using the function AddFlex() // // if not properly initialized, member functions may return // garbage values, but Name() will return the string "no name" // for variables that are not initialized or initialized with // an illegal social security number // // DukeStudent() -- construct student object // (NOT a valid student until Init() is called). // // void Init(string ssn) -- initialize to student with social security # // specified by ssn (if invalid ssn then all // other functions return garbage) // // double GPA() -- returns the GPA of a student // // double Flex() -- Flex balance (in dollars and cents as a double) // // string Name() -- returns name of student "First Last", e.g., // "Jane Doe", returns "no name" if NOT valid student // // string Login() -- returns login id, e.g., "jdoe","ola","rodger" // // void AddToFlex(int amount) -- add amount to Flex account // amount can be negative to remove money class DukeStudent { public: DukeStudent(); // constructor void Init(string ssn); // initialize to social security # ssn double GPA(); // return GPA, e.g., 3.75 double Flex(); // return flex balance, e.g., 400.32 string Name(); // return "first last", name of student string Login(); // return login id void AddToFlex(int amount); // add amount (can be < 0) to flex private: // can't see this part }; \end{verbatim}} {\bf Part A} A Duke student has social security number 054-53-1295. Write a code fragment to print this student's name, login id, and Flex balance. \vspace*{2in} {\bf Part B} A data file named {\tt flexspend.dat} contains a social security number on the first line, followed by flex account purchases, one per line. For example, the file belows shows four purchases of \$1.25, \$25.75, \$4.95, and \$8.05 \begin{verbatim} 054-53-1295 1.25 25.75 4.95 8.05 \end{verbatim} You are to complete a program that, if run, will generate output as shown below. \vspace*{\parskip} {\hsize=4.5in \begin{boxoutput} {\leftskip=.3in \small\setlength{\parskip}{0pt} \obeylines \verb@@ \verb@Enter name of data file:@ {\em flexspend.dat} \verb@name is Terry Platt@ \verb@initial Flex balance = $410@ \verb@after expenses balance = $370@ \verb@@ } \end{boxoutput} } Complete the program below so that it will process the data file, subtracting the purchases from a student's Flex account. Assume that the data file exists, you do not need to check for this. \clearpage \begin{verbatim} #include #include // for class ifstream #include "dukestudent.h" // for class DukeStudent main() { DukeStudent student; \end{verbatim} \vfill \begin{verbatim} } \end{verbatim} \hfill ({\em over for extra credit}) \clearpage \problem{Extra Credit: Born to Run ({\em 6 points})} \begin{center} \bf This problem is NOT required. \end{center} If a die is rolled several times, a {\em run} is a sequence of consecutive rolls showing the same number. For example, a twelve-sided die might roll $1 5 5 6 8 2 3 9 9 11 4 12 4 2 2 2 2 8$. There is a run of two 5's, a run of two 9's and a run of four 2's. Write a program that simulates the rolling of a twelve-sided die one million times and that outputs the length of the longest run and what the value rolled for the run is. For the sequence above, the longest run is 4, and the value rolled for that run is 2. \end{document}