Find Max Min Value

Posted on 18.06.2010

0


program max_min(Output);
uses crt;
var
   x : array[1..1000] of integer;
   i, n, min, max : integer;
   scndmax, scndmin : integer;
   moremax, moremin : integer;
begin
   clrscr;
   write(‘banyak data : ‘); readln(n);
   randomize;

{ .:: Input data ::. }
   for i:=1 to n do begin
      x[i]:=random(100)+1;
      write(x[i]:4,’ ‘);
      if (i mod 15=0) then writeln;
   end;
   writeln;
   writeln;

{ .:: Find max and min ::. }
   min:=x[1];
   max:=x[1];
   for i:=2 to n do begin
      if (x[i]max) then
         max:=x[i];
   end;

{ .:: Are there duplication for max and min ::. }
   moremax:=0;
   moremin:=0;
   for i:=1 to n do begin
      if (x[i]=min) then
         moremin:=moremin+1;
      if (x[i]=max) then
         moremax:=moremax+1;
   end;

   scndmax:=min;
   scndmin:=max;
// writeln(‘banyak nilai min : ‘, moremin);
// writeln(‘banyak nilai max : ‘, moremax);
   if (moremin>1) then
      scndmin:=min
   else begin
      for i:=1 to n do
         if ((x[i]min)) then
            scndmin:=x[i]
   end;

   if (moremax>1) then
      scndmax:=max
   else begin
      for i:=1 to n do
         if ((x[i]>scndmax) and (x[i]<max)) then
            scndmax:=x[i]
   end;

   writeln;
   writeln('nilai minimal : ',min);
   writeln('nilai minimal_2 : ',scndmin);
   writeln('nilai maximal : ',max);
   writeln('nilai maximal_2 : ',scndmax);
   readln;
end.

Posted in: Pascal Code