Jumat, 22 Maret 2013

Counting

        
         Counting students who passed the exam. With values ranging between 0-100. the requirement to pass value of more than 50.
         suppose there are seven students with each value
                         70,48,60,69,32,90,85
To know how many students that passed the examination, first, we have to make inizialitation of count start from 0. We need to check required to pass. if student marks as required to pass, then add 1 to count process. And if student marks not fulfill required to pass, add 0 to count process.

Marks Counting detail for passes
70 previous count=0 current count=1
48 previous count=1 current count=1
60 previous count=1 current count=2
69 previous count=2 current count=3
32 previous count=3 current count=3
90 previous count=3 current count=4
85 previous count=4 current count=5

Algorithm Description

  1. Request and read the number of marks.
  2. Initialize count=0.
  3. Process  repeatedly for all marks: (a). Read next mark, (b). if mark>50, count as a pass and add 1 to count.
  4. write out total number of passes.

Pascal Implementation 


program passcount {input,output};
const passmark=50;
var count{contains number of passes on termination}
       i{current number of mark processed}
      m{current mark}
      n{total number of marks processed}: integer;
begin {count the number of passes (>=50) in a set of marks}
      writeIn ('enter a number of marks n on a separate line followed by the marks');
      readIn (n);
      {assert:n>=0}
      count := 0;
      i := 0;
      {invariant: count=number of marks in the first i read that are >= passmark ^i=<n}
       while i<n do
             begin{read next mark, test it for pass and update count if necessary}
               i := i+1;
               read(m);
               if eoIn (Input)then readIn;
               if m>=passmark then count := count+1
             end;
       {assert: count=number of passes in the set of n marks read}
       writeIn ('number of passes=',count)
end.

Tidak ada komentar:

Posting Komentar