\documentstyle[boxoutput,epsf,cprog]{article} \input{/u/ola/lib/wide} \input{/u/ola/lib/para} \input{/u/ola/lib/macros} \input{/u/ola/lib/cps103} \title{Test 2: CPS 08} \author{Owen Astrachan} \date{November 18, 1994} \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 & 6 pts. &\ \\ & & \\ \hline\hline Problem 2 & 6 pts. &\ \\ & & \\ \hline\hline Problem 3 & 18 pts. &\ \\ & & \\ \hline\hline Problem 4 & 12 pts. &\ \\ & & \\ \hline\hline Problem 5 & 10 pts. &\ \\ & & \\ \hline \hline Problem 6 & 12 pts. &\ \\ & & \\ \hline \hline extra: & 8 pts. &\ \\ & & \\ \hline \hline TOTAL: & 64 pts. &\ \\ & & \\ \hline\hline \end{tabular} %\normalsize \end{table} } \clearpage \problem{Vocabulary: {\em 6 points}} For each of the words/phrases below, circle the definition that is the best description as it pertains in the context of computer science, programming, and C/C++. \begin{enumerate} \item {\em aliasing/shadowing} \begin{enumerate} \item using the same identifier in a nested scope ``hiding'' the identifier in the outer scope. \item providing default values for parameters, e.g., as was done in the \verb!ClockTime! class for 0 hours and 0 minutes and 0 seconds. \item using a reference parameter to avoid the overhead of making a copy of a value being passed. \end{enumerate} \item {\em static} \begin{enumerate} \item used as a type modifier --- extends the lifetime of a local variable so that its value persists over multiple function calls \item used as a parameter modifier --- prevents the value of the parameter from changing within a function \item used as a stream modifier --- prevents the stream from ``flowing'', i.e., stops values from being read \end{enumerate} \item {\em ASCII} \begin{enumerate} \item a library function in \verb!! for converting numbers to characters \item a standard coding for characters (each char has an underlying numerical value) \item something requested from an attendant at a BP service-station \end{enumerate} \end{enumerate} \problem{To Sir: With Name {\em 6 points}} What is printed by the \verb!cout << ...! statement below? Supply meaningful pre/postconditions for the function \verb!Mystery!. {\small\begin{verbatim} void Mystery(String & name); String nom = "Bjarne Stroustrup"; Mystery(nom); cout << nom << endl; void Mystery(String & name) { String first,second; istrstream temp(name); first.GetLine(temp,' '); second.GetLine(temp); name = second + "," + first; } \end{verbatim}} \clearpage \problem{I/O and Away {\em 18 points}} The program below prints six lines of output. Indicate each line of output in the space below to the right. The input to the program is shown below on the left. \vspace*{.1in} \cprogfile{iostuff.cc} \begin{minipage}{2.25in} \begin{center} \em input \end{center} \obeylines \small\setlength{\parskip}{0pt} A big dog chased the bubble. A big dog chased the bubble. A big dog chased the bubble. A big dog chased the bubble. A big dog chased the bubble. A big dog chased the bubble. \end{minipage} \ \begin{minipage}{3.5in} {\hsize=3.5in \begin{boxoutput} {\leftskip=.2in \small\setlength{\parskip}{0pt} \obeylines \verb@@ \verb@@ \verb@@ \verb@@ \verb@@ \verb@@ \verb@@ \verb@@ \verb@@ \verb@@ \verb@@ \verb@@ \verb@@ \verb@@ } \end{boxoutput} } \end{minipage} \clearpage \problem{Window {\em 12 points}} The number of meters a bicycle travels every 10 seconds is recorded in a file. As an example, the data below {\small\begin{verbatim} 20 25 30 25 20 10 10 5 5 \end{verbatim}} indicates that the bicycle travels $20 + 25 + 30 + 25 + 20 + 10 = 130$ meters in the first minute (60 seconds). The average speed is then 130 meters/min. After 20 more seconds (and 15 more meters) the average speed over the most recent minute will be 100 meters/min since the distance travelled in that time is $30 + 25 + 20 + 10 + 10 + 5 = 100$. You are to complete the program below so that it reads data from the file specified by the user and, for each reading after the first 6 (the first minute), prints the average speed in meters per minute for the most ``recent'' minute. For the data file above the output would be \verb!130 120 100 75!. {\leftskip=.2in\small \cprogfile{smooth.cc} \par} \clearpage \problem{Debugging {\em 10 points}} {\bf part A} (4 points) The function \verb!atoi! in \verb!! converts a string to a number. The function \verb!itoa! below is intended to convert a number to a string. Although \verb!itoa(6)! returns the string \verb!"6"! and \verb!itoa(666)! returns the string \verb!"666"!, the call \verb!itoa(1234)! returns the string \verb!"4321"!. Fix \verb!itoa! so that it works as intended by changing exactly one line in the function. {\small\begin{verbatim} String itoa(int num) // precondition: num > 0 // postcondition: returns string form of num { String val = ""; while (num != 0){ val = val + char('0' + num % 10); num /= 10; } return val; } \end{verbatim}} {\bf part B} (6 points) A function \verb!NoDupes! designed to remove consecutive, duplicate elements from a sorted array is shown below. The picture below is intended to describe how the function works. {\leavevmode \epsfxsize=.30\linewidth \centerline{\epsfbox{nodupes.eps}} } The example below shows how the function should work on an array that initially contains 7 elements. {\bf\em Add or alter at most three lines of code below so that \verb!NoDupes! works as intended.} \begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|} \multicolumn{7}{c}{\em before} & \multicolumn{1}{c}{\ } & \multicolumn{7}{c}{\em after} \\ \cline{1-7} \cline{9-15} 2 & 2 & 3 & 4 & 4 & 4 & 5 & \multicolumn{1}{c|}{\hspace*{1in}} & 2 & 3 & 4 & 5 & & & \\ \cline{1-7} \cline{9-15} \multicolumn{1}{c}{\tiny 0} & \multicolumn{1}{c}{\tiny 1} & \multicolumn{1}{c}{\tiny 2} & \multicolumn{1}{c}{\tiny 3} & \multicolumn{1}{c}{\tiny 4} & \multicolumn{1}{c}{\tiny 5} & \multicolumn{1}{c}{\tiny 6} & \multicolumn{1}{c}{\ } & \multicolumn{1}{c}{\tiny 0} & \multicolumn{1}{c}{\tiny 1} & \multicolumn{1}{c}{\tiny 2} & \multicolumn{1}{c}{\tiny 3} & \multicolumn{1}{c}{\tiny 4} & \multicolumn{1}{c}{\tiny 5} & \multicolumn{1}{c}{\tiny 6} \\ \end{tabular} {\small\begin{verbatim} void NoDupes(int a[], int & numElts) //precondition: a is sorted: a[0] <= a[1] <= ... <= a[numElts - 1] //postcondition: duplicates in a removed, a still sorted { int lastNonDup = 0; // location of last non-duplicate int k; for(k=1; k < numElts; k++){ if (a[k] != a[lastNonDup]){ a[lastNonDup] = a[k]; // not a duplicate, store it } } numElts = ; // potentially fewer elts, update } \end{verbatim}} \clearpage \problem{Kroger {\em 12 points}} {\bf part A} (6 points) A \verb!struct! named \verb!Grocery! can store information for items in a shopping cart. Given the definitions below, write the function \verb!CartWorth! that determines how much all the items in \verb!cart! cost together. {\small\begin{verbatim} enum Kind{heavy,squishy,frozen}; struct Grocery { String brandName; double price; Kind type; }; double CartWorth(const Grocery cart[], int numItems) // precondition: numItems = # of grocery items in cart // postcondition: returns total cost of all items in cart { } \end{verbatim}} {\bf part B:} (6 points) Assume information is stored in a file as shown below in the format \begin{center} {\em brand name}:{\em price}:{\em universal product code} \end{center} {\small\begin{verbatim} Froot Loops:2.97:780079-116918 Skippy (Crunchy):3.15:1201-3939 Haagan Das (Vanilla):1.99:23913-19383 Wonder Bread:0.99:91938-39393819 \end{verbatim}} (a universal product code, or upc, is what's scanned at the checkout as shown) \vspace*{-1in} \hfill %{\leavevmode \epsfxsize=.17\linewidth \epsfbox{upc.eps} %} \vspace*{.2in} Write a function \verb!ReadCart! to read information from such a file into an array of groceries. A stream bound to a file is passed to the function \verb!ReadCart!. Assume that there's a function \verb!StringToKind! you may call that converts a upc to a \verb!Kind!. {\small\begin{verbatim} Kind StringToKind(String upCode) // precondition: upCode is a valid universal product code // postcondition: returns a Kind : heavy, squishy, frozen \end{verbatim}} \clearpage \begin{verbatim} void ReadCart(Grocery cart[], int & numItems, istream & input) \end{verbatim} \vspace*{3in} {\bf EXTRA CREDIT}: (8 points) Write a function \verb!ReArrange! that re-arranges the items in a grocery cart so that all the heavy ones come before all the frozen ones which come before all the squishy ones: {\leavevmode \epsfxsize=.30\linewidth \centerline{\epsfbox{dutchflag.eps}} } You can assume that any useful \verb!Swap! function exists. As a hint, keep the order shown and a ``needs to be processed'' part at the right end of the array. Initially the entire array needs to be processed. If the item being processed is frozen, it's in the right section --- otherwise some swapping is needed. \end{document} \item {\em istrstream} \begin{enumerate} \item an input stream bound to a file specified by a string's value. \item an input stream bound to a string. \item the class of which \verb!cin! is an instance. \end{enumerate}