Recursion question

This is a discussion on Recursion question within the C++ Programming forums, part of the General Programming Boards category; today i finished the last exercise on functions, it was about writting a prog that takes 2 numbers, and gives ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    494

    Recursion question

    today i finished the last exercise on functions, it was about writting a prog that takes 2 numbers, and gives u the power, u had to do this using recursion. after i finished it i went back to check the answer and tested the answer the book had, well i have to say that i was disappointed.

    they only tested for 1 base case when in a prog like this u have to test for 2 correct?
    Code:
    {
    	if (power == 1)
    		return n;
    	{
    		if (power ==0)
    		return 1;
    	else
    		return (n * PowerFunc(n,power -1));
    }
    they were only testing for

    Code:
    if (power == 1)
    		return n;
    so would u say that their answer was incomplete?
    another question, recursion function do they always have 2 base cases? should i email them to correct it? eheh
    When no one helps you out. Call google();

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    today i finished the last exercise on functions, it was about writting a prog that takes 2 numbers, and gives u the power, u had to do this using recursion. after i finished it i went back to check the answer and tested the answer the book had, well i have to say that i was disappointed.

    they only tested for 1 base case when in a prog like this u have to test for 2 correct?
    I think the book is ok. Your code won't work for negative numbers either.

    Recursive functions won't always have two base cases. In fact, you could write your code as
    Code:
          if (power ==0)
               return 1;
          else
               return (n * PowerFunc(n,power -1));

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    u stand correct on the negative numbers but i still think that this prog requires 2 base cases,
    2^ 1 =2
    2^0=1
    2 ^ 2 = 4

    so if u dont test for (power ==1 ) then ull get 1 when u suppose to get n correct?
    When no one helps you out. Call google();

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    nm lol i figured it out, the function calls itself, its recursion. lol
    When no one helps you out. Call google();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. iteration and recursion question
    By Stonehambey in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2008, 07:16 PM
  2. simple recursion question
    By salvadoravi in forum C Programming
    Replies: 4
    Last Post: 12-30-2007, 07:53 AM
  3. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 05:22 AM
  4. Recursion vs multiple functions
    By PJYelton in forum C++ Programming
    Replies: 4
    Last Post: 12-29-2002, 08:52 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM

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