Thread: recursive function

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    3

    recursive function

    hi there.
    i just started c programming 2 weeks ago and i have a project which is due in a weeks time.

    there is a question which i am stuck at:

    Write a recursive function TotalQuotients that take in 3 float parameters start, end anf factor. TotalQuotients sum up all quotients of start and factor. For each recursion, start is added by factor, up to or including end.
    For example:
    TotalQuotients(3.0, 7.0, 1.5) returns 9.0 ((3.0/1.5)+(3.0+1.5)/1.5+(4.5+1.5)/1.5)


    Later change the above to an iterative solution.


    Hope someone will be able to hel pme with this.

    thank you.

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    3
    Hi, thanks for replying.
    I didn't understand the question in the beginning too!
    But this is what my classmates explained to me, and they are also stucked on how to get the equation out.

    Example:
    TotalQuotients(3.0, 7.0, 1.5) returns 9.0
    ((3.0/1.5)+(3.0+1.5)/1.5+(4.5+1.5)/1.5)

    Start = 3.0
    End = 7.0
    Factor = 1.5

    therefore, "Start" add "Factor" until <= "End"(7.0),
    later divide by "Factor".
    it is like the "Start" increases by 1.5 until <= End.

    first, "start" adds zero, then adds 1.5 (becomes 4.5), adds 1.5 (becomes 4.5), adds 1.5 again (becomes 6.0)...

    and each sum must be divided by the factor (1.5). Or maybe they all can be added up first before dividing by 1.5 (as 1.5 is the common denominator).

    Thanks again!

  3. #3
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    You just started programming two weeks ago- and you are allready doing recursion. WOW!! that is quick!!! This is something that is not very basic.
    Mr. C: Author and Instructor

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    3
    it is a 3 month module program...
    it'skinda tuff when i have no programming background, but i guess i won't give up now.


    i referred to a book Art of C Programming, but there was only one example on recursive functions, which is the calculation of factorials...

    BTW, must there always be a "return" used in a recursive function?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by hnc
    BTW, must there always be a "return" used in a recursive function?
    Technicly, no. For example, this is valid code. Compile it and watch what happens:
    Code:
    void fun(void) {
        int x[1024];
        fun( );
    }
    
    int main (void)
    {
        fun( );
        return 0;
    }
    The program will eventually (fairly quickly) crash because it will run out of stack space. Stack space is basicly the amount of memory alloted to the program for storing variables and calling functions.

    Since nothing ever tells the function to stop recursing, it calls itself indefinately, and runs out of stack space, causing your program to crash.

    So the answer is "no", but in practice it's "yes".

    Quzah.
    Hope is the first step on the road to disappointment.

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