What's new

question for the c++ programmers- recursion

Status
Not open for further replies.

mesman00

What's that...?
not really emucoding... but nonetheless i need help...
damn, this is a simple problem i'm havin in c++ with recursion. its wicked pissing me off, so i decided to ask here. i need to write a program that asks the user for a number and a power. Then, i need to write a recursive function that takes the number the user entered and raises it to the power the user entered. For example, if the user enters a number of 2 and a power of 4, the function will return 16. now, help me please! i've been goin at it for a while, and i just can't seem to come up with it. here's the code i have, it's really just the main function, because the only other one is the power function (where the recursion will occur), the one i'm havin a little trouble with.

// Exercise 7
#include <iostream.h>

int power(int number, int raise);

int main()
{
int number;
int answer;
int raise;

cout << "Enter a number: ";
cin >> number;
cout << "Enter a power to raise the number to: ";
cin >> raise;
cout << endl;

answer = power(number, raise);

cout << number << " raised to the " << raise << " th power is equal to " << answer << endl;

return 0;
}

int power(int number, int raise)
{
HELP NEEDED HERE
}
 

Azimer

Emulator Developer
Moderator
Should be something like:

Code:
int power(int number, int raise) 
{ 
   if (raise == 0)
      return 1;
   else
     return number * power (number, raise-1);
}

if you have power (2, 0) it should return 1...
if you have power (2, 2) is should return power (2, 2) (which is 2 * power (2, 1) (which is 2 * 2 power(2, 0)) (which is (2 * 2 * 1) ))) I assume you don't need negative squares... :-/ it would just be 1/power(2, abs(raise)) anyway...
 
OP
mesman00

mesman00

What's that...?
ok, now i got it, i had the first part of this if statement right, i went wrong at else.

i was trying to break up the else into all different ways, but i did have the function call in the else statement right!
power (number, raise - 1)
anyway, thanks azimer, now i can go to bed happy, yet feeling a little stupid because i should have been able to figure that out. oh well, you live and learn.

just one more thing, the return value on the first part of the if statement in the power function should be zero, no? because if you a raise a number to a zero power, the answer will be 0, not 1.
 
Last edited:

Pheo

New member
this should help


int i, total=1;

for (i=0;i<power;i++)
{
total=total*number;
}

return total;
 

Hacktarux

Emulator Developer
Moderator
Pheo : you are making an iterative algorithm and not a recursive one.
And Mesmann : When you raise a number to the zero power the result is 1 !!.
 
OP
mesman00

mesman00

What's that...?
hah, ur right, im so dumb, i guess i wasn't thinking last night, goes to show that i need to be gettin more sleep!
 

Pheo

New member
sleep would be nice, 1g of caffeine is way too much to survive 8 hrs of work w\ no sleep.
i cannot even remember much of my C++ from high school
 

Maxiums

New member
int recursion(int num, int power)
{
if(power == 0)
return 1;
else if(power == 1)
return num;
else if(power > 1)
return recursion(num, power - 1);
}
 

Eddy

I Run This
mesman, alot of the content on your web site (crematorium) isnt right, get your mind right.
 
OP
mesman00

mesman00

What's that...?
Originally posted by Eddy
mesman, alot of the content on your web site (crematorium) isnt right, get your mind right.

Hahahahahaha. It's all a big joke, everything on the site, lol. it's just a place to go for me and my friends to laugh, nothing should be taken seriously there, except maybe the poll! btw, someone close this thread, it has no reason to be open anymore, problem solved a way long time ago.
 

Doomulation

?????????????????????????
Is it really this hard to make a function that raises a number?
Generally, in the language I program in, it's a simple math calculation.
 
OP
mesman00

mesman00

What's that...?
nope, not hard at all, i was just tryin to do it a specific way, but it don't matter anymore, that was a long time ago
 

Malcolm

Not a Moderator
its not always hard, someone could write a program that rases a number to 40 and counts how many had to be added like so:

Code:
#include <iostream.h>

void main(){
    int x =29;
    int z;
    
    for (z = 0; x < 41; z++)
    {
         x++
    }
cout << "To get 29 to 40 you'd have to add " << z;
}

and yes people, u can use printf, i just like cout
 
Status
Not open for further replies.

Top