|
|
|
|
|
|
|
All the input data (echo print) |
|
|
|
|
|
|
|
|
The number of females and their average income |
|
|
|
|
|
|
|
|
The number of males and their average income |
|
|
|
|
|
|
|
|
Discussion: The problem breaks down into three main steps. First, we have to process the data, counting and summing the salary amounts for each sex. Next, we compute the averages. Finally, we have to print the calculated results. |
|
|
|
|
|
|
|
|
The first step is the most difficult. It involves a loop with several subtasks. We'll use our checklist of questions to develop these subtasks in detail. |
|
|
|
|
|
|
|
|
1. What is the condition that ends the loop? The termination condition is EOF on the file incFile. It leads to the pseudocode While statement |
|
|
|
 |
|
|
|
|
WHILE NOT EOF on incFile |
|
|
|
|
|
|
|
|
2. How should the condition be initialized? We must open the file for input, and a priming read must take place. |
|
|
|
|
|
|
|
|
3. How should the condition be updated? We must input a new data line with a gender code and amount at the end of each iteration. Here's the resulting algorithm: |
|
|
|
 |
|
|
|
|
Open incFile for input (and verify the attempt)
Read sex and amount from incFile
WHILE NOT EOF on incFile
.
: (Process being repeated)
Read sex and amount from incFile |
|
|
|
|
|
|
|
|
4. What is the process being repeated? From our knowledge of how to compute an average, we know that we have to count the number of amounts and divide this number into the sum of the amounts. Because we have to do this separately for females and males, the process consists of four parts: counting the females and summing their incomes, and then counting the males and summing their incomes. We develop each of these in turn. |
|
|
|
|
|
|
|
|
5. How should the process be initialized? femaleCount and femaleSum should be set to zero. maleCount and maleSum also should be set to zero. |
|
|
|
|
|
|
|
|
6. How should the process be updated? When a female income is input, femaleCount is incremented, and the income is added to femaleSum. Oth- |
|
|
|
|
|