Pages

Jumat, 22 Maret 2013

tugas alpro -3


Counting
Problem
Given a set of nstudent’s 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 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.

Pascal implementation
Program passacount (input, output);
Conts passmark=50;
Var count {contains number of passes on terminitation},
    i {current number of marks processed},
   m {current mark},
   n {total number of marks to be processed}: integer;
begin {count the number of passed (>=50) in a set of marks}
   writeln (‘enter a number of marks n on a separate line followed by the  marks’);
  readln (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 eoln (input) then readln;
if m >= passmark then count := count +1
end;
{assert: count = number of passes in the set of n marks read}
Writeln (‘number of passes =’, count)
End.

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 i=n) count represents the total number of passes in the set. Because i is incremented by 1 with each iteration, eventually the condition i<n will 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.
Applications
All forms of counting.

Tidak ada komentar:

Posting Komentar