C Primer Plus (5th Edition)

   

Our alien was able to take off with the spaceship in this case. But what if he was unable to leave due to an unknown error. Well, with a laser gun pointed at our heads by an enraged alien, we would have to consider several possibilities quickly. By knowing the three different types of errors described below, we would certainly be able to locate the problem faster.

Unfortunately, it looks as if we have, in fact, made an error in the pseudocode that could easily cause a runtime error. Consider a scenario where the list of numbers is empty. We then end up with Sum and ListSize being zero, dividing zero by zero in step 4. This is considered an invalid operation by the computer, and you will receive the following message, "The application has generated an exception that could not be handled." Fortunately, all the calculations in the spaceship must have involved lists of one or more numbers for it to take off. But we might not be as lucky next time.

Note

The test performed by the alien of the "average calculating" algorithm by using the list { 1,2,3,4,5} seemed to ascertain the correctness of the algorithm. However, we have just seen one instance proving the algorithm to be faulty. This is a good example of how delicate software testing is.

It is impossible to prove that a program is correct simply by testing it. On the other hand, a program can be proven to be incorrect if errors are detected.


   

Категории