recursion

Discussion in 'C++' started by jw, Dec 1, 2005.

  1. jw

    jw Guest

    Hi all,i have a recursive function and in main for ex it takes 5 as a
    parameter,n'll equal to 5 and than decrease.At the end it ll be 1 and
    why doesnt the result equal to 1 because of this=>
    if(n==1)
    return 1


    -----
    int sum(int n){
    if(n==1)
    return 1;
    else
    return sum(n-1)+n;
    }
    void main(){
    cout<<sum(5);
    }
     
    jw, Dec 1, 2005
    #1
    1. Advertising

  2. jw wrote:
    > Hi all,i have a recursive function and in main for ex it takes 5 as a
    > parameter,n'll equal to 5 and than decrease.At the end it ll be 1 and
    > why doesnt the result equal to 1 because of this=>
    > if(n==1)
    > return 1
    >
    >
    > -----
    > int sum(int n){
    > if(n==1)
    > return 1;
    > else
    > return sum(n-1)+n;
    > }
    > void main(){
    > cout<<sum(5);
    > }


    So output should be
    sum(5-1) + 5
    5 + 4 + sum(4-1) and onward
    So you should end up with
    5 + 4 + 3 + 2 + 1, or 15 if my math serves me correctly.

    What's the problem?
     
    Josh Mcfarlane, Dec 1, 2005
    #2
    1. Advertising

  3. * jw:
    > Hi all,i have a recursive function and in main for ex it takes 5 as a
    > parameter,n'll equal to 5 and than decrease.


    In a sense the parameter's value decreases, but don't take that too
    literally.

    Each call of the function has it's own parameter value.


    > At the end it ll be 1 and
    > why doesnt the result equal to 1 because of this=>
    > if(n==1)
    > return 1


    That specifies only the result for that call of the function.


    > int sum(int n){
    > if(n==1)
    > return 1;
    > else
    > return sum(n-1)+n;
    > }
    > void main(){


    The 'main' return type must be 'int'.

    See FAQ item 29.3, "Should I use void main or int main?".



    > cout<<sum(5);
    > }


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Dec 1, 2005
    #3
  4. * Josh Mcfarlane:
    > jw wrote:
    > > Hi all,i have a recursive function and in main for ex it takes 5 as a
    > > parameter,n'll equal to 5 and than decrease.At the end it ll be 1 and
    > > why doesnt the result equal to 1 because of this=>
    > > if(n==1)
    > > return 1
    > >
    > >
    > > -----
    > > int sum(int n){
    > > if(n==1)
    > > return 1;
    > > else
    > > return sum(n-1)+n;
    > > }
    > > void main(){
    > > cout<<sum(5);
    > > }

    >
    > So output should be
    > sum(5-1) + 5
    > 5 + 4 + sum(4-1) and onward
    > So you should end up with
    > 5 + 4 + 3 + 2 + 1, or 15 if my math serves me correctly.
    >
    > What's the problem?


    I think the problem is you solved jw's homework problem instead of what
    he literally asked for.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Dec 1, 2005
    #4
  5. jw

    Ron Natalie Guest

    jw wrote:
    > Hi all,i have a recursive function and in main for ex it takes 5 as a
    > parameter,n'll equal to 5 and than decrease.At the end it ll be 1 and
    > why doesnt the result equal to 1 because of this=>
    > if(n==1)
    > return 1
    >
    >
    > -----
    > int sum(int n){
    > if(n==1)
    > return 1;
    > else
    > return sum(n-1)+n;
    > }
    > void main(){
    > cout<<sum(5);
    > }
    >

    Main returns int by the way.

    While the sun(1) is the last one CALLED, it's not the one that
    returns to main, it returns to the sum(2) call.
     
    Ron Natalie, Dec 1, 2005
    #5
  6. jw

    Cy Edmunds Guest

    "jw" <> wrote in message
    news:...
    > Hi all,i have a recursive function and in main for ex it takes 5 as a
    > parameter,n'll equal to 5 and than decrease.At the end it ll be 1 and
    > why doesnt the result equal to 1 because of this=>
    > if(n==1)
    > return 1
    >
    >
    > -----
    > int sum(int n){
    > if(n==1)
    > return 1;
    > else
    > return sum(n-1)+n;
    > }
    > void main(){
    > cout<<sum(5);
    > }
    >


    The code above is the same as:

    int sum1() {
    return 1;
    }


    int sum2() {
    return sum1()+2;
    }


    int sum3() {
    return sum2()+3;
    }


    int sum4() {
    return sum3()+4;
    }


    int sum5() {
    return sum4()+5;
    }

    int main(){ // note: the return type of main() is int, not void
    cout<<sum5();
    }
     
    Cy Edmunds, Dec 1, 2005
    #6
  7. Alf P. Steinbach wrote:
    > I think the problem is you solved jw's homework problem instead of what
    > he literally asked for.


    Whoops, I figured he was wondering why it didn't output 1, and figured
    showing him step by step what it was doing would be easiest way to
    understand.

    Josh McFarlane
     
    Josh Mcfarlane, Dec 1, 2005
    #7
  8. jw

    jw Guest

    Josh Mcfarlane wrote:
    > Alf P. Steinbach wrote:
    > > I think the problem is you solved jw's homework problem instead of what
    > > he literally asked for.

    >
    > Whoops, I figured he was wondering why it didn't output 1, and figured
    > showing him step by step what it was doing would be easiest way to
    > understand.
    >
    > Josh McFarlane


    it is not my homework i saw it in a book and wondered,and if it had
    been a homework i wouldnt have asked it to the group because the best
    way to learn is doing homeworks without any help.thanks to people who
    helped me..
     
    jw, Dec 1, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim Mohler
    Replies:
    1
    Views:
    460
    Steve Grazzini
    Sep 16, 2003
  2. B McInnes

    recursion with perl

    B McInnes, Nov 1, 2003, in forum: Perl
    Replies:
    4
    Views:
    4,149
    B McInnes
    Nov 4, 2003
  3. Chris

    Recursion Query

    Chris, May 17, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    452
    avnrao
    May 17, 2004
  4. Iain
    Replies:
    3
    Views:
    1,835
    Robbe Morris [C# MVP]
    Mar 9, 2006
  5. Replies:
    8
    Views:
    798
    John Reye
    Apr 26, 2012
Loading...

Share This Page