Counting

Author Adhitya - -
Home » , » Counting



Counting

Problem

Given a set of n students' examination marks (in the range 0 to 100) make a count of the number of students that passed the examination. A pass is awarded for all marks of 50 and above.


Algorithm development
Counting mechanisms are very frequently used in computer algorithms. Generally a count must be made of the number of items in a set which possess some particular property or which satisfy some particular condition or conditions. This class of problems is typified by the "examination marks" problem.

As a starting point for developing a computer algorithm for this prob­lem we can consider how we might solve a particular example by hand.

Suppose that we are given the set of marks
55, 42, 77, 63, 29, 57, 89


Current_count therefore becomes 4. Similarly when we get to the next mark (i.e. 89) the current_count of 4 must assume the role of previous_count. This means that whenever a new current_count is generated it must then assume the role of previous _count before the next mark is considered. The two steps in this process can be represented by



Before any marks have been examined the count must have the value zero. To complete the algorithm the input of the marks and the output of the number of passes must be included. The detailed algorithm is then as described below.




Algorithm description
1.    Prompt then read the number of marks to be processed.
2.    Initialize count to zero.           
3    While there are still marks to be processed repeatedly do
(a)    read next mark,
(b)    if it is a pass (i.e. >=50) then add one to count.
4.    Write out total number of passes.


Notes on design
1.    Initially, and each time through the loop, the variable count represents the number of passes so far encountered. On termination (when n) count represents the total number of passes in the set. Because i is incremented by 1 with each iteration, eventually the condition iwill be violated and the loop will terminate.
2.     It was possible to use substitution to improve on the original solution to the problem. The simplest and most efficient solution to a problem is usually the best all-round solution.
3.    An end-of-line test is included to handle multiple lines of data.