\documentstyle{article} \input{/u/ola/lib/wide} \input{/u/ola/lib/para} \begin{document} \begin{center} \Large \bf Lab 8: CPS08: Recursion, Exponentiation, Rabbits \end{center} You should work on this assignment on mentor. It is on the acpub system as well, but you should get used to working on mentor during lab. It would be prudent, however, to back things up by ftp-ing files to your acpub account. If you can't log into mentor, get help NOW! \subsection*{Logging in to mentor} \begin{enumerate} \item From an xterm window type {\tt telnet mentor.cs.duke.edu} to connect to the machine mentor. \item Change into your {\tt cps08} directory and create a {\tt lab8} subdirectory. Then change into this and copy files by typing what's below (don't forget the trailing dot). \begin{verbatim} cp ~ola/cps08/lab8/* . \end{verbatim} If you type {\tt ls} you should see the following files: {\tt Makefile}, {\tt numbers.cc} and {\tt fib.cc}. \item You need to give your machine permission for mentor to connect to it. To do this type {\em in an xterm on the DEC station} NOT in a mentor xterm the following: \begin{verbatim} xhost +mentor.cs.duke.edu. \end{verbatim} Do NOT put a space after the + sign. \item You should then be able to type {\tt emacs \&} to get an emacs window. You want to use the ampersand \& which leaves you with control of your xterm (so you can run programs from it). If this doesn't work, skip to the description of {\bf Acpub Alternative} below for completing the lab on the acpub machines. You should check with a TA since all students should have working mentor accounts. \end{enumerate} \subsection*{Acpub Alternative} Change into your {\tt cps08} directory using the {\bf cd} command and create another directory called {\bf lab8} using the {\bf mkdir} command. Change into the {\bf lab8} directory. If you did this correctly, when you type {\bf pwd} you should see a long path name that ends with {\bf /cps08/lab8.} You need to copy some files to do this lab. The {\bf cp} command is used for copying files on Unix systems. Type the following command to copy files for the lab (don't forget the trailing period, or dot): \begin{verbatim} cp ~ola/cps08/lab8/* . \end{verbatim} This command will copy all the files in the directory \verb!~ola/cps08/lab8! into your {\tt cps08/lab8/} directory. If you type {\tt ls} you should see the following files: {\tt Makefile}, {\tt numbers.cc}, and {\tt fib.cc}. \subsection*{Lab} Use the emacs commands {\tt C-x C-f} to find/load the file {\sl numbers.cc}. Then compile this command using {\tt C-x c} and type {\tt make numbers} in the minibuffer. You'll be prompted for a base and an exponent as well as the number of times to iterate. Then two algorithms will be used to compute $a^n$ (where $a$ and $n$ are the numbers entered). Use the program to compute $12^{2000}$ three times (enter 3 for number of iterations). How long does it take using the linear method \underline{\hspace*{.75in}}? Using the logarithmic method \underline{\hspace*{.75in}}? When the number is printed, there are no commas used, e.g., numbers are NOT printed as $1,023,567$. You are to finish implementing the function {\tt WriteCommas} that is supposed to write the value of {\tt num} with commas inserted properly. In the current program, commas are insetred, but some of the three-digit sequences are NOT printed properly. To print them properly, you'll need to print leading zeros when appropriate (e.g., 1,023,001,456). There are several methods for doing this, you can either check the value mathematically and print leading zeros, or you can set the fill character to '0' and the printing width to three (this was done in the clocktime class). To do the latter you'll need to add the code below in the appopriate place. \begin{verbatim} char ch = cout.fill(); // remember fill character cout.fill('0'); // fill with zeros cout.setf(ios::left); // left justified cout.width(3); // print number here cout.fill(ch); // reset fill character \end{verbatim} Make sure you test the function by having it print numbers you know about (unlike $12^{2000}$ which you probably haven't memorized). \subsection*{Fibonacci} Fibonacci numbers appear in nature, in mathematics, and are used in analyzing algorithms in computer science. The Fibonacci sequence is: \begin{displaymath} 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, \ldots \end{displaymath} where each number is the sum of the previous two. This can be used to calculate the $n^{\mbox{\small th}}$ Fibonacci number using a routine like the one shown below \begin{verbatim} Integer Fib(int n) // precondition: 0 < n // postcondition: returns n-th Fibonacci number { if (n == 1 || n == 2) { return 1; // first two Fib numbers are 1 } else { return Fib(n-1) + Fib(n-2); // otherwise sum of previous two } } \end{verbatim} You are to create a program based on this function --- use {\tt fib.cc} as the start. Use the program to calculate the first 28 Fibonacci numbers by calling it in an appropriate loop. You should time how long it takes to compute each number, so that you can print a table like this (to print a tab you can use something like \verb!cout << ... << "\t" << ... !) \begin{verbatim} number Fibonacci time (secs) 1 1 0 2 1 0 3 2 0 4 3 0 5 5 0 6 8 0 7 13 0 8 21 0 9 34 0 ... 20 ?? ?? \end{verbatim} After you've done this, think about how long it will take to compute the 100th Fibonacci number using this method. Try to determine a reasonable approximation to how long this will take. Now you'll implement another method for computing Fibonacci numbers that doesn't require using recursion. To do this you'll need a loop: \begin{verbatim} first = 1; second = 1; for(k=0; k < ??; k++) { temp = first + second; first = second; second = temp; } return ???? \end{verbatim} Here the variables {\tt first} and {\tt second} are initialized to the first two Fibonacci numbers and are then updated so that they always hold the ``latest'' two numbers. You should include this code in a function {\tt Fib2}, and use it to calculate the first 50 Fibonacci numbers, printing a table similar to the one you printed using the recursive version. Then calculate the 100-th Fibonacci number. What are the last three digits \underline{\hspace*{1in}}? \subsection*{Heart} If you have more time in lab, you should start working with the heart monitor program so that you can ask questions of the TAs. \subsection*{Submission} {\bf To turn in programs}: When your modified programs works, you can submit it, along with a README describing how long it took you and what your impressions of the lab are. Submit works on the DEC stations and on mentor. \begin{center} \tt submit08 lab8 numbers.cc fib.cc README \end{center} The README file should include your name, how long you worked on the program, and anyone you received significant help from. You should also include any comments you have about the lab. You should receive a message telling you that the programs were submitted correctly. \end{document}