Com S 228 --- Introduction to Data Structures HOMEWORK 2: Control Abstraction (File $Date: 1995/02/08 16:02:17 $) Due: problem 2, at the beginning of your discussion section the week of Feb. 2; problem 4, at the beginning of class on Feb 6; problem 6, at the beginning of your discussion section the week of Feb 9. In this homework, you will learn about loops, control abstraction, function specifications, and arrays. The readings are in the section headings below. Problem 1 is suggested practice to help learning loops in C++. Problem 3 is more suggested practice to help with this. These are very important for your learning the course material, especially if looping is not familiar to you. For this homework, don't use recursion for any solution. For each programming problem, hand in a printout of your code files, and your transcript. Your transcript must be run on the Com S department machines (use the Unix command ``script''); it should show your testing. Your transcript and each file you print should have the file name and your information (your ~/.me file as in HW0) at the top as comments. You should also use comments as we are doing in class. For additional practice, we suggest that for at least some of the problems, you write out a solution on paper first. This is more like what the test will be. When you are happy with your paper version, try it on the computer. (Note that you must hand in a printout and a transcript, however.) We're experimenting with putting time estimates on the problems. Let us know what you think of them. (Are they helpful, misleading, etc.?) HEADINGTON & RILEY: Appendix A.13-14, and DEITEL & DEITEL: Chapter 2, and Sections 3.3-9, 3.11, and 3.15. UNIX MANUAL: page for "make" (type "man make"). (See also the makefile examples in the files /home/cs228/public/examples/*/Makefile) 1. (suggested practice) Do some of the exercises in the self-review section of Deitel & Deitel's book. I suggest working exercises 2.1, 2.4-5, 2.7-11, 2.12(a,d,e), and 2.13(a,c) on paper. Do what parts seem useful to you, then look at the answers. 2. (40 points) Time estimate: about 2 hours. Write a program, tempconv, that prints a Celsius (C) to Farenheit (F) conversion table, and also provide a makefile for your program. Recall that for given temperature, F, the Celsius equivalent is C = (5/9)*(F-32). The program should prompt for and input: a number, low, representing a minimum temperature in degrees C, a number, high, representing a maximum temperature in degrees C, a number, step, representing degrees C. The program should check that low <= high, and step > 0; If these conditions are not met, then the program should print an error message of the form tempconv: bad input: , , on cerr, with replaced by the input value of low, replaced by the input value of high, and similarly for . If an error message is issued, the program should return 1 to the operating system, otherwise it should return 0. This will be implicit in all problem specifications from now on. The program should then print a heading, and then a table of equivalent temperatures, with the left column in degrees C and the right is in degrees F, and the columns separated by a tab ("\t"); the table should start (at the top) with low, and go up to high in increments of size step. That is, the program should print the label and lines for each of the following (in order): low, low + step, low + 2*step, low + 3*step, ..., low + k*step, where k is the greatest integer such that low + k*step <= high, but low + (k+1)*step > high. For example, consider the following as a sample of several interactions with the program. The % is the unix prompt. (Your answers may be slightly different due to the approximate nature of floating point calculations. Use the C++ type double for variables storing degree values to get closest. Also the exact formatting of the numeric output may differ somewhat.) % cat ~/.me // Name: Gary T. Leavens // TA: Bjarne Stroustrup // Section: A1 % % ./tempconv.exe low temperature (degrees C)? -20.0 high temperature (degrees C)? 50.0 step size (degrees C)? 5.0 C F -20 -4 -15 5 -10 14 -5 23 0 32 5 41 10 50 15 59 20 68 25 77 30 86 35 95 40 104 45 113 50 122 % % ./tempconv.exe low temperature (degrees C)? -40 high temperature (degrees C)? 100 step size (degrees C)? 20 C F -40 -40 -20 -4 0 32 20 68 40 104 60 140 80 176 100 212 % % ./tempconv.exe low temperature (degrees C)? 5.345 high temperature (degrees C)? 72.1156 step size (degrees C)? 21.579 C F 5.345 41.621 26.924 80.4632 48.503 119.305 70.082 158.148 % echo $status 0 % % ./tempconv.exe low temperature (degrees C)? 50 high temperature (degrees C)? -5 step size (degrees C)? 3 tempconv: bad input: 50, -5, 3 % echo $status 1 % % ./tempconv.exe low temperature (degrees C)? 0 high temperature (degrees C)? 22 step size (degrees C)? -1 tempconv: bad input: 0, 22, -1 % echo $status 1 % For grading this problem, you'll get full credit for handing it in (code and transcript), along with a makefile, during your discussion section. (The usual late policy applies). Note that the transcript should also cover testing of your makefile. In your discussion section, you'll discuss your solution with another class member, and will have an opportunity to ask questions and discuss solutions with the class as a whole. So be sure to write your program clearly. We won't check the details of your work, as that will be the subject of the discussion. So don't worry (too much) if it doesn't work exactly right. We will check that what you hand in has the right parts (a printout of each file, labeled correctly with comments at the top, and a transcript). HEADINGTON & RILEY: Introduction and Chapter 1, 3. (suggested practice) Do some of the exercises in Headington and Riley's book for chapter 1 (pp. 47-50). I suggest exercises 1.1(a,c,e,g), 1.2-5(a,c), 1.7. Do what parts seem useful to you, then look at the answers, which are at the end of the book, in pages A-169 to A-171. DIETEL & DIETEL: 4.1-5 HEADINGTON & RILEY: Appendix A.7 and page A-45 4. (60 points) Time estimate: 3 hours. Do programming project 1.3 in Headington and Riley's book, page 53. HEADINGTON & RILEY: Appendix D, and FILE: /home/cs228/public/doc/assertion-notation.txt 5. (suggested practice) Do some of the exercises in Headington and Riley's book for chapter 1 (pp. 50-52). I suggest exercises 1.10-11(a,c,e), 1.12(a). Do what parts seem useful to you, then look at the answers, which are at the end of the book, in pages A-171 to A-172. 6. (20 points) Write a specification for each of the functions in Headington and Riley's exercise 1.8, on pages 50-51 of Headington and Riley's book. This should be done in the style used in class, not as in the book. (See /home/cs228/public/doc/assertion-notation.txt for our changes.) Bring your solutions on paper to your discussion section, where you'll discuss them with another class member, and will have an opportunity to ask questions and discuss solutions with the class as a whole. So be sure to write your program clearly. We won't check the details of your work, as that will be the subject of the discussion. So don't worry (too much) if it's not exactly right.