|
|
|
|
|
|
|
Testing: Program TempStat reads 24 integer values. We can test the While statement by entering data values until the program outputs the high and low temperatures. If the program doesn't input exactly 24 numbers, there must be an error in the control of the loop. |
|
|
|
|
|
|
|
|
We should try data sets that present the high and low temperatures in different orders. For example, we should try one set with the highest temperature as the first value and another set with it as the last. We should do the same for the lowest temperature. We also should try a data set in which the temperature goes up and down several times, and another in which the temperatures are all the same. Finally, we should test the program on some typical sets of data and check the output with results we've determined by hand. For example, given these data: |
|
|
|
|
|
|
|
|
45 47 47 47 50 50 55 60 70 70 72 75
75 75 75 74 74 73 70 70 69 67 65 50 |
|
|
|
|
|
|
|
|
the final output of the high and low temperatures would look like this: |
|
|
|
|
|
|
|
|
High temperature is 75
Low temperature is 45 |
|
|
|
|
|
|
|
|
What happens if we combine the two If-Then statements into one IfThen-Else, as shown below? |
|
|
|
|
|
|
|
|
if (temperature < low)
low = temperature;
else if (temperature > high)
high = temperature; |
|
|
|
|
|
|
|
|
At first glance, the single statement looks more efficient. Why should you ask if temperature is larger than high if you know it is lower than low? But this code segment gives the wrong answer if the highest temperature is the first value read in because of the way high and low are initialized. |
|
|
|
|
|
|
|
|
In Programming Warm-Up Exercise 8, you are asked to redo the TempStat program using an initialization scheme that removes this data dependency. (Hint: Use a priming read and set high and low to that first value.) |
|
|
|
|
|