< previous page page_288 next page >

Page 288
erwise, an income is assumed to be for a male, so maleCount is incremented, and the amount is added to maleSum.
7. What is the state of the program on exiting the loop? The file stream incFile is in the fail state; femaleCount contains the number of input values preceded by F; femaleSum contains the sum of the values preceded by F; maleCount contains the number of values not preceded by F; and maleSum holds the sum of those values.
From the description of how the process is updated, we can see that the loop must contain an If-Then-Else structure, with one branch for female incomes and the other for male incomes. Each branch must increment the correct event counter and add the income amount to the correct total. After the loop has exited, we have enough information to compute and print the averages, dividing each total by the corresponding count.
Assumptions: There is at least one male and one female among all the data sets. The only gender codes in the file are M and Fany other codes are counted as M. (This last assumption invalidates the results if there are any illegal codes in the data. Programming Warm-Up Exercise 11 asks you to change the program as necessary to address this problem.)
Now we're ready to write the complete algorithm:
Main Module Level 0
Separately count females and males, and sum incomes
Compute average incomes
Output results

Separately Count Females and Males, and Sum Incomes Level 1
Initialize ending condition
Initialize process
WHILE NOT EOF on incFile
 Update process
 Update ending condition

Compute Average Incomes
Set femaleAverage = femaleSum / femaleCount
Set maleAverage = maleSum / maleCount

 
< previous page page_288 next page >