Thread: Re : Recursive

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    5

    Unhappy Re : Recursive

    Dear All,

    I have problem regarding the following, can anyone help, please...

    /* function aim : should ask for the number of summation terms and print out the result.

    e.g if the input is 2, the answer should be
    x=1/2 + 1/3 = 0.8333

    if the input is 4, the anwer should be x=1/2+1/3+1/4+1/5 =
    1.2833,*/


    int sum_Sequence (int n)
    {float answer;

    if (n==0)
    answer = 0;
    else
    answer = 1/n+1 + sum_sequence(n-1);
    return (answer);
    }

  2. #2
    Unregistered
    Guest
    maybe...
    Code:
    float sumSequence( int n )
    {
       if ( n == 0 ) 
          return 0
       return ( 1 / ( n + 1 ) )+ sumSequence( n - 1 )
    }

  3. #3
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    i thought i was logged in.

  4. #4
    freshman
    Guest

    Unhappy

    I tried but it's not work.

  5. #5
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Because of the integer division? Do something like
    return ( 1.0 / ( n + 1 ) )+ sumSequence( n - 1 )

  6. #6
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    or use a cast operator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recursive function
    By technosavvy in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 05:42 AM
  2. difference between recursive and iterative
    By Micko in forum C Programming
    Replies: 33
    Last Post: 07-06-2004, 09:34 PM
  3. Algorithm help (Changing from Recursive to Non Recursive)
    By Thantos in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2004, 07:27 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM