\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{April 7, 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 & 6 pts. &\ \\ & & \\ \hline\hline Problem 2 & 7 pts. &\ \\ & & \\ \hline\hline Problem 3 & 16 pts. &\ \\ & & \\ \hline\hline Problem 4 & 15 pts. &\ \\ & & \\ \hline\hline Problem 5 & 16 pts. &\ \\ & & \\ \hline \hline extra: & 8 pts. &\ \\ & & \\ \hline \hline TOTAL: & 60 pts. &\ \\ & & \\ \hline\hline \end{tabular} %\normalsize \end{table} } \vfill This test has 9 pages. Be sure you have them all. There are blank pages to work on. Don't spend too much time on one problem. \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 tolower} \begin{enumerate} \item a String member function that converts all characters in the string to lower case. \item a function defined in \verb!! that converts characters to lower case. \item a function defined in \verb!! that rounds a double down to an int (e.g., 3.14 to 3). \end{enumerate} \item {\em seekg} \begin{enumerate} \item a stream member function that can be used to reset a stream so that it can be reread from the beginning. \item a UNIX command asking for help from a guru (one who knows everything about UNIX). \item a stream member function used to read compiled (object) code from files on systems to which client programs don't normally have access. \end{enumerate} \item {\em Fibonacci} \begin{enumerate} \item the co-inventor, with Bjarne Stroustrup, of C++. \item an amateur mathematician who wrote about sequences based on rabbit-breeding. \item an editor, similar to Emacs, but that uses less memory. \end{enumerate} \end{enumerate} \problem{Phrases (7 points)} {\bf Part I} (3 points) In at most two sentences, (one word can earn full credit) describe one feature of object-oriented programming. \hspace*{.5in} {\bf Part II} (4 points) What is printed if the user enters \verb!"pleasing to the eye"! when running the program below. (Indicate values for \verb!t! and \verb!s! for partial credit.) {\small\begin{verbatim} #include #include "CPstring.h" main() { String s,t; cout << "enter phrase: "; s.GetLine(cin,'a'); cin >> t; cout << t+s << endl; } \end{verbatim}} \clearpage \problem{Zero Crossing Count (16 points)} For this question you will write three functions. The array \verb!A!, diagrammed below, will be used to illustrate the functions. \begin{center} \begin{tabular}{|c|c|c|c|c|c|c|} \hline 5 & 3 & 8 & 2 & 1 & 10 & 7 \\ \hline \end{tabular} \end{center} {\bf Part A:} ( 6 points) Write the function \verb!IndexMax! that returns the \underline{index} of the largest element in the array/vector \verb!list!. The call \verb!IndexMax(A,7)! (where \verb!A! is diagrammed above) should evaluate to 5 since the largest element, 10, has index 5. \begin{verbatim} int IndexMax(const Vector & list, int numElts) // precondition: numElts = # of elements in list // list contains no duplicate elements // postcondition: return index of largest element in list { \end{verbatim} \vfill \begin{verbatim} } \end{verbatim} \clearpage {\bf Part B:} (6 points) Write the function \verb!IndexSecond! that returns the \verb!index! of the second largest element in the vector \verb!list!. The call \verb!IndexSecond(A,7)! should evaluate to 2 since the second largest element, 8, has index 2. In writing \verb!IndexSecond! assume that \verb!IndexMax! works as specified regardless of what you wrote in part A. \begin{verbatim} int IndexSecond(const Vector & list, int numElts) // precondition: numElts = # of elements in list // list contains no duplicate elements // postcondition: return index of second largest element in list { \end{verbatim} \vfill \begin{verbatim} } \end{verbatim} \clearpage {\bf part C} (4 points) The function \verb!ShiftRight! shown below is intended to ``shift'' all elements of the array one place to the right, inserting a new value as the first element of the array. The rightmost element is ``shifted off the end'' in this process. The call \verb!ShiftRight(A,7,27)! should result in the array A: \begin{center} \begin{tabular}{|c|c|c|c|c|c|c|} \hline 27 & 5 & 3 & 8 & 2 & 1 & 10 \\ \hline \end{tabular} \end{center} Instead, because of a mistake in the code, it results in the array A: \begin{center} \begin{tabular}{|c|c|c|c|c|c|c|} \hline 27 & 5 & 5 & 5 & 5 & 5 & 5 \\ \hline \end{tabular} \end{center} {\em Describe} a way of fixing the code. You don't need to write code, although you may if this is the easiest way to explain how to fix the code. \begin{verbatim} void ShiftRight(Vector & list, int numElts, int newValue) { for(k=0; k < numElts-1; k++) { list[k+1] = list[k]; } list[0] = newValue; } \end{verbatim} \vspace*{.5in} \problem{Without a Net ( 15 points)} The struct \verb!Stock! below is used to store the name of a stock, the number of shares of the stock owned, and the price at which the stock was purchased. \begin{verbatim} struct Stock { String name; // name of company, e.g., IBM, Chrysler int shares; // # of shares owned (at given price) double price; // price at which shares purchased }; \end{verbatim} A class \verb!Broker! exists that supports the member functions whose headers are given below: \begin{verbatim} Broker::Broker(); // constructor, creates/initializes a broker object double Broker::CurrentPrice(String name); // precondition: name is a company for which the broker has information // postcondition: returns current price of stock with given name \end{verbatim} You are to write the function \verb!NetWorth! whose header is given below. This function returns the net worth of all the stocks in parameter \verb!portfolio!. The networth of one share of single stock is the difference between the current price and the purchase price. \clearpage For example, if Microsoft is bought at \$30.00, and the current price is \$50.00, the networth of one share is \$20.00. If there are 5 shares of Microsoft in the portfolio, the networth of Microsoft is \$100.00 ($5 \times 20$). If the current price is less than the purchase price, the networth will be negative. For example, with the stocks below the networth is \$40.00. \begin{center} \begin{tabular}{crrr|r} \multicolumn{1}{c}{name} & \multicolumn{1}{c}{purchase} & \multicolumn{1}{c}{current} & \multicolumn{1}{c}{shares} & \multicolumn{1}{c}{worth} \\ \hline Microsoft & 30.00 & 50.00 & 5 & 100.00 \\ Motorola & 20.00 & 35.00 & 10 & 150.00 \\ Genentech & 50.00 & 20.00 & 7 & -210.00 \end{tabular} \end{center} \begin{verbatim} double NetWorth(Vector & portfolio, int numStocks) // precondition: numStocks = # of stocks in portfolio // postcondition: retuns net worth of portfolio \end{verbatim} \clearpage \problem{It occurs to me (16 points)} Write a function \verb!Modal! that finds the string that occurs most often in a vector of strings. If there is more than one string that occurs most often (e.g., two strings both occur 10 times, all other strings occur less than 10 times), then either one can be returned. The function should return both the string and the number of times it occurs. The statements below \begin{verbatim} Vector list(5000); int numStrings, occurs; String maxStr; // code here to fill list, set numStrings to # of strings in list Modal(list,numStrings,maxStr,occurs); cout << maxStr << " occurs " << occurs << " times" << endl; \end{verbatim} should set \verb!maxStr! to the maximally occurring string and \verb!occurs! to the number of times it occurs. You may find it useful to write another function that is called from \verb!Modal! (hint: write a function that is passed a vector and a string and returns how many times the string occurs in the vector) but this isn't required. \clearpage \ \ \ \ \vfill \ \ \ \hfill (over for extra credit) \clearpage \problem{Extra Credit: (runaway) (8 points)} Write a function that returns the length of the longest ``run'' in a sorted array. All the integers in a run are equal. For example: \begin{center} \begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|c|} \hline 2 & 4 & 4 & 5 & 5 & 5 & 5 & 6 & 7 & 7 & 7 & 8 \\ \hline \end{tabular} \end{center} In this array there is a run of 4's (length 2), a run of 5's (length 4) and a run of 7's (length 3). There are runs of 2, 6, 8 of length 1. \begin{verbatim} int MaxRun(Vector & list, int numItems) // precondition: list is sorted in non-decreasing order // postcondition: returns length of maximal run \end{verbatim} \end{document}