recursive routines

Reply

Join Date: Feb 2008
Posts: 1
Reputation: marlow08 is an unknown quantity at this point 
Solved Threads: 0
marlow08 marlow08 is offline Offline
Newbie Poster

recursive routines

 
0
  #1
Feb 18th, 2008
I don't know why the next program doesn't find an element of the array.

  1.  
  2. program rec;
  3.  
  4. {$APPTYPE CONSOLE}
  5.  
  6. uses
  7. SysUtils;
  8.  
  9.  
  10. procedure intarray(a:array of integer;longar:integer);
  11.  
  12. begin
  13. if longar>=0 then begin
  14. write(a[longar],' ');
  15. intarray(a,longar-1);
  16. end;
  17. end;
  18.  
  19. procedure exist (a:array of integer;num2,longar:integer;found:integer);
  20.  
  21. begin
  22. if longar>1 then begin
  23. if a[longar]=num2 then found:=1
  24. else if longar=1 then found:=0
  25. else exist(a,num2,longar-1,found);
  26. end;
  27. end;
  28.  
  29.  
  30.  
  31. var
  32. num2,I,longar,found:integer;
  33. a:array[0..9] of integer;
  34.  
  35. begin
  36.  
  37.  
  38. For I:=0 to 2 do begin
  39. a[I]:=2;
  40. end;
  41.  
  42. For I:=3 to 5 do begin
  43. a[I]:=4;
  44. end;
  45.  
  46. For I:=6 to 7 do begin
  47. a[I]:=1;
  48. end;
  49.  
  50. For I:=8 to 9 do begin
  51. a[I]:=3;
  52. end;
  53.  
  54. longar:=9;
  55. found:=0;
  56.  
  57. intarray(a,longar);
  58. writeln('');
  59. write('write the number you want to find: ');
  60. readln(num2);
  61. exist(a,num2,longar,found);
  62. if found=1 then writeln('found');
  63. if found=0 then writeln('not found');
  64.  
  65.  
  66. readln;
  67.  
  68. end.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: recursive routines

 
0
  #2
Feb 18th, 2008
You have a couple of problems.

Your "display the array" routine (intarray) is perfect. Your "search the array" routine (exist) should look almost exactly like it. (But it doesn't).

The found variables should be boolean. Also, the found parameter should be a var parameter. The procedure type should be:
procedure exist (a: array of integer; longar, num2find: integer; var found: boolean);

Frankly, I'd make it a function instead:
function exist (a: array of integer; longar, num2find: integer): boolean;


Also, a couple of suggestions:
It is OK to say just writeln;.

At the bottom, use an if..else statement:
  1. if found
  2. then writeln( 'Found' )
  3. else writeln( 'Not Found );
  4.  

Hope this helps.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Pascal and Delphi Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC