CS21 Homework 4
Due Sunday, September 21 by 11:30pm


Introduction

For this assignment your job is to write a program that emphasizes function decomposition in the style of the calendar.c program from chapter 5 of our textbook. Before you write any code, it is important to sit down and think about what needs to be done and how to organize your solution strategy into a well-structured program.

Because this is the largest program we have written to date, I encourage you to work with a partner for this assignment.


A Check Writing Program

Companies often use computers to generate checks. Suppose we needed a check for $1,234.56 made out to Widgets. Your program should be able to write out a check that looks like this:

..................................................................

                                               09-14-03       1001

Pay to the order of: Widgets                            $  1234.56
Amount: one thousand two hundred thirty-four and 56/100


                                         _________________________

..................................................................
The main program should allow the user to write several checks in a row. At the beginning of the program, the user should be asked for:

For all checks written in the current session, the program should use the same date and should number the checks consecutively from the starting point specified by the user.

For each check, the program should requrest:

After reading in this data, the program should write out a diagram of the check, filling in each of the fields in the appropriate format. When the user enters a zero as the amount of a check, the program should interpret that value as a sentinel indicating the end of the session.

A complete sample run of the program that writes out three check is shown below.

Enter the starting check number: 1001
Enter the date: 09-16-03
Enter the amount and payee for each check.
Use 0 to exit from the program.

Amount of check (0 to stop): 134.12
Pay to the order of: Swarthmore Coop             

..................................................................

                                               09-16-03       1001

Pay to the order of: Swarthmore Coop                    $   134.12
Amount: one hundred thirty-four and 12/100                          


                                         _________________________

..................................................................

Amount of check: 17775
Pay to the order of: Swarthmore College

..................................................................

                                               09-16-03       1002

Pay to the order of: Swarthmore College                 $ 17775.00
Amount: seventeen thousand seven hundred seventy-five and 00/100


                                         _________________________

..................................................................

Amount of check: 18.41
Pay to the order of: Swarthmore Pizza

..................................................................

                                               09-16-03       1003

Pay to the order of: Swarthmore Pizza                   $    18.41
Amount: eigthteen and 41/100                            


                                         _________________________

..................................................................

Amount of check: 0
End of program

Getting Started

When you write your program, the output should appear in precisely the format shown above. For example, your program is responsible for displaying the two lines of periods that separate each check from the input data and for aligning the fields so that the same fields appear in the same places on each check. In the sample run shown, each of the dividing lines is 66 periods long. The other fields are arranged to align against one side of the check or the other, except for the date, which ends in column 55. The signature line is 25 underscore characters; the underscore appears above the hyphen on the keyboard. For background on how to align these fields, you should re-read section 3.5 of the textbook, which covers everything you need.

The hard part of the program, and the part that will take both the most thinking and the most decomposition, is translating the numeric check amount into its English form. The largest check that your program needs to handle is $999,999.99. (Remember to use #define to set up important constants, like the maximum check amount.) Your program must break the number into pieces and translate each piece into words through a series of procedure calls. In many cases, the procedures will have a form similar to:

void PrintOneDigit(int n) {
  switch (n) {
  case 0: printf("zero"); break;
  case 1: printf("one"); break;
  case 2: printf("two"); break;
  case 3: printf("three"); break;
  case 4: printf("four"); break;
  case 5: printf("five"); break;
  case 6: printf("six"); break;
  case 7: printf("seven"); break;
  case 8: printf("eight"); break;
  case 9: printf("nine"); break;
  default: Error("Illegal call to PrintOneDigit");
  }
}

The interesting question, however, is how to assemble the different procedures into a program that handles all parts of the number. Be on the lookout for functions that you can reuse. For example, displaying the first three digits of a six-digit number (the number of thousands) is closely related to displaying the last three digits, and you should avoid duplicating as much code as possible.

Remember to apply the process of stepwise refinement when developing your program. You should begin thinking wholistically first. Start with a very general main program. Decompose the main program into separate functions that implement parts of the solution. Then begin coding each function, which may require further decomposition. Continue this until you have implemented all of the necessary functions.


Extra Credit

You may have noticed that for large check amounts, the translation of the amount into words does not fit in the allotted space on the check. Fix your program so that it can handle these large amounts properly. For smaller amounts, it should continue to print the amount on a single line. Keep in mind that the number of lines in the check must remain fixed. For example, below is a check with a large amount written so that it does not overflow the width of the check.

..................................................................

                                               09-10-03       1007

Pay to the order of: Lisa Meeden                        $199876.34
Amount: one hundred ninety-nine thousand 
        eight hundred seventy-six and 34/100

                                         _________________________

..................................................................

Handing in your solution

Use cs21handin to turn in your program. Handin has been updated to record who your partner was for the assignment. If you worked with a partner, only one of you needs to turn in the program, but both of you should use cs21handin to indicate that you were partners. If you worked alone, you should indicate this as well.