This algorithm will add the searched element's place in an array
This works only when there is so element what we're searching for.
By FlamingClaw

102 Views
``````{
Question
This algorithm will add the searched element's place in an array
This works only when there is so element what we're searching for.

u is a variable
i:=1
Loop while T[i]<>u
i:=i+1
End of Loop
Out: i

}

Program Solution01;
Uses Crt;
Var i,j,k,u:Integer;
t:Array[1..10]Of Integer;
Begin {main}
Write('Give me a number between 1 and 50: ');
Randomize;
t:=u; {10.element will the user's answer}
{we need only 9 numbers}
For i:=1 To 9 Do Begin
t[i]:=Random(50)+1; {1..50}
End;
WriteLn;
{direct arrange the numbers of the array}
For i:=1 To 9 Do Begin
For j:=i+1 To 10 Do Begin
If (t[i]>t[j])Then Begin
k:=t[i];
t[i]:=t[j];
t[j]:=k;
End;
End;
End;
{write the arranged results}
{we write green colored the searched number that we check the
working of our algorithm}
For i:=1 To 10 Do Begin
If t[i]=u Then Begin
TextColor(10);
WriteLn('t[',i:2,']:=',t[i]:2);
End
Else Begin
TextColor(15);
WriteLn('t[',i:2,']:=',t[i]:2);
End;
End;
{the algorithm}
i:=1;
While (t[i] <> u) Do Begin
i:=i+1;
End;
WriteLn;
Write(i,'. The first occurance');
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=-
}``````