Test 1: CPS 08 October 7 1994 Name: __________________ problem 1: 8 pts _______ problem 2: 8 pts _______ problem 3: 8 pts _______ problem 4: 10 pts _______ problem 5: 10 pts _______ problem 6: 10 pts _______ \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 Makefile} \begin{enumerate} \item a file that must be compiled ahead of all other files before linking can occur. \item a file used in compiling programs that insulates the user from the need to know where certain files exist and how the compiler is invoked. \item a Unix command that creates a file without the need to use emacs or some other editor. \end{enumerate} \item {\em constructor} \begin{enumerate} \item A method of building programs by implementing the programs one piece at a time, ensuring that all pieces work before proceeding to the next piece. \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 function that is automatically invoked and that initializes data fields of a class. \end{enumerate} \item {\em emacs} \begin{enumerate} \item An editor and environment in which to develop programs. \item A compiler for C++ programs. \item A debugger used with C++ programs. \end{enumerate} \item {\em short-circuit evaluation} \begin{enumerate} \item A term used to express the precedence hierarchy of operators, e.g., that the operator \verb!*! has higher precedence than \verb!+!. \item A term used to describe how evaluation of the logical operators \verb!&&! and \verb!||! works in evaluating their operands from left-to-right. \item A term used to describe the mechanism by which parameters are passed by reference (e.g., when a parameter is defined as \verb!int & small!). \end{enumerate} \end{enumerate} \problem{Header files: {\em 8 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. & CPstring.h \\ \underline{\hspace*{.35in}} & cout & 4. & balloon.h \\ \underline{\hspace*{.35in}} & double & 5. & fstream.h \\ \underline{\hspace*{.35in}} & Balloon & 6. & dice.h \\ \underline{\hspace*{.35in}} & sqrt & 7. & stdio.h \\ \end{tabular} \clearpage \problem{Trace/Debug: {\em 8 points}} The program below prints three lines of output with two numbers on each line. {\small\begin{verbatim} #include int Test(int first, int second) // postcondition: returns some value { int a = first + second; if (first == second){ return a; } else if (first > second){ return 2*a; } else{ return a + 5; } } main() { int a,b,c; a = 3; b = 7; c = 15; cout << a + c/b << " " << a + c % b << endl; cout << (c == 5*a) << " " << (3*b + 1.5)/c << endl; cout << Test(b,c) << " " << Test(c,b) << endl; } \end{verbatim}} What is the output of the program? \vspace*{2in} If the line \verb!if (first == second)! is replaced with \verb!if (first = second)! in the function \verb!Test! the third line of output will change. What is the new third line of output? \clearpage \clearpage \problem{Average: {\em 10 points}} You are to complete a code segment that will permit the user to enter integers until the number 0 is entered. After all the numbers are entered, the average of the ``special'' numbers should be printed. A number is {\em special} if the function \verb!IsSpecial!, whose header is given below, returns 1. {\small\begin{verbatim} int IsSpecial(int num) // precondition: num > 0 // postcondition: returns 1 if num is special, else returns 0 \end{verbatim}} For example, in the input sequence below only the numbers 5 and 8 are special (i.e., \verb!IsSpecial(5) == 1!, \verb!IsSpecial(8) == 1!, but \verb!IsSpecial(4) == 0!). \vspace*{.1in} {\hsize=4.5in \begin{boxoutput} {\leftskip=.3in \small\setlength{\parskip}{0pt} \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 0} \verb@@ \verb@average of special numbers = 6.5@ \verb@@ } \end{boxoutput} } The code segment is partially given below: {\small\begin{verbatim} int total = 0; int numSpecial = 0; int num; while ( ) { } cout << "average of special numbers = " << avg << endl; \end{verbatim}} \clearpage \problem{Surface Area: {\em 10 points}} Doug Cooper says that the surface area (in square meters) of a person can be approximated by the formula \begin{displaymath} \mbox{\rm surface area} = 7.184^{-3}\; \times\; \mbox{\rm Weight}^{0.452}\; \times\; \mbox{\rm Height}^{0.725} \end{displaymath} where weight is measure in kilograms and height in centimeters. The program below is designed to allow a user to calculate a person's surface area. You are to fill in the \verb!main! function so that the program would run and generate output like that shown below. \begin{center} \bf No code should be added except in \verb!main!. \end{center} \vspace*{.1in} {\hsize=4.5in \begin{boxoutput} {\leftskip=.3in \small\setlength{\parskip}{0pt} \obeylines \verb@@ \verb@enter weight (in lbs.): @ {\em 155} \verb@enter height (in inches): @{\em 73} \verb@your surface area = 2.6119 sq. meters@ \verb@@ } \end{boxoutput} } {\small\begin{verbatim} #include #include void GetVitals(double & weight, int & height) // postcondition: sets weight (kgs) and height (cms) { cout << "enter weight (in lbs.): "; cin >> weight; weight /= 2.2; // convert to kilograms cout << "enter height (in inches): "; cin >> height; height *= 2.54; // convert to centimeters } double SurfaceArea(double height, double weight) // precondition: height and weight given in cms and kgs respectively // postcondition: returns surface area of person with given height/weight { return 0.007184 * Pow(weight,0.452) * Pow(height,0.725); } main() { \end{verbatim}} \vfill {\small\begin{verbatim} } \end{verbatim}} \clearpage \problem{Elementary, my dear Watson: {\em 10 points}} A class \verb!Element! for manipulating elements from the periodic table of elements is described below. For this problem assume that there are 103 elements with consecutive atomic numbers from 1 to 103. The header file for the class is \verb!"elements.h"!. {\small\begin{verbatim} // class for manipulating elements (as in periodic table) // // Element(int atomNumber) -- create element with given atomic number // e.g., Element hydrogen(1); creates hydrogen (atom. number = 1) // Element carbon(6); creates carbon (atom. number = 6) // // String Name(); -- returns name of element, // e.g., "hydrogen", "carbon" for hydrogen and carbon respectively // // String Symbol(); -- returns element's symbol, // e.g., "H" for hydrogen, "C" for carbon // // double Weight(); -- returns atomic weight of element, // e.g., 1.008 and 12.011 for hydrogen and carbon respectively // // int AtomicNumber(); -- returns atomic number of element, // e.g., 1 for hydrogen and 6 for carbon // // void SetAtomic(int num); -- change atomic number to num // e.g., elem.SetAtomic(8); sets elem to represent oxygen // (the 8th element) class Element{ public: Element(int atomNumber); // element will have atomic # = parameter String Name(); // returns name of element String Symbol(); // returns symbol of element double Weight(); // returns weight of element int AtomicNumber(); // returns atomic number of element void SetAtomic(int num); // change atomic number to num private: // stuff here that you can't see }; \end{verbatim}} {\bf Part A:} The atomic number of iron is 26. Complete the statements below so that the symbol of the element iron and its atomic weight are printed in some format (you must use member functions to provide the values of the symbol and weight). {\small\begin{verbatim} Element cout << \end{verbatim}} \clearpage {\bf Part B:} Write a program that permits the user to enter the name of an element and that prints the atomic number, and weight of that element. The user should be allowed to enter names until control-D is entered to signify no more data (end-of-file). \vspace*{.1in} {\hsize=4.5in \begin{boxoutput} {\leftskip=.3in \small\setlength{\parskip}{0pt} \obeylines \verb@@ \verb@enter name of element (control-D to finish):@ {\em copper} \verb@copper: number = 29, weight = 63.54@ \verb@enter name of element (control-D to finish):@ {\em kryptonite} \verb@kryptonite: not a recognized element@ \verb@enter name of element (control-D to finish):@ {\em phosphorus} \verb@phosphorus: number = 15, weight = 30.975@ \verb@enter name of element (control-D to finish):@ {\em \^\ D} \verb@@ } \end{boxoutput} } \vspace*{.1in} {\small\begin{verbatim} #include #include "elements.h" \end{verbatim}} \vfill {\small\begin{verbatim} main() { String name; cout << "enter name of element (control-D to finish): "; while (cin >> name){ cout << "enter name of element (control-D to finish): "; } } \end{verbatim}} \hfill ({\em over for extra credit}) \clearpage Who invented the language C++ \underline{\hspace*{2.5in}} ({\em 2 points}) \vspace*{1in} The formula below yields an approximation of $\pi/4$: \begin{displaymath} \frac{\pi}{4} = 1 - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \frac{1}{9} - \cdots \end{displaymath} The more terms of the formula used, the better the approximation (up to a point). Write a function \verb!PiApprox! that uses the formula above to approximate $\pi$. The value of the parameter \verb!numTerms! provides the number of terms of the formula to use. For example, if one term is used then $\pi/4 = 1$ so $\pi = 4$. If three terms are used the $\pi/4 = 1 - 1/3 + 1/5$ so $\pi = 3.468$ since $1 - 1/3 + 1/5 =0.867$. \vspace*{.1in} (5 points) \vspace*{.1in} {\small\begin{verbatim} double PiApprox(int numTerms) // postcondition: returns approximation to pi using numTerms terms { \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. {\bf Part B} A student finds the following formula for approximating $\pi/4$ in a mathematics book: \begin{displaymath} \pi/4 = 1 - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \frac{1}{9} - \cdots \end{displaymath} and translates the formula into a function \verb!PiApprox! that approximates $\pi$ using the formula above with the number of terms specified by the parameter \verb!numTerms!. However, the student notices that {\small\begin{verbatim} cout << PiApprox(10) << endl; \end{verbatim}} prints 4.0 and that if the 10 is replaced with any number the value printed is always 4.0. Describe a \underline{simple} change to the code below that will permit the function \verb!PiApprox! to work as intended. {\small\begin{verbatim} double PiApprox(int numTerms) // postcondition: returns approximation to pi using numTerms terms { double sum = 0.0; // accumulate sum of pi/4 int denominator = 1; int k; for(k=0; k < numTerms; k++){ if (k % 2 == 0){ // first, third, ... terms are added sum += 1/denominator; } else{ // second, fourth,... terms subtracted sum -= 1/denominator; } denominator += 2; } return 4.0*sum; } \end{verbatim}}