Programming and Software Development: Question 6
Syllabus 12.3
A program is intended to read in a list of numbers entered by the user, one at a time, adding
each number to a running Total and counting how many numbers have been entered in Count,
until the user enters -1 to stop. Once this loop finishes, the pseudocode below calculates and
outputs the average of the numbers entered.
DECLARE Total : INTEGER
DECLARE Count : INTEGER
DECLARE Average : REAL
Total ← 0
Count ← 0
// ... a loop here reads numbers from the user, adding each one to Total and
// adding 1 to Count, until the user enters -1 to stop ...
Average ← Total / Count
OUTPUT Average
This pseudocode translates successfully, and the program runs correctly for a user who enters
several numbers before -1. However, if a user enters -1 immediately, without entering any
numbers first, the program crashes when it reaches the line Average ← Total / Count.
Which one of the following best describes the type of error that occurs in this situation, and when it is detected?
Show worked solution Hide worked solution
Worked solution
Why option C is correct
The statement Average ← Total / Count is a perfectly well-formed pseudocode statement. It
follows every grammar rule of the language, so the program translates successfully with no
problem at all. The issue only appears while the program is actually running: if the user enters
-1 immediately, the loop that would normally add to Total and Count never executes, so
Count is still 0 when Average ← Total / Count is reached, and dividing by zero cannot be
carried out.
Because this failure depends entirely on the specific data supplied while the program is executing, whether or not the user happens to enter -1 straight away, and can only be detected once the program actually tries to carry out this calculation, it is a runtime error.
Why the other options are wrong
- A (syntax error): a syntax error is detected during translation, before the program ever
runs, because a line breaks the grammar rules of the language.
Average ← Total / Countbreaks no grammar rule at all. It translates successfully, and the problem only shows up once the program is executing with a particular set of data. - B (logic error): a logic error means a program runs successfully but produces the wrong
result because of a flaw in the reasoning behind the algorithm. The formula
Total / Countis the mathematically correct way to calculate an average, and it gives a correct result for any value ofCountother than 0. There is no flaw in the formula itself. - D (no error): a translator strictly translates the program as written; it does not monitor
or silently correct the values variables happen to hold while the program is running, so
nothing prevents
Countfrom actually being 0 when this line executes.
Final answer
- C. Dividing by zero can only be detected once the program is actually running and
attempts this specific calculation with
Count = 0, so this is a runtime error, not a syntax error (which would be caught during translation) or a logic error (which would still allow the program to run, just with a wrong result).