Test 1: CPS 53 February 9, 1994 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 & 8 pts. &\ \\ & & \\ \hline\hline Problem 3 & 10 pts. &\ \\ & & \\ \hline\hline Problem 4 & 10 pts. &\ \\ & & \\ \hline\hline Problem 5 & 10 pts. &\ \\ & & \\ \hline \hline Problem 6 & 10 pts. &\ \\ & & \\ \hline \hline Problem 7 & 6 pts. &\ \\ {\em extra} & & \\ \hline \hline TOTAL: & 60 pts. &\ \\ & & \\ \hline\hline \end{tabular} %\normalsize \end{table} } \clearpage \problem{Vocabulary: {\em 12 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 header file} \begin{enumerate} \item a file that must be compiled ahead of all other files before linking can occur when a Makefile is used. \item a file with declarations and prototypes used for importing needed information about classes and functions into a C or C++ program. \item a special kind of C++ class that uses polymorphism and inheritance to implement a building block to be used in many programs. \end{enumerate} \item {\em built-in types} \begin{enumerate} \item a type that the user builds from scratch to use in a program instead of relying on the system-defined type, e.g., using \verb!"String.h"! instead of \verb!!. \item A type that is part of the standard C or C++ language (e.g., int or double) as opposed to one defined by the user. \item A class whose methods are an integral part of the type (e.g., as Shock and Sense are part of the Heart class). \end{enumerate} \item {\em precondition} \begin{enumerate} \item steps taken to ensure that a program will execute correctly \item a statement that indicates what must be true in order for a function to work as intended \item a medical ailment suffered at birth (e.g., by the mathematician and logician Augustus de Morgan) \end{enumerate} \item {\em void function} \begin{enumerate} \item a function that does not return a value \item a function that is never executed during some particular run of a program \item the part of a Makefile that indicates what compiler will be used in generating .o files. \end{enumerate} \end{enumerate} \problem{Debug: {\em 8 points}} {\bf Part A} A function to return 1 if a string is in an array and 0 if the string is not is given below, it has an error in it. {\small\begin{verbatim} int Find(String list[], int numStrings, String key) // precondition: numStrings = # of strings in list // postcondition: returns 1 if key is in list, otherwise returns 0 { int k = 0; while (list[k] != key && k < numStrings){ k++; } if (k >= numStrings) return 0; else return 1; } \end{verbatim}} When run, the error message \begin{verbatim} illegal array access: occurred in function Find \end{verbatim} appears whenever a string is searched for that is NOT in the list and the list is full. Describe a {\em very simple} fix to this code that will remove the error. \vspace*{1in} {\bf Part B} Which statement group is more likely to cause trouble in a program \begin{verbatim} int num; cin >> num; double twice = 2*num; \end{verbatim} or the group \begin{verbatim} double num; cin >> num; int twice = 2*num; \end{verbatim} and why? \clearpage \problem{Average: {\em 10 points}} You are to complete a code segment that will permit the user to enter integers until a non-negative integer is entered. After the numbers are entered the average of the negative integers entered should be printed. (Assume that at least one negative number will be entered.) For example: {\small\setlength{\parskip}{0in} \setlength{\parindent}{0in} \obeylines \verb@@ \verb@enter a number> @ {\em -3} \verb@enter a number> @ {\em -4} \verb@enter a number> @ {\em -5} \verb@enter a number> @ {\em -7} \verb@enter a number> @ {\em -2} \verb@enter a number> @ {\em -1} \verb@enter a number> @ {\em -8} \verb@enter a number> @ {\em 2} \verb@ @ \verb@average of negative numbers = -4.285@ } The code segment is partially given below: \begin{verbatim} int num; cin >> num; while (num < 0) { } cout << "average of negative numbers = " << avg << endl; \end{verbatim} \clearpage \problem{Prime: {\em 10 points}} Assume a function \verb!IsPrime! exists whose header is {\small\begin{verbatim} int IsPrime(int num) // postcondition: returns 1 if num is prime, else returns 0 \end{verbatim}} Write the function \verb!PrimeCount! whose header is given below. \verb!PrimeCount! returns the number of prime numbers in parameter \verb!nums!. For example if \verb!numList! is diagrammed as \begin{center} \begin{tabular}{|c|c|c|c|c|c|c|c|}\hline 13 & 5 & 12 & 17 & 21 & 9 & 3 & 25 \\ \hline \end{tabular} \end{center} then the call \verb!PrimeCount(numList,6)! should evaluate to 3 since there are three primes (13, 5, 17) in the first 6 locations of \verb!numList!. In writing \verb!PrimeCount! you may call function \verb!IsPrime!. \begin{verbatim} int PrimeCount(int nums[], int numNums) // precondition: numNums = # of integers in nums // postcondition: returns number of primes in nums[0]...nums[numNums-1] { \end{verbatim} \vfill \begin{verbatim} } \end{verbatim} \clearpage \problem{Newton: {\em 10 points}} Given a positive real number $b$, and $x$, an approximation to $\sqrt{b}$, a new (and better) approximation can be determined from the old approximation using the equation \begin{displaymath} x_{\mbox{\tiny new}} = \frac{1}{2} \left( x_{\mbox{\tiny old}} + \frac{b}{x_{\mbox{\tiny old}}} \right) \end{displaymath} where the new and better approximation (on the left) is obtained from the old. This method is called {\em Newton's} method and is how calculators and computers find square roots. {\bf Example:} To compute the square root of $19$ (which is 4.358899...), an initial guess of $x=19$ could be improved using the formula above and as illustrated below. \begin{eqnarray*} 10 &=& \frac{1}{2} \left( 19.0 + 19.0/19.0 \right) \\ 5.950 &=& \frac{1}{2} \left( 10.0 + 19.0/10.0 \right) \\ 4.571639 &=& \frac{1}{2} \left( 5.95 + 19.0/5.95 \right) \\ 4.4363849 &=& \frac{1}{2} \left( 4.571639 + 19.0/4.571639 \right) \\ 4.358902 &=& \frac{1}{2} \left( 4.363849 + 19.0/4.363849 \right)\\ 4.358899 &=& \frac{1}{2} \left( 4.358902 + 19.0/4.358902 \right) \\ 4.358899 &=& \frac{1}{2} \left( 4.358899 + 19.0/4.358899 \right) \\ \end{eqnarray*} After this point, a new approximation results in no improvement so we use 4.358899 as our approximation of $\sqrt{19}$. Write a function that takes a number $b$ as a parameter, and computes an approximation to $\sqrt{b}$ by using the method described above. The method should be applied until the difference between the old and new approximations is less than $0.0001$, but no more than 20 approximations should be calculated (the initial guess doesn't count). You can use the standard function \verb!fabs(x)! which returns the absolute value of x (where x is of type double). (The f in \verb!fabs! stands for {\em float}.) \clearpage Complete the function below the following header. {\small\begin{verbatim} double Sqrt(double b) // precondition: b >= 0 // postcondition: return approximation to square root of b { \end{verbatim}} \vfill \begin{verbatim} } \end{verbatim} \clearpage \problem{Substring : {\em 10 points}} In this problem you will write a function \verb!SubString! whose header is given below. Explanation, documentation, and examples of the use of the class {\sl String} is provided first. {\small\begin{verbatim} class String { private: char letters[100]; // storage for letters in the string int numLetters; public: String(); // constructor, string initially has 0 chars void AppendChar(char); // add character to end of string int Length(); // return # of characters in string char KthChar(int); // return a specific char (starting with k=0) void MakeEmpty(); // sets string to have 0 chars }; \end{verbatim}} Sample code using the member functions is shown below. {\small\begin{verbatim} String str; cout << str.Length() << " "; str.AppendChar('a'); str.AppendChar('s'); cout << str.Length() << " "; cout << str.KthChar(1) << " "; str.MakeEmpty(); cout << str.Length() << endl; \end{verbatim}} This code results in the lines below being printed {\small\begin{verbatim} 0 2 s 0 \end{verbatim}} The function {\sl StringsEqual} (done for a homework assignment) is also given as an example of the use of this class. {\small\begin{verbatim} int StringsEqual(String s, String t) // postcondition: returns 1 if s == t, otherwise returns 0 { int len = s.Length(); int retval = 1; // assume strings are equal if (len == t.Length()){ int k; for(k=0; k < len; k++){ if (s.KthChar(k) != t.KthChar(k)){ retval = 0; // strings not equal break; // out of loop } } } else{ retval = 0; // lengths different, not equal } return retval; } \end{verbatim}} %\subsection*{Part A} Write the procedure {\sl SubString} whose header is given below. {\sl SubString} copies a sequence of characters from parameter \verb!s! to parameter \verb!result!, starting with the character designated by parameter \verb!first!. The number of characters copied is designated by parameter \verb!size!. If there are fewer than \verb!size! characters in \verb!s! starting from character \verb!first!, then all the characters in s starting from character \verb!first! should be copied to \verb!result!. For example: \begin{center} \begin{tabular}{llll} \multicolumn{1}{c}{\bf string s} & \multicolumn{1}{c}{\bf first} & \multicolumn{1}{c}{\bf size} & \multicolumn{1}{c}{\bf result} \\ cabbage & 0 & 1 & c \\ cabbage & 0 & 3 & cab \\ cabbage & 3 & 4 & bage \\ cabbage & 4 & 6 & age \\ \end{tabular} \end{center} \begin{verbatim} void SubString(String s, int first, int size, String & result) // precondition: first >= 0, size >= 1 // postcondition: appropriate substring of s stored in result { \end{verbatim} \vfill \begin{verbatim} } \end{verbatim} \hfill (turn page for extra credit) \clearpage \problem{No Duplicates : {\em 6 points}} Write the function \verb!RemoveDupes! whose header is given below. The function removes duplicate elements from parameter \verb!list!, a sorted array of integers. For example: \begin{center} \begin{tabular}{|c|c|c|c|c|c|c|c|}\hline 1 & 1 & 2 & 3 & 3 & 4 & 4 & 4 \\ \hline \end{tabular} \end{center} \begin{center} \begin{tabular}{|c|c|c|c|c|c|c|c|}\hline 1 & 2 & 3 & 4 & & & & \\ \hline \end{tabular} \end{center} Complete \verb!RemoveDupes! below the following header. In writing \verb!RemoveDupes! you may NOT use any array other than \verb!list!. {\small\begin{verbatim} void RemoveDups(int list[], int & numItems) // precondition: list is sorted in non-decreasing order // numItems = # of integers in list // postcondition: list contains no duplicates, and is sorted // numItems = # of integers in list \end{verbatim}} \end{document} \item {\bf Extra Credit:}\ \ If in any series of 100 consecutive price quotes each quote is less than the immediately preceding quote, then the stock should be sold (the stock is going down fast) and trading stopped. Note that as long as some quote is greater than the previous quote in a series of 100 then this rule is NOT invoked. \problem{Header files: {\em 14 points}} For each function, type, or class on the left, list the header file needed for its use. All header files are not used and a header file can be listed more than once. If no header file is needed (e.g., the type is built-in) put 0. \begin{tabular}{clll} \underline{\hspace*{.35in}} & int & 0. & no header file needed \\ \underline{\hspace*{.35in}} & Dice & 1. & iostream.h \\ \underline{\hspace*{.35in}} & String & 2. & math.h \\ \underline{\hspace*{.35in}} & ifstream & 3. & String.h \\ \underline{\hspace*{.35in}} & cout & 4. & heart.h \\ \underline{\hspace*{.35in}} & double & 5. & fstream.h \\ \underline{\hspace*{.35in}} & Shock & 6. & dice.h \\ \underline{\hspace*{.35in}} & sqrt & 7. & stdio.h \\ \end{tabular}