Next: Other forms of Repetition Up: Further Structured Design Previous: Example One: Using a


Example Two: Using a while loop

A set of marks are available for a class of students. For each student the following details are available:

a candidate number - 4 digits
a percentage examination mark for subject 1
a percentage coursework mark for subject 1
a percentage examination mark for subject 2
a percentage coursework mark for subject 2

A program is required which will read in the marks as above for each student and will output for each student, in one line, the above information together with a final mark for each subject. The final mark in each subject is 70% of the examination mark plus 30% of the coursework mark. The average final mark for each subject should also be output. End of input is to be signalled by input of a negative candidate number. A first algorithmic description of the required program is:

initialise.
enter candidate number.
while candidate number is positive
   {
    enter marks for candidate.
    process marks.
    output results for candidate.
    enter candidate number.
   }
calculate subject averages.
output subject averages.

A little thought shows that two accumulation variables will be required to sum the two final subject marks, and since an average has to be found the number of students must be counted. Initially these sums and the count must be initialised to zero and in process marks the marks must be added to the sums and the count incremented. Including these features the algorithmic description is expanded to:

set sum1 and sum2 to zero.
set count to zero.
enter candidate number.
while candidate number is positive
  {
    enter s1, cw1, s2, cw2.   // the four candidate marks
    increment count.
    set final1 to 0.7*s1+0.3*cw1.
    set final2 to 0.7*s2+0.3*cw2.
    add final1 to sum1.
    add final2 to sum2.
    output candidate number, s1, cw1, s2, cw2, final1, final2.
    enter candidate number.
  }
set subject1 average to sum1/count.
set subject2 average to sum2/count.
output subject1 average.
output subject2 average.

Note that this algorithm would fail if the first candidate number entered was negative. The statement loop in the while statement would not be executed and hence count would retain its initial value of zero. This would lead to a division by zero when an attempt was made to calculate the averages. It is often difficult to decide whether a program should cope gracefully with non-existent data, after all why would a user use the program if they had no data to enter? However a typing error may lead to the entry of unsuitable data and it is preferable that the program should recover gracefully from this situation rather than fail with an obscure run-time error later. This algorithm could do this by either checking the sign of the first candidate number entered and terminating with a suitable message if it is negative or could recognise the situation by checking that count is non-zero before proceeding to calculate the average.

This algorithm could be extended by adding various facilities to it, for example indicating for each student whether they have passed judged by some criteria. See the exercises for possible extensions for you to try.



Next: Other forms of Repetition Up: Further Structured Design Previous: Example One: Using a