Test 1 : CPS 08 February 17, 1995 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 & 8 pts. &\ \\ & & \\ \hline\hline Problem 2 & 12 pts. &\ \\ & & \\ \hline\hline Problem 3 & 10 pts. &\ \\ & & \\ \hline\hline Problem 4 & 10 pts. &\ \\ & & \\ \hline\hline Problem 5 & 12 pts. &\ \\ & & \\ \hline \hline Extra & 6 pts. &\ \\ & & \\ \hline \hline TOTAL: & 52 pts. &\ \\ & & \\ \hline\hline \end{tabular} %\normalsize \end{table} } \hspace*{.5in} This test has 9 pages, be sure your test has them all. Do NOT spend too much time on one question --- remember that this class lasts 50 minutes. \clearpage \problem{Vocabulary: {\em 8 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 compiler} \begin{enumerate} \item A program that compiles a textual list of all functions used in a program indexed by the line numbers on which the functions appear. \item A program that translates a high-level language (e.g., C++) into a low-level language directly executable by a computer. \item A program used to check the semantic content of a C++ program to determine if it will function as the programmer intends. \end{enumerate} \item {\em member function} \begin{enumerate} \item A function that is part of a class, that can be applied to variables (objects) of the class, e.g., Length for the the class String. \item A function that belongs to a library of functions, e.g., sin and sqrt as part of \verb!!. \item A function whose prototype appears before the body of the function is actually defined. \end{enumerate} \item {\em ftp} \begin{enumerate} \item A Unix command that's used to move from one directory to another. \item A program used to set up an environment in Unix so that it works according to a user's preferences. \item A program and protocol used to transfer files from one computing system to another. \end{enumerate} \item {\em reference parameter} \begin{enumerate} \item A parameter that appears in a function header (or prototype) as opposed to in a function call. \item A parameter that appears in a function call as opposed to a function header (or prototype). \item A parameter that permits values to be ``passed back'' when a function is called. \end{enumerate} \end{enumerate} \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 = 13; double value = 3.5; String word = "lease"; cout << count + 15/6 * 2 << endl; cout << (7.0 + 13)/8 << endl; cout << count + 13 % 2 * value << endl; cout << "p" + word << endl; cout << word[3] << endl; cout << 3.5*3.14159*2.718/0 << endl; \end{verbatim}} \clearpage \problem{A Capital Idea: {\em 10 points}} A sample run of a program generating quizzes about countries and capitals is shown below. \vspace*{.1in} {\hsize=4.5in \begin{boxoutput} {\leftskip=.3in \small\setlength{\parskip}{0pt} \obeylines \verb@@ \verb@ How many questions : @{\em 3} \verb@ What is the capital of Venezuala? @{\em Caracas} \verb@ Correct@ \verb@ What is the capital of Kenya? @{\em Mombasa} \verb@ No, the capital is Nairob@i \verb@ What country has Reykjavik as its capital? @{\em Iceland} \verb@ Correct@ \verb@ You got 2 out of 3 right@ \verb@@ } \end{boxoutput} } Assume that the function {\tt GeoQuiz} whose header is given below is accessible by including the header file \verb!"geoquiz.h"!. \begin{verbatim} void GeoQuiz(String & country, String & capital); // postcondition: sets country and capital to a random // country and its capital // e.g., "United States", "Washington D.C." \end{verbatim} You may call {\tt GeoQuiz}, you should NOT write {\tt GeoQuiz}. \underline{Complete} the program below so that it gives the kind of quiz shown above in the sample run. Each question should either print a country and ask for the capital or print the capital and ask for the country by simulating a ``coin flip'' (this can be done using a {\tt Dice} variable). You can put all the code in {\tt main}. Use the next page if you need more room. \begin{verbatim} #include "dice.h" #include #include "geoquiz.h" main() { int numQ; cout << "How many questions : "; cin >> numQ; // continued on next page \end{verbatim} \clearpage \begin{verbatim} GeoQuiz(country,capital); if (die.Roll() == 1) { cout << "What is the capital of " << country << "? "; if (answer == capital) { cout << "Correct" << endl; } else{ } } else { cout << "What country has " << capital << " as its capital? "; } cout << "You got " << numRight << " out of " << numQ << " right" << endl; \end{verbatim} \clearpage \problem{A Perfect World : {\em 10 points}} A number is called a {\em perfect} number if it is equal to the sum of its proper divisors (not including the number itself). For example, 6 and 28 are perfect because $6 = 1 + 2 + 3$ and $28 = 1 + 2 + 4 + 7 + 14$. The partial program below is designed to print all perfect numbers between 6 and a number entered by the user. If run with the input 30 it will print 6 and 28 (each on a separate line). Only the prototype for \verb!IsPerfect! is given, you are to write the body of the function below {\tt main}. In writing {\tt IsPerfect} do NOT worry about the efficiency of the function, just its correctness. Include brief pre and post-conditions for {\tt IsPerfect}. \begin{verbatim} #include int IsPerfect(int num); main() { int num; int limit; cout << "enter upper limit :"; cin >> limit; for(num=6; num <= limit; num++) { if ( IsPerfect(num) ) { cout << num << endl; } } } \end{verbatim} \clearpage \problem{A Course is a Course of course of course : {\em 12 points}} A class {\tt AceCourse} is designed to allow C++ programs to get information about academic courses at Duke using the designated ACES number. The header file \verb!"acecourse.h"! is reproduced below. As a sample of its use, the code fragment below {\small\begin{verbatim} AceCourse course(139772); if (course.IsValid()) // 139772 is a valid course # { cout << course.Dept() << ", " << course.Title() << ", "; cout << course.Instructor() << ", " << course.Area() << endl; cout << "enrollment ceiling = " << course.Limit() << endl; } \end{verbatim}} generates the output below. {\small\begin{verbatim} ART, Art and Cyberculture, K. Stiles, AL enrollment ceiling = 15 \end{verbatim}} \begin{center} \fbox{\bf Note: all ACES course numbers are six-digit numbers between 100,000 and 160,000.} \end{center} {\small\begin{verbatim} // declaration for class AceCourse --- used to get/set information // about courses at Duke using ACES call numbers // // AceCourse(int acenum) // // -- construct object having ACES call # // e.g., AceCourse course(139772); defines course // to correspond to Art 49S, Art and Cyberculture // // String Dept(), Title(), Instructor(), Area() // // -- return department, title, instructor, and area of knowledge // // int Limit() // // -- returns maximum number of students in course // // void Meeting(String & day, int & period) // // -- returns day course meets (either "M-W-F" or "T-Th" // and period course meets (1-9) // // void SetNumber(int acenum) // // -- change course number to acenum (passed as parameter // e.g., course.SetNumber(144560); makes course CPS 08.01 // // bool IsValid(); // // -- returns true if course number used in constructor/SetNumber // corresponds to valid Duke Course // e.g., after course.SetNumber(1); // course.IsValid() returns false since 1 isn't a valid ACES # class AceCourse { public: AceCourse(int acenum); // construct course from ACES number String Dept(); // dept of course String Title(); // title of course String Instructor(); // teacher of course String Area(); // area of knowledge int Limit(); // max # of students void Meeting(String &,int &); // time course meets: MWF/Th, 1-9 void SetNumber(int acenum); // set course to have ACES # acenum bool IsValid(); // true if course is ok, false if not private: // stuff here }; \end{verbatim}} %\clearpage {\bf Part A: (3 points)} The ACES number for Political Science 110 is 129244. Write a code segment that prints the instructor for the course and the day and period that the course meets. You MUST use {\tt AceCourse} member functions to do this, you CANNOT write \verb!cout << "P. Gronke" << endl;! to print the instructor, for example. \clearpage {\bf Part B: (9 points)} Write the function {\tt KnowledgeArea} whose header is given below. The function should print the title of every course whose area of knowledge is the value specified by the parameter {\tt distrib}. A count of the number of courses should be printed too. For example, the call {\tt KnowledgeArea("CZ");} might generate output similar to the following: \begin{verbatim} ... Health, Healing, History American History/Social Theory The Atlantic Slave Trade # of courses with CZ designation = 152 \end{verbatim} Complete the function below. \begin{verbatim} void KnowledgeArea(String distrib) // postcondition: titles of all courses with area of knowledge = distrib // printed with a count of the number of such courses { \end{verbatim} \vfill \begin{verbatim} } \end{verbatim} \clearpage \problem{Extra Credit: {\em 6 points}} {\bf This problem is NOT required} An {\em Armstrong number} is any number of $n$ digits such that the number is equal to the sum of each digit raised to the $n^{th}$ power. Formally: a number $i$ = $d_1 d_2\ldots d_n$ is an armstrong number if \begin{displaymath} i = \sum_{j=1}^n (d_j)^n \end{displaymath} \vspace{1in} \noindent Example: $153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27$\\ Example: $9 = 9^1 = 9$ Write the function {\tt IsArmstrong} that returns true if {\tt num} is an Armstrong number and false otherwise. \begin{verbatim} bool IsArmstrong(int num) // precondition: 0 < num // postcondition: returns true if num is an Armstrong number // otherwise returns false \end{verbatim} \end{document}