Searched element's place in an array

Please support our Pascal and Delphi advertiser: Programming Forums - DaniWeb Sister Site
FlamingClaw FlamingClaw is offline Offline Apr 5th, 2009, 8:24 am |
0
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
Quick reply to this message  
Pascal and Delphi Syntax
  1. {
  2. Question
  3. This algorithm will add the searched element's place in an array
  4. This works only when there is so element what we're searching for.
  5.  
  6. u is a variable
  7. i:=1
  8. Loop while T[i]<>u
  9. i:=i+1
  10. End of Loop
  11. Out: i
  12.  
  13. }
  14.  
  15. Program Solution01;
  16. Uses Crt;
  17. Var i,j,k,u:Integer;
  18. t:Array[1..10]Of Integer;
  19. Begin {main}
  20. Write('Give me a number between 1 and 50: ');
  21. ReadLn(u);
  22. Randomize;
  23. t[10]:=u; {10.element will the user's answer}
  24. {we need only 9 numbers}
  25. For i:=1 To 9 Do Begin
  26. t[i]:=Random(50)+1; {1..50}
  27. End;
  28. WriteLn;
  29. {direct arrange the numbers of the array}
  30. For i:=1 To 9 Do Begin
  31. For j:=i+1 To 10 Do Begin
  32. If (t[i]>t[j])Then Begin
  33. k:=t[i];
  34. t[i]:=t[j];
  35. t[j]:=k;
  36. End;
  37. End;
  38. End;
  39. {write the arranged results}
  40. {we write green colored the searched number that we check the
  41.   working of our algorithm}
  42. For i:=1 To 10 Do Begin
  43. If t[i]=u Then Begin
  44. TextColor(10);
  45. WriteLn('t[',i:2,']:=',t[i]:2);
  46. End
  47. Else Begin
  48. TextColor(15);
  49. WriteLn('t[',i:2,']:=',t[i]:2);
  50. End;
  51. End;
  52. {the algorithm}
  53. i:=1;
  54. While (t[i] <> u) Do Begin
  55. i:=i+1;
  56. End;
  57. WriteLn;
  58. Write(i,'. The first occurance');
  59. ReadLn;
  60. End.
  61.  
  62. {
  63. -= Note By FlamingClaw =-
  64.  
  65. All programs created by me are written and tested
  66. in Dev Pascal and/or Turbo Pascal 7.0
  67. Of course working!
  68. -= Created By FlamingClaw =-
  69. -=2009.04.05=-
  70. }

Message:


Thread Tools Search this Thread



Tag cloud for Pascal and Delphi
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC