|
|
|
|
|
|
|
Testing: With an EOF-controlled loop, the obvious test cases are a file with data and an empty file. We should test input values of both F and M for the gender, and try some typical data (so we can compare the results with our hand-calculated values) and some atypical data (to see how the process behaves). An atypical data set for testing a counting operation is an empty file, which should result in a count of zero. Any other result for the count indicates an error. For a summing operation, atypical data might include negative or zero values. |
|
|
|
|
|
|
|
|
The Incomes program is not designed to handle empty files or negative income values. An empty file causes both femaleCount and maleCountt to equal zero at the end of the loop. Although this is correct, the statements that compute average income cause the program to crash because they divide by zero. And a negative income would be treated like any other value, even though it is probably a mistake. |
|
|
|
|
|
|
|
|
To correct these problems, we should insert If statements to test for the error conditions at the appropriate points in the program. When an error is detected, the program should print an error message instead of carrying out the usual computation. This prevents a crash and allows the program to keep running. We call a program that can recover from erroneous input and keep running a robust program. |
|
|
|
|
|
|
|
|
Problem-solving case study High and Low Temperatures |
|
|
|
|
|
|
|
|
Here's another problem in loopdesign. In this case we design a countcontrolled loop that finds the minimum and maximum values in a data set. |
|
|
|
|
|
|
|
|
Problem: A heating oil company uses the temperature range for each day to determine its customers' typical oil use and to schedule deliveries. The firm has hired you to take hourly outdoor temperature readings for each 24-hour period and find the day's high and low temperatures from this data. Because you won't be getting much sleep on this job, you decide that it would be a good idea to have the computer keep track of the maximum and minimum values. |
|
|
|
|
|
|
|
|
Input: Twenty-four integer numbers representing hourly temperatures. |
|
|
|
|
|