Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Binary Search Procedure with Recursive and non-Recursive

syhsainsyhsain Member Posts: 4
Hi all;
I'm a new member for this web site, and face a problem with Binary Search Procedure, there r 2 ways to code this procedure/function
1- as a Recursive
2- as non-Recursive

Is anyone knows what is the different between the Recursive and non-Recursive procedure? and what is the "shell" way of writting a non-recursive procedure? I'll be gladto hear from anyone of you soon
Thanx

Comments

  • bpajkbpajk Member Posts: 156
    : Hi all;
    : I'm a new member for this web site, and face a problem with Binary Search Procedure, there r 2 ways to code this procedure/function
    : 1- as a Recursive
    : 2- as non-Recursive
    :
    : Is anyone knows what is the different between the Recursive and non-Recursive procedure? and what is the "shell" way of writting a non-recursive procedure? I'll be gladto hear from anyone of you soon
    : Thanx
    :
    :
    Recursive Functions or Procedures call them selfs and non recusive Functions or procedures handle the problem with simple while do, repeat until or for do statements. Recursive functions or procedures are more difficult to understand because they calculate the result in the opposite orrder.
    Example:
    [CODE]
    program Facturial;
    var n: longint;
    //recursive function
    function RecursiveFacturial(x: longint): longint;
    begin
    if x>1 then
    RecursiveFacturial:=x*recursiveFacturial(x-1)
    else
    recursiveFacturial:=1
    end;
    //loop function
    function LoopFacturial(x: longint): longint;
    var temp: longint;
    begin
    temp:=1;
    for x:=x downto 1 do
    temp:=temp*x;
    LoopFacturial:=temp
    end;
    begin
    write('Input the number: ');
    readln(n);
    writeln(n,'!=',RecursiveFacturial(n));
    writeln(n,'!=',LoopFacturial(n));
    readln
    end.
    //compiled in Free Pascal
    [/CODE]
  • syhsainsyhsain Member Posts: 4
    : : Hi all;
    : : I'm a new member for this web site, and face a problem with Binary Search Procedure, there r 2 ways to code this procedure/function
    : : 1- as a Recursive
    : : 2- as non-Recursive
    : :
    : : Is anyone knows what is the different between the Recursive and non-Recursive procedure? and what is the "shell" way of writting a non-recursive procedure? I'll be gladto hear from anyone of you soon
    : : Thanx
    : :
    : :
    : Recursive Functions or Procedures call them selfs and non recusive Functions or procedures handle the problem with simple while do, repeat until or for do statements. Recursive functions or procedures are more difficult to understand because they calculate the result in the opposite orrder.
    : Example:
    : [CODE]
    : program Facturial;
    : var n: longint;
    : //recursive function
    : function RecursiveFacturial(x: longint): longint;
    : begin
    : if x>1 then
    : RecursiveFacturial:=x*recursiveFacturial(x-1)
    : else
    : recursiveFacturial:=1
    : end;
    : //loop function
    : function LoopFacturial(x: longint): longint;
    : var temp: longint;
    : begin
    : temp:=1;
    : for x:=x downto 1 do
    : temp:=temp*x;
    : LoopFacturial:=temp
    : end;
    : begin
    : write('Input the number: ');
    : readln(n);
    : writeln(n,'!=',RecursiveFacturial(n));
    : writeln(n,'!=',LoopFacturial(n));
    : readln
    : end.
    : //compiled in Free Pascal
    : [/CODE]
    :


    Thank u so much for ur clarify which make sence with my understanding the difference of recursion and non-recursion special with Binary Search procedure. The Binay Search will be a "function" if we use it as a Recursive, and will be a "Procedure" if it is non-recursive.
    once again; thank u so match u have been open my mind for alot of things regarding the Recursion study.
    Actually I have another Question; if we have a line of text includes extraneous characters, as well as integers. for example:
    25*-2g &&-+ad dd15wd
    How we can ignor the extraneous characters and extract the numeric characters, we should read the text line into string and then extract the numeric characters from string using Val function to determine if a given string character is the 1st character in a valid integer.
    if the value of the code parameter = 1 then the character is non-numeric, if it is > 1 then u r looking at the 1st character in a valid integer and the value of code indicates the position of of the 1st non-numeric character. To extract the value of the integer u must copy the numeric characters to another string and then use Val function to extract the new string's numeric value. if the value of code variable = zero, then a valid integer has been read.

  • bpajkbpajk Member Posts: 156
    : : : Hi all;
    : : : I'm a new member for this web site, and face a problem with Binary Search Procedure, there r 2 ways to code this procedure/function
    : : : 1- as a Recursive
    : : : 2- as non-Recursive
    : : :
    : : : Is anyone knows what is the different between the Recursive and non-Recursive procedure? and what is the "shell" way of writting a non-recursive procedure? I'll be gladto hear from anyone of you soon
    : : : Thanx
    : : :
    : : :
    : : Recursive Functions or Procedures call them selfs and non recusive Functions or procedures handle the problem with simple while do, repeat until or for do statements. Recursive functions or procedures are more difficult to understand because they calculate the result in the opposite orrder.
    : : Example:
    : : [CODE]
    : : program Facturial;
    : : var n: longint;
    : : //recursive function
    : : function RecursiveFacturial(x: longint): longint;
    : : begin
    : : if x>1 then
    : : RecursiveFacturial:=x*recursiveFacturial(x-1)
    : : else
    : : recursiveFacturial:=1
    : : end;
    : : //loop function
    : : function LoopFacturial(x: longint): longint;
    : : var temp: longint;
    : : begin
    : : temp:=1;
    : : for x:=x downto 1 do
    : : temp:=temp*x;
    : : LoopFacturial:=temp
    : : end;
    : : begin
    : : write('Input the number: ');
    : : readln(n);
    : : writeln(n,'!=',RecursiveFacturial(n));
    : : writeln(n,'!=',LoopFacturial(n));
    : : readln
    : : end.
    : : //compiled in Free Pascal
    : : [/CODE]
    : :
    :
    :
    : Thank u so much for ur clarify which make sence with my understanding the difference of recursion and non-recursion special with Binary Search procedure. The Binay Search will be a "function" if we use it as a Recursive, and will be a "Procedure" if it is non-recursive.
    : once again; thank u so match u have been open my mind for alot of things regarding the Recursion study.
    : Actually I have another Question; if we have a line of text includes extraneous characters, as well as integers. for example:
    : 25*-2g &&-+ad dd15wd
    : How we can ignor the extraneous characters and extract the numeric characters, we should read the text line into string and then extract the numeric characters from string using Val function to determine if a given string character is the 1st character in a valid integer.
    : if the value of the code parameter = 1 then the character is non-numeric, if it is > 1 then u r looking at the 1st character in a valid integer and the value of code indicates the position of of the 1st non-numeric character. To extract the value of the integer u must copy the numeric characters to another string and then use Val function to extract the new string's numeric value. if the value of code variable = zero, then a valid integer has been read.
    :
    :
    I would do it somehow like so:
    [CODE]
    program test;
    var s,
    IntS: string;
    i: byte;
    int: longint;
    begin
    IntS:='';
    i:=1;
    write('Input string: ');
    readln(s);
    while i<=length(s) do
    begin
    if (ord(s[i])>=48) and (ord(s[i])<=57) then
    IntS:=IntS+s[i];
    inc(i)
    end;
    writeln('String: ',IntS);
    val(IntS, int);
    writeln('Integer: ',int);
    readln
    end.
    [/CODE]
    You have to implement the support for the "-" character. It does not support it now.
  • badingbading Member Posts: 9
    : : Hi all;
    : : I'm a new member for this web site, and face a problem with Binary Search Procedure, there r 2 ways to code this procedure/function
    : : 1- as a Recursive
    : : 2- as non-Recursive
    : :
    : : Is anyone knows what is the different between the Recursive and non-Recursive procedure? and what is the "shell" way of writting a non-recursive procedure? I'll be gladto hear from anyone of you soon
    : : Thanx
    : :
    : :
    : Recursive Functions or Procedures call them selfs and non recusive Functions or procedures handle the problem with simple while do, repeat until or for do statements. Recursive functions or procedures are more difficult to understand because they calculate the result in the opposite orrder.
    : Example:
    : [CODE]
    : program Facturial;
    : var n: longint;
    : //recursive function
    : function RecursiveFacturial(x: longint): longint;
    : begin
    : if x>1 then
    : RecursiveFacturial:=x*recursiveFacturial(x-1)
    : else
    : recursiveFacturial:=1
    : end;
    : //loop function
    : function LoopFacturial(x: longint): longint;
    : var temp: longint;
    : begin
    : temp:=1;
    : for x:=x downto 1 do
    : temp:=temp*x;
    : LoopFacturial:=temp
    : end;
    : begin
    : write('Input the number: ');
    : readln(n);
    : writeln(n,'!=',RecursiveFacturial(n));
    : writeln(n,'!=',LoopFacturial(n));
    : readln
    : end.
    : //compiled in Free Pascal
    : [/CODE]
    :
    recursive functions are used to call to itself.they are using an activation frame known as stacks or in data structure known as LIFO (last in first out).It is not recommended when involving a large amount of data.The program becomes slower when processing a large amount of data,just like an example above,the factorial function.Enter a very large amount of integre and you will see the running time of your program.So it is rational enough when you are going to use non recursive function & recursive function.Just study the different data structures by AHO,ULLMAN,& HOLLCROFT or by WALTER SAVITCH.
  • badingbading Member Posts: 9
    : : : : Hi all;
    : : : : I'm a new member for this web site, and face a problem with Binary Search Procedure, there r 2 ways to code this procedure/function
    : : : : 1- as a Recursive
    : : : : 2- as non-Recursive
    : : : :
    : : : : Is anyone knows what is the different between the Recursive and non-Recursive procedure? and what is the "shell" way of writting a non-recursive procedure? I'll be gladto hear from anyone of you soon
    : : : : Thanx
    : : : :
    : : : :
    : : : Recursive Functions or Procedures call them selfs and non recusive Functions or procedures handle the problem with simple while do, repeat until or for do statements. Recursive functions or procedures are more difficult to understand because they calculate the result in the opposite orrder.
    : : : Example:
    : : : [CODE]
    : : : program Facturial;
    : : : var n: longint;
    : : : //recursive function
    : : : function RecursiveFacturial(x: longint): longint;
    : : : begin
    : : : if x>1 then
    : : : RecursiveFacturial:=x*recursiveFacturial(x-1)
    : : : else
    : : : recursiveFacturial:=1
    : : : end;
    : : : //loop function
    : : : function LoopFacturial(x: longint): longint;
    : : : var temp: longint;
    : : : begin
    : : : temp:=1;
    : : : for x:=x downto 1 do
    : : : temp:=temp*x;
    : : : LoopFacturial:=temp
    : : : end;
    : : : begin
    : : : write('Input the number: ');
    : : : readln(n);
    : : : writeln(n,'!=',RecursiveFacturial(n));
    : : : writeln(n,'!=',LoopFacturial(n));
    : : : readln
    : : : end.
    : : : //compiled in Free Pascal
    : : : [/CODE]
    : : :
    : :
    : :
    : : Thank u so much for ur clarify which make sence with my understanding the difference of recursion and non-recursion special with Binary Search procedure. The Binay Search will be a "function" if we use it as a Recursive, and will be a "Procedure" if it is non-recursive.
    : : once again; thank u so match u have been open my mind for alot of things regarding the Recursion study.
    : : Actually I have another Question; if we have a line of text includes extraneous characters, as well as integers. for example:
    : : 25*-2g &&-+ad dd15wd
    : : How we can ignor the extraneous characters and extract the numeric characters, we should read the text line into string and then extract the numeric characters from string using Val function to determine if a given string character is the 1st character in a valid integer.
    : : if the value of the code parameter = 1 then the character is non-numeric, if it is > 1 then u r looking at the 1st character in a valid integer and the value of code indicates the position of of the 1st non-numeric character. To extract the value of the integer u must copy the numeric characters to another string and then use Val function to extract the new string's numeric value. if the value of code variable = zero, then a valid integer has been read.
    : :
    : :
    : I would do it somehow like so:
    : [CODE]
    : program test;
    : var s,
    : IntS: string;
    : i: byte;
    : int: longint;
    : begin
    : IntS:='';
    : i:=1;
    : write('Input string: ');
    : readln(s);
    : while i<=length(s) do
    : begin
    : if (ord(s[i])>=48) and (ord(s[i])<=57) then
    : IntS:=IntS+s[i];
    : inc(i)
    : end;
    : writeln('String: ',IntS);
    : val(IntS, int);
    : writeln('Integer: ',int);
    : readln
    : end.
    : [/CODE]
    : You have to implement the support for the "-" character. It does not support it now.
    :
    ONE OF TH E CLASSIC EXAMPLE OF A FACTORIAL RECURSVE FUNCTION IS:

    FUNCTION FACT(N:INTEGER):INTEGER;
    BEGIN
    IF N=1 THEN
    FACT:=1
    ELSE
    FACT:=FACT*(N-1)
    END;
  • zibadianzibadian Member Posts: 6,349
    [b][red]This message was edited by zibadian at 2004-3-17 12:37:17[/red][/b][hr]
    : : : : : Hi all;
    : : : : : I'm a new member for this web site, and face a problem with Binary Search Procedure, there r 2 ways to code this procedure/function
    : : : : : 1- as a Recursive
    : : : : : 2- as non-Recursive
    : : : : :
    : : : : : Is anyone knows what is the different between the Recursive and non-Recursive procedure? and what is the "shell" way of writting a non-recursive procedure? I'll be gladto hear from anyone of you soon
    : : : : : Thanx
    : : : : :
    : : : : :
    : : : : Recursive Functions or Procedures call them selfs and non recusive Functions or procedures handle the problem with simple while do, repeat until or for do statements. Recursive functions or procedures are more difficult to understand because they calculate the result in the opposite orrder.
    : : : : Example:
    : : : : [CODE]
    : : : : program Facturial;
    : : : : var n: longint;
    : : : : //recursive function
    : : : : function RecursiveFacturial(x: longint): longint;
    : : : : begin
    : : : : if x>1 then
    : : : : RecursiveFacturial:=x*recursiveFacturial(x-1)
    : : : : else
    : : : : recursiveFacturial:=1
    : : : : end;
    : : : : //loop function
    : : : : function LoopFacturial(x: longint): longint;
    : : : : var temp: longint;
    : : : : begin
    : : : : temp:=1;
    : : : : for x:=x downto 1 do
    : : : : temp:=temp*x;
    : : : : LoopFacturial:=temp
    : : : : end;
    : : : : begin
    : : : : write('Input the number: ');
    : : : : readln(n);
    : : : : writeln(n,'!=',RecursiveFacturial(n));
    : : : : writeln(n,'!=',LoopFacturial(n));
    : : : : readln
    : : : : end.
    : : : : //compiled in Free Pascal
    : : : : [/CODE]
    : : : :
    : : :
    : : :
    : : : Thank u so much for ur clarify which make sence with my understanding the difference of recursion and non-recursion special with Binary Search procedure. The Binay Search will be a "function" if we use it as a Recursive, and will be a "Procedure" if it is non-recursive.
    : : : once again; thank u so match u have been open my mind for alot of things regarding the Recursion study.
    : : : Actually I have another Question; if we have a line of text includes extraneous characters, as well as integers. for example:
    : : : 25*-2g &&-+ad dd15wd
    : : : How we can ignor the extraneous characters and extract the numeric characters, we should read the text line into string and then extract the numeric characters from string using Val function to determine if a given string character is the 1st character in a valid integer.
    : : : if the value of the code parameter = 1 then the character is non-numeric, if it is > 1 then u r looking at the 1st character in a valid integer and the value of code indicates the position of of the 1st non-numeric character. To extract the value of the integer u must copy the numeric characters to another string and then use Val function to extract the new string's numeric value. if the value of code variable = zero, then a valid integer has been read.
    : : :
    : : :
    : : I would do it somehow like so:
    : : [CODE]
    : : program test;
    : : var s,
    : : IntS: string;
    : : i: byte;
    : : int: longint;
    : : begin
    : : IntS:='';
    : : i:=1;
    : : write('Input string: ');
    : : readln(s);
    : : while i<=length(s) do
    : : begin
    : : if (ord(s[i])>=48) and (ord(s[i])<=57) then
    : : IntS:=IntS+s[i];
    : : inc(i)
    : : end;
    : : writeln('String: ',IntS);
    : : val(IntS, int);
    : : writeln('Integer: ',int);
    : : readln
    : : end.
    : : [/CODE]
    : : You have to implement the support for the "-" character. It does not support it now.
    : :
    : ONE OF TH E CLASSIC EXAMPLE OF A FACTORIAL RECURSVE FUNCTION IS:
    :
    : FUNCTION FACT(N:INTEGER):INTEGER;
    : BEGIN
    : IF N=1 THEN
    : FACT:=1
    : ELSE
    : FACT:=FACT*(N-1)
    : END;
    :
    If you read correctly, then you'll see that this classic example is already shown above (with the if-then and else block reversed). Also next time KEEP OF YOUR CAPS LOCK. Its considered shouting.


Sign In or Register to comment.