|
|
|
|
|
|
|
Outer Loop Design. The loop exits when a sentinel value of 0 or less is input for an item quantity. The termination condition is initialized by opening the file inFile and performing a priming read of the quantity. The condition is updated by reading a new quantity at the end of each iteration. The process is to read and print the data for an item, and to add the quantity and dollar amount for the item to running totals. The process is initialized by setting the totals to 0. It is updated by reading the data for an item, printing a line of the invoice, and adding the appropriate values to the totals. |
|
|
|
|
|
|
|
|
When the loop ends, the following is true: The reading marker is positioned just beyond the sentinel value in the input file; an invoice line has been printed for each item; quantity contains the last quantity input; price contains the last price input; inputChar contains the last character in the final description; blank contains the character separating the last quantity and description; amount contains the price times the quantity of the last item ordered; counter equals 31; and the quantity of items ordered and their costs have been summed in totalUnits and totalAmount, respectively. |
|
|
|
|
|
|
|
|
Inner Loop Design: The loop exits when the loop control variable exceeds 30. The loop control variable is initialized to 1 and incremented at the end of each iteration. The process is to read and print one character. No initialization or update is necessary because the reading and writing markers advance automatically. At loop exit, the reading marker should be on the character following the item description; the item description should be printed on the current line; and the loop control variable should be equal to 31. |
|
|
|
|
|
|
|
|
In addition to printing an invoice line for each item, we must print headings at the top of the page and above each column. Programs that use loops often produce large amounts of output, and it is common to organize that output in columns with headings. |
|
|
|
|
|
|
|
|
The first step in designing columnar output is to determine what is going to be printed. The next step is to make a sketch on graph paper of how the output should look. Line up the headings and put in some actual values. Once you have an acceptable design, count the number of character positions associated with each heading or data value. |
|
|
|
|
|
|
|
|
Figure 6-3 shows a sample design for the output here. From it we can determine the appropriate fieldwidths for aligning headings with columns of values. |
|
|
|
|
|
|
|
|
We now have enough information to write the algorithm: |
|
|
|
|
|