Recursion

This is a discussion on Recursion within the C Programming forums, part of the General Programming Boards category; Hey there guys, I am doing some exercises in my study book for uni and have just started recursion. One ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    15

    Recursion

    Hey there guys,

    I am doing some exercises in my study book for uni and have just started recursion. One of the exercises is use recursion to multiply 2 numbers using repeated addition.

    The formula is: M x N = N + (M-1) x N

    and the stopping case is 1 x N = N

    the exercise says to create a function

    int multiply(int m, int n)

    I understand the general idea of recursion but am having trouble translating this into code.
    Could some one show me how to create the recursive function and just explain the steps they used.

    Any help would be great

    Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Not really; see the homework policy.

    But, I suppose I could give you the outline of Every Previous-Answer Recursive Function Ever:
    Code:
    int recursive_function(int n) {
        if (n == base_case)
            return base_answer;
        temp = recursive_function(n-1);
        return do_nth_step_to_temp;
    }
    Last edited by tabstop; 10-21-2008 at 10:16 PM. Reason: wrong adjective

  3. #3
    cph
    cph is offline
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    82
    Quote Originally Posted by trevordunstan View Post
    Hey there guys,

    I am doing some exercises in my study book for uni and have just started recursion. One of the exercises is use recursion to multiply 2 numbers using repeated addition.

    The formula is: M x N = N + (M-1) x N

    and the stopping case is 1 x N = N

    the exercise says to create a function

    int multiply(int m, int n)

    I understand the general idea of recursion but am having trouble translating this into code.
    Could some one show me how to create the recursive function and just explain the steps they used.

    Any help would be great

    Thanks
    how about this
    Code:
    unsigned int rec_func (unsigned int m, unsigned int n)
    {
        return((m == 1)? n : n + rec_func(m - 1, n));
    }
    Last edited by cph; 10-21-2008 at 11:06 PM.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    15
    thanks, that coding worked but i do not understand this bit

    return((m == 1)? n :

    can you quickly break it down for me.

    thanks for the help

  5. #5
    cph
    cph is offline
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    82
    Quote Originally Posted by trevordunstan View Post
    thanks, that coding worked but i do not understand this bit

    return((m == 1)? n :

    can you quickly break it down for me.

    thanks for the help
    the function is equal to (someone please correct me if I'm wrong)
    Code:
    unsigned int rec_func (unsigned int m, unsigned int n)
    {
        if (m == 1) {
    	return(n);
        }
    
        return(n + rec_func(m - 1, n));
    }

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    15
    Yeh thats what I thought,

    Thanks for the help, still getting my head around recursion

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 09:45 PM
  2. Recursion... why?
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2008, 10:37 AM
  3. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 10:56 AM
  4. To Recur(sion) or to Iterate?That is the question
    By jasrajva in forum C Programming
    Replies: 4
    Last Post: 11-07-2001, 09:24 AM
  5. selection sorting using going-down and going-up recursion
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 02:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21