Thread: Recursive Help

  1. #1
    Tommy
    Guest

    Recursive Help

    How do i write a recursive function that takes two parameters with one representing the base of an exponent and the other the power of the exponent. For example if the function was called with the with the following parameters(5, 3) it would return 125

    Thanks for your help

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Ive gotten this so far but it doesnt work right. Maybe you might want to edit it.
    Code:
    #include <iostream>
    #include <conio.h>
    
    double exponent (double base, double power);
    
    int main()
    {
        double b, p;
        double answer;
        cout << " Enter base and power respectively";
        cin >>b,p;
        cout << "result is "<<answer;
        getch();
        return 0;
        
    }
    double exponent(double b, double p)
    {
        double answer;
        for (int power = 0; p<power; p--)
        answer = b *p;
        return answer;
    }
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Code:
    #include <iostream>
    #include <conio.h>
    
    double exponent (double base, double power);
    
    int main()
    {
        double b, p;
        double answer;
        cout << " Enter base and power respectively"<<endl;
        cin >>b,p;
        cout << "result is "<<answer;
        getch();
        return 0;
        
    }
    double exponent(double b, double p)
    {
        double answer;
        answer = b * exponent(b,p-1);
        return answer;
    }
    dont work either
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    26
    Code:
    ##include <stdio.h>
    #include <iostream.h>
    
    int exponent (int, int);
    
    void main()
    {
    	int num;
    	int expo;
    
    	printf("Enter a number:");
    	scanf("%d", &num);
    
    	printf("Enter a exponent number:");
    	scanf("%lld", &expo);
    
    	printf("The answer is: %d\n", exponent(num, expo));
    
    }
    
    int exponent(int a, int b)
    {
    	int sum=0;
    	int avg=1;
    
    	while (avg <b)
    	{
    		sum=sum*a;
    		avg++;
    	}
    	return sum;
    }
    This Code works 100% (it worked great for me)just make sure you just have to change the printf put cout<< and for scanf put cin>> dont forget that ok see yaa good lock
    C++ Is cool

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Oh yeah thats what i was missing. Hopefully Tommy will read this.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    um your code dont work returns 0 as answer all the time.
    Also i dont think its a recursive function. Even though i dont know why tommy would want to use one.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  7. #7
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
    double power(double b, double p)
    {
    	if (p > 1)
    		return b * power(b, --p);
    	return b;
    }

  8. #8
    Tommy
    Guest
    Thanks

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    26
    Code:
    i made some mastake in first one 
    #include <stdio.h>
    #include <iostream.h>
    
    int exponent (int, int);
    
    void main()
    {
    	int num;
    	int expo;
    
    	printf("Enter a number:");
    	scanf("%d", &num);
    
    	printf("Enter a exponent number:");
    	scanf("%lld", &expo);
    
    	printf("The answer is: %d\n", exponent(num, expo));
    
    }
    
    int exponent(int a, int b)
    {
    	int avg=0;
    	int sum=1;
    
    	while (avg <b)
    	{
    		sum=sum*a;
    		avg++;
    	}
    	return sum;
    }
    //Now it's going to work look at the
     //first one and see the diffrence ok look
     //if you can find my mistake
    gamer4life687 you can check to now if it works
    C++ Is cool

  10. #10
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Yet it's still not a recursive function.

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