Lab 12: CPS08: Graphics, Recursion, Debugging You should work on this assignment on mentor. 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! *****Logging in to mentor From an xterm window type telnet mentor.cs.duke.edu} to connect to the machine mentor. Change into your cps08 directory and create a lab12 subdirectory. Then change into this and copy files by typing what's below (don't forget the trailing dot). cp ~ola/cps08/lab12/* . If you type ls you should see the following files: Makefile, subdir.cc, and usedir.cc. You need to give your machine permission for mentor to connect to it. To do this type in an xterm on the DEC station NOT in a mentor xterm the following: xhost +mentor.cs.duke.edu. Do NOT put a space after the + sign. You should then be able to type '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, check with a TA since all students should have working mentor accounts. ****** Lab First make a link to several graphics image files. To do this type ln -s /u/ola/misc/data/images images Then you'll be able to use the directory images to access several graphics files. ******** Using xv This section is just to get you familiar with the image-viewer xv. You shouldn't spend all lab with this. You'll use the graphics image viewer xv to look at some pictures. Type ls images to see the files that are stored in the images directory. Then type 'xv images/teapot.ppm' to view a ray-traced color teapot. Put the mouse cursor on the image and press the 'q' key to quit xv. Then use 'xv images/olanoise.pgm'. Click the RIGHT mouse button when the cursor is on the image. This will pop up a menu of xv controls. Click the left mouse button on the algorithms menu and choose the despeckle option, press ok for mask-size 3. This does median smoothing. There are many other things you can do with xv. The program you write for program 6 will do some of these as well. ****** Processing Directories Use the emacs commands C-x C-f to find/load the file usedir.cc. Then compile this command using C-x c and type make usedir in the minibuffer. You'll be prompted for the name of a directory. Type the name of your directory which is /u/login, e.g,. it is /u/ola if your login is ola. This will print a list of all files in the directory. The program does NOT print subdirectories. Then use C-x C-f to find/load the file subdir.cc. Compile and execute this program. Run the program and enter the name of your directory. This will print a listing of all your files and subdirectories. This can take a long time if you have many directories. You'll make a few changes to this program so that it will print the size of each file/subdirectory. ******* Sizes of files/directories Change the return type of the function ProcessDir from void to int. The function should return the size of all the files in the directory specified by the parameter s. In addition, the size of each individual file will be printed before the name of the file. To do this, use the member function Size() for the DirEntry variable entry. Print the size before the name of the file: cout << entry.Size() << "\t" << entry.Name() << endl; You should also define an int variable size that is initialized to 0 (define it after entry is defined). Increment size appropriately by the size of each file before the file is printed. You must also increment size by the value of all files in each subdirectory. To do this you'll modify the recursive call as shown below. size += ProcessDir(s + "/" + entry.Name(),tabCount+1); Finally, you'll need to return a value. The last statement of the function, after the if statement, should be return size. After making these changes, compile and run your program. Use the name of your own directory as input. How big is this directory ____________________? (Note: the counts may not be precisely correct, but are fairly accurate). How big is the directory /u/ola _______________________? \subsection*{Vectors/Arrays/Debugging Copy the file usedir.cc to mydir.cc using cp usedir.cc mydir.cc You'll modify the file so that each file name is placed in an array, and then printed. To do this follow the steps below (in emacs using mydir.cc). define Vector names(200); assuming that no directory has more than 200 names. use the integer variable num to keep track of the number of entries stored in names. In the while loop, rather than printing the name of each file, store the name of the file in names and increment num (increment after storing). Write a for loop (after the while loop) to print the name of each file stored in the vector names. Run the program and enter /dev as the name of the directory. What happens? Why? Now change the definition of names so that it uses a built-in array rather than a Vector String names[200]; Run the program again. You should get the error message: Segmentation fault (core dumped) To see what's happening, you'll use a symbolic debugger. In later courses you'll use the debugger more. Type gdb mydir, this will run the Gnu Debugger on the file mydir. When you get the prompt (gdb), type: run. When prompted for the name of the directory, enter /dev. The program will bomb, but print an error message. At this point you can type where and the program should print where the error occurs. In what line of main is the error? ___________________? Notice the \# signs when you typed where, they indicate how ``deep'' function calls are. Type up, this will move you ``up'' a function call. Then type print num to print the value of the variable num. You should get No symbol "num" in current context. Type up again followed by print num until a value of num is printed. What is the value? _______________________? ****** Submission 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. You can type make submit to submit the appropriate files, or you can type the command below (this doesn't include words.cc). submit08 lab12 README mydir.cc subdir.cc 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.