Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple program, but I don't know what's wrong

Status
Not open for further replies.

hollaback

Technical User
Apr 8, 2003
6
US
Hello! I'm new to C++ and learning about functions. I came up with no errors while compiling and linking, but it couldn't execute. I think it was a syntax error or something. I'm supposed to write a function integerPower (base, exponent) that returns the value of base exponent.

The code:
#include <iostream>

using std::cout;
using std::endl;

int integerPower(int, int);

int main ()
{
int base = 3;
for (int exponent = 1; exponent <= 10; exponent++)
cout << base << &quot;^&quot; << exponent << &quot; = &quot;
<< integerPower(base, exponent) << endl;

return 0;
}

int integerPower(int base, int exponent)
{
return base * integerPower(base, (exponent - 1));
}

Thanks!
wink.gif
 
Well .. this is a microsoft visual basic forum ...
so you may want to try the Microsoft c++ forum

Transcend
[gorgeous]
 
Do you realise this is a Visual Basic forum?

that being said I'll answer your question

int integerPower(int base, int exponent)
{
return base * integerPower(base, (exponent - 1));
}

will recursively call itself for ever. I think you want to put in a if statement to not recurse once exponent = 1 ... errr 0. I'm not going to think about the math at this point.
 
Hey hollaback....

you need to put if(exponent>=1) before return...

Code:
int integerPower(int base, int exponent)
{
    return base * integerPower(base, (exponent - 1));
}

...to...

Code:
int integerPower(int base, int exponent)
{
    if(exponent>=1) return base * integerPower(base, (exponent - 1));
}

Next time you Might wanna use the VC++ forum ;-) Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 
oops... almost for got....


else return 1

...

Code:
int integerPower(int base, int exponent)
{
    if(exponent>=1) return base * integerPower(base, (exponent - 1)); else return 1;
}
Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 
here is the full code and the Output I got...

Code:
#include <iostream>

using std::cout;
using std::endl;

int integerPower(int, int);

int main ()
{
    int base = 3;
    for (int exponent = 0; exponent <= 10; exponent++) 
        cout << base << &quot;^&quot; << exponent << &quot; = &quot; << integerPower(base, exponent) << endl;
        
    return 0;
}

int integerPower(int base, int exponent)
{
    if(exponent>=1) return base * integerPower(base, (exponent - 1)); else return 1;
}

and the Output...

3^0 = 1
3^1 = 3
3^2 = 9
3^3 = 27
3^4 = 81
3^5 = 243
3^6 = 729
3^7 = 2187
3^8 = 6561
3^9 = 19683
3^10 = 59049


Hope this helps... ;-) Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 
Thanks to all of you who helped. I really appreciate it. Plus I didn't know I posted in the wrong thread. I'm new and I was in a hurry so I just posted somewhere.

Thanks again!
wink.gif
 
I guess you got lucky on this one...

I bet you're glad that most programmers know multiple (code) languages ;-)

Next Time You Might Want To Post Here Though...

forum207

... C++: Microsoft ...

Good Luck Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top