Chapter 5 Assignment Page 1

Trailer Record Loop

NOTE: The problems in this chapter cover loops that are used when the total number of input records is unknown. The loops are used when there are many input records or the number of input records is constantly changing. There is no counter required to keep track of the number of processed records. The problems cover modules, and multiple page reports.

A trailer record loop uses a trailer record to tell the computer system when it is time to exit the loop. With a trailer record loop, a trailer record is created using a sentinel value. If the input file is given to you, check the end of the file for the sentinel value. If you are creating the input file, you need to create your own trailer record and sentinel value.

To create a properly formed trailer record loop you must:

    1. Read the first input record.
    2. Check for the sentinel value. This performs error checking and checks for an empty file. If the first record in the file has the sentinel value, then the file is empty and an error message should be printed. If the first record in the file does not have the sentinel value, then processing is continued.
    3. Process the record that was read.
    4. Print any necessary output.
    5. Read the next record. (The steps from step 2 are followed until the sentinel value is read.)
NOTE: The problem below is an example of a trailer record loop that will simply read a record and print the output. This will allow you to focus on the loop and not focus on processing the problem. Walk through the steps the computer will perform using a memory diagram.

Input File (Name and Age)

wpe12.gif (977 bytes)

wpe18.gif (864 bytes)

On the trailer record, you only need to list the sentinel value, if the remaining fields are character fields. For example, the name field can be left blank because spaces have a character value in the computer system. If the remaining fields are numeric, then some value must be placed in the field. Usually zeros are used since the value of the field does not matter.

Pseudocode Example:

wpe13.gif (977 bytes)
 

Flowchart Example:

wpe10.gif (5771 bytes)
 
  1. Heading, detail and total lines
NOTE: The solutions for the problems in this chapter include total lines. It will take a lot of practice before you will understand how to calculate and output totals. The output on page 307 to illustrate heading, detail and total lines.
Heading Line – a heading line serve as a title to a report that is output. Headings are almost always used on business reports to identify them to readers. A report might include a report heading such as "PAYROLL REPORT" or "DEFECTIVE PARTS REPORT". It might also include one or more column headings such as "NAME", "HOURS", "RATE" and so on. It is not usually necessary to define the headings in the early stages of algorithm development. Only the indication that heading are needed should be included in the design at this point. Notice that headings are output at the beginning of the processing, therefore, this output is not included in the loop. The write statement is placed before the loop is performed.

Detail Line – a detail line will print out the information in the body of the report. There is usually one detail line printed for each input record read. Since the detail line is output more than once, the write statement for the detail line is place inside the loop.

Total Line – a total line is printed at the end of the report. Total lines are usually output after the loop is exited. These lines will output the results of some accumulated numeric field. For example: suppose you want to total and output the total salary for all employees at a particular company. The processing for the totals are placed inside the loop, since the salary for each individual person is added to the total salary location each time the loop is performed. The output for the total line is placed after the loop is processed. This location is where the loop is exited and before the stop statement.

Pseudocode Example:

wpe14.gif (977 bytes)
 
 

Flowchart Example:

wpe11.gif (4157 bytes)
Exercises:

NOTE: Before you begin solving the problems, list the variables for the input, detail line output, total line output. You will also need to list the processing steps for the detail line and total line. Once this step is finished, create a sample input file and sample output using the input.

Problem 5 – This problem uses a design you created without a loop and adds a trailer record loop. Create sample data for the file and add the sentinel value. Walk the sample input through the design solution.

Problem 8 - This is the first problem the you will solve that involves totals. You also need to compute an average. Anytime you need to compute an average you will need to two pieces of information to compute the average. For example, to compute the average of your grades you need to know the total of the grades and the total number of grades. Sometimes the information you need to compute the average is already computed as a total, otherwise you will need to compute the totals to calculate the average. Create a sample input file and walk through the processing before the students create the design. Once the design is created, walk the data through the design. If you spend a lot of time on this problem, the other problems will be easier to solve.

Problem 10 – This is the easiest trailer record problem. It is the best problem to start with. The room number 000 on the trailer record is a character value, since the room number can contain letter, and needs to be placed inside quotation marks.

Problem 13 – This is a difficult problem for you to solve. You need to add to the total for "rate of $50 per hour" on both the true and false path of the decision statement. Remember that only the true path statements or the false path statements are followed, never both.