PDA
$threadinfo[title]
-


mesman00
February 26th, 2002, 09:27
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
February 26th, 2002, 09:53
Should be something like:



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...

mesman00
February 26th, 2002, 10:00
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.

Pheo
February 26th, 2002, 13:30
this should help


int i, total=1;

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

return total;

Hacktarux
February 26th, 2002, 16:07
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 !!.

mesman00
February 26th, 2002, 19:25
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
February 28th, 2002, 11:31
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

fivefeet8
March 7th, 2002, 09:24
All this coding stuff makes me want to start coding again..

mesman00
March 8th, 2002, 09:09
then do so!

Maxiums
March 12th, 2002, 07:39
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
March 13th, 2002, 08:39
mesman, alot of the content on your web site (crematorium) isnt right, get your mind right.

mesman00
March 13th, 2002, 22:02
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
March 22nd, 2002, 08:45
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.

mesman00
March 22nd, 2002, 09:04
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
March 22nd, 2002, 09:12
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:


#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