Searching for a number in an array

FlamingClaw 0 Tallied Votes 2K Views Share

This algorithm will decide that is there a serched element
in the array.If it finds one then the loop halts.If the loop
halts cause we're stepping over the last element of the array then
there is not searched element.
By FlamingClaw

{
Question

This algorithm will decide that is there a serched element
in the array.If it finds one then the loop halts.If the loop
halts cause we're stepping over the last element of the array then
there is not searched element.

Is there 'u' in the array?
u is a variable.

i:=1
While i<=N és T[i]<>u
i:=i+1
End of while
If i<=N Then out: there is,u
Else out:there isn't,u

Note that important the order of conditions!!!

}

Program Solution01;
Const n=20;
Var i,u:Integer;
    T:Array[1..n]Of Integer;
Begin {main}
   Write('Give me a number between 1 and 20: ');
   ReadLn(u);
   Randomize;
   For i:=1 To n Do Begin
     T[i]:=Random(n)+1;
     Write(T[i],',');
   End;
   WriteLn;
   i:=1;
   While (i <= n) And (T[i]<>u) Do Begin
      i:=i+1;
   End;
   If i<=n Then Write('There is: ',u)
   Else Write('There is not: ',u);
   ReadLn;
End.

{
-= Note By FlamingClaw =-

All programs created by me are written and tested
in Dev Pascal and/or Turbo Pascal 7.0
Of course working!
-= Created By FlamingClaw =-
-=2009.04.05=-
}