Thread: confused base case(s)

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    4

    confused base case(s)

    I understand that each recusive function must have one base case(A solution given directly) however they may also have two. This is where I get confused see below. Is my thinking below correct or way off?


    Code:
    int funct1(int m, int n) {
        if (m==n || n==1)          //  single base case       
            return 1;
        else
              return funct1(m-1,n-1) + n*func1(m-1, n);
    }
    if I broke these in two parts as below I would have two base cases. I think it still says the same as (this |or| that) Am I on the right track.

    Code:
     
    int ...................
            if (m==n)
                    return 1              //1st base case
            else if (n==1)
                    return 1                // 2nd base case
            else 
                      .........................

  2. #2
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Logically your two constructs are exactly the same.

    A recursive function must have at least one return to break the recursion, it can have any number >= 1.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    4
    Thanks,
    I thought they were saying the same thing. To have two base cases one of the returns would have to be something other than 1?

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I don't think it matters whether you write it as:
    Code:
        if (m==n || n==1)
            return 1;
    Or:
    Code:
            if (m==n)
                    return 1;             //1st base case
            else if (n==1)
                    return 1;               // 2nd base case
    It's still two base cases. With the first example, you just combined the two base cases into one statement. Someone correct me if I'm wrong.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >To have two base cases one of the returns would have to be something other than 1?

    one case may be 1, two or more cases may be 1, but no cases need to be 1, and in fact the case to stop it doesn't even have to be an int. You could stop it if the user enters a 'q' or types in quit or pushes the Page Up key, or presses the Ctr-Alt-Delete keys in combination. You just need something to stop it.
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. base 10 to base 2 conversion
    By dteum in forum C++ Programming
    Replies: 4
    Last Post: 09-27-2008, 01:12 PM
  2. Bit Computation Program
    By cisokay in forum C++ Programming
    Replies: 6
    Last Post: 05-13-2005, 09:32 PM
  3. Calling Base Class Operators
    By pianorain in forum C++ Programming
    Replies: 2
    Last Post: 03-10-2005, 10:53 AM
  4. adding base n numbers
    By doogle in forum C++ Programming
    Replies: 4
    Last Post: 11-11-2002, 11:23 AM
  5. converting numbers from one base to another
    By partnole in forum C++ Programming
    Replies: 4
    Last Post: 10-04-2001, 12:29 PM