Lets Begin the summary!
Problem
- Given a set of n students' examination marks(0-100). The students with mark 50 above declared passed the examination. Make count of students who passed the examination!
Algorithm development
Suppose that we are given the set of students marks60, 39, 82, 59, 28, 64, 91
To make count of the passes students, we can start from the left. First mark is 60. We know that if mark >50, it means that students passed the examination. Now we have a student that passed. Remember it! The second mark are less than 50, it means that students not passed, and we dont need to count it. We still have 1 student passed. Look at the third mark, 82 is clearly passed examination. we can count it with the first one that already count. Process continues are similar like 3 students marks first until the last student.
Here are detail of count process:
Marks >50=Pass Counting details for passes
60 +1 (previous count=0)+1, current count=1
39 +0 (previous count=1)+0, current count=1
82 +1 (previous count=1)+1, current count=2
59 +1 (previous count=2+1), current count=3
28 +0 (previous count=3+0), current count=3
64 +1 (previous count=3+1), current count=4
91 +1 (previous count=4+1), current count=5
Algorithm Description
- Request and read the number of marks.
- Initialize count=0.
- Process repeatedly for all marks: (a). Read next mark, (b). if mark>50, count as a pass and add 1 to count.
- 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