Thread: Recursive function

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    75

    Question Recursive function

    How does the fRecur function works and what value does it return to main?

    Somebody help!!


    #include "stdio.h"

    enum con{MIN_NUM=0, MAX_NUM=100};

    int fRecur(int n);

    main(void)
    {
    int i,suma=0;

    for(i=0;i<=MAX_NUM;i++) suma+=i;
    printf("El valor de suma es %i\n",suma);
    printf("El valor de suma que devuelve fRecur es %i\n",fRecur(MAX_NUM));

    return(0);
    }

    fRecur(int n)
    {
    if(n==MIN_NUM) return 0;
    else return fRecur(n-1)+n;
    }
    ---Programming is like roaming, you never know where you'll end at------

    If 'here' is pronounced as 'hear', why 'there' isnt pronounced as 'dear'??

    [email protected]

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    No cierto, pero - por favor, aqui nos hacemos los Ingles!!
    Buena suerte!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Actually, from the looks of it, the function will never stop since the number just keeps growing...I think you have waht we call here a "bum algorithm".
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    <fx: runs code>
    El valor de suma es 5050
    El valor de suma que devuelve fRecur es 5050

    Now what was the question?

    > How does the fRecur function works
    Imagine a call to fRecur(2)

    This will result in
    return fRecur(2-1)+2;

    So now do the same, (2-1) results in fRecur(1)
    return fRecur(1-1)+1;

    So now do the same, (1-1) results in fRecur(0)
    Aha, this is different,
    if(n==MIN_NUM) return 0; /* end of the road */

    Working your way back fRecur(0) returns 0, so
    return fRecur(1-1)+1;
    is
    return 0+1;

    And
    return fRecur(2-1)+2;
    is
    return 1+2;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive function
    By Fork in forum C Programming
    Replies: 3
    Last Post: 10-26-2006, 11:27 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM