Next: Example Program: Iterative evaluation Up: The while statement Previous: Example while loop: Average,

Example Program: Student mark processing

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.

This is then easily translated into the following program - the main function only is listed.

void main()
{
  int candno;           // candidate number
  int s1, cw1, s2, cw2; // candidate marks
  int final1, final2;   // final subject marks
  int count;            // number of students
  int sum1, sum2;       // sum accumulators
  int subav1, subav2;   // subject averages
  const float EXAMPC = 0.7, // mark weightings
              CWPC = 0.3;
    // initialise
  sum1 = 0;
  sum2 = 0;
  count = 0;
    // enter candidate number
  cout << "Input candidate number: ";
  cin >> candno;
  while (candno >= 0)
    {
        // enter marks
      cout << "Input candidate marks: ";
      cin >> s1 >> cw1 >> s2 >> cw2;
        // process marks
      count++;
      final1 = int(EXAMPC*s1 + CWPC*cw1);
      final2 = int(EXAMPC*s2 + CWPC*cw2);
      sum1 += final1;
      sum2 += final2;
        // output candidate number and marks
      cout << candno << " "
           << s1 << " " << cw1 << " "
           << s2 << " " << cw2 << " "
           << final1 << " " << final2
           << endl;
        // enter next candidate number
      cout << "Input candidate number (negative to finish): ";
      cin >> candno;
    }
    // evaluate averages
  subav1 = sum1/count;
  subav2 = sum2/count;
     // output averages
  cout << endl << "Subject1 average is " << subav1
       << endl << "Subject2 average is " << subav2
       << endl;
}
Download program.

Note that this program 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 program 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 program 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: Example Program: Iterative evaluation Up: The while statement Previous: Example while loop: Average,