The revolutionary project management tool is here! Plan visually with a single glance and make sure your projects get done.

Hello, i been trying to write my own pow function but it's not working out.. when i enter "base 2, power 3" gives me 4... not sure what my problem is..

```
#include <iostream>
using namespace std;
int CalcPower(int base, int power)
{
int total;
for (int i =0; i<power; ++i)
{
total = base * base;
return total;
}
}
int main(void)
{
int one;
int two;
cout <<"please type in your base" <<endl;
cin >> one;
cout <<"please type in your power" <<endl;
cin >> two;
CalcPower(one,two);
cout<<endl;
cout << CalcPower(one,two);
}
```

```
int total = base;
```

can't i just write
```
int total; total = base * base
```

is that the same thing your doing here..? thanks
```
int total;
total = base;
```

instead of```
int total = base;
```

In the first snippet the variable total is first declared and then initialized.In the second snippet this is done a single step.

BUT: base is not the same as base * base!

I'll try to explain the code I gave you in my first post:

This checks if power is 0. If so the result is per definition 1, so we can just return that.

```
if(power == 0)
{
return 1;
}
```

The next step is to declare the variable we will use to calculate the result.Since we already handled the case that power is 0 with the code above we can assume that power is at least 1. As base^1 is just base we can use this value to initialize total.

```
int total = base;
```

Now for the loop. If power is 1 the variable total already holds the correct result, so we don't have to enter the loop. That's why i starts at 2. If power is 2 or higher it will enter the loop an multiply the product of previous iterations (held in total) by base.```
for (int i =2; i<=power; ++i)
{
total = total * base;
}
```

At the end we simply have to return the value stored in total and we're done.```
return total;
```

Just walk it through with power = 3:power is != 0, so we initialize total = base

no the loop: i = 2 which is <= 3

total becomes total * base which equals base * base

next step: i = 3 which is <= 3

total becomes total * base which equals base * base * base

next step: i = 4 which is > 3

the loop is exited and total is returned as the result

total is now base * base * base which is base^3.

I hope that cleared things up a bit,

Frank

```
#include <iostream>
using namespace std ;
/*
Compile time options on Linux:
g++ -Wall fileName.C
Remove the warning messages during compilation.
*/
/*
Add comments here for describing the function CalcPower and the parameters.
*/
int CalcPower( int base, int power)
{
/* int total ; Initialize all variables at the definition location. (To resolve the valgrind reported issues) */
int total = 0 ;
for ( int i =0 ; i < power ; ++i)
{
/* total = base * base ; */
total += base * base ; /* Replace = with += */
/* return total ; This needs to be added at the end of this function */
}
/* warning: control reaches end of non-void function */
/* Hence write the following statement */
return total ; /* Always use return for each functions */
}
/*
Add comments here for describing the function main and the parameters.
*/
int main( void)
{
/* int one ; Initialize all variables at the definition location. (To resolve the valgrind reported issues) */
/* int two ; Initialize all variables at the definition location. (To resolve the valgrind reported issues) */
int one = 0 ;
int two = 0 ;
cout << "please type in your base" <<endl ;
cin >> one ;
cout << "please type in your power" <<endl ;
cin >> two ;
CalcPower( one, two) ;
"calPow.C" 50 lines, 1611 characters
bectest2@aix52 /develop/bectest2/.Murugesan/tcp [ 0 ]
$ mk
make: 1254-004 The error code from the last command is 2.
Stop.
xlC -g calPow.C
^[/bectest2@aix52 /develop/bectest2/.Murugesan/tcp [ 0 ]
$ cat calPow.C
#include <iostream>
using namespace std ;
/*
Compile time options on Linux:
g++ -Wall fileName.C
Remove the warning messages during compilation.
*/
/*
Add comments here for describing the function CalcPower and the parameters.
*/
int CalcPower( int base, int power)
{
/* int total ; Initialize all variables at the definition location. (To resolve the valgrind reported issues) */
int total = 0 ;
for ( int i =0 ; i < power ; ++i)
{
/* total = base * base ; */
total += base * base ; /* Replace = with += */
/* return total ; This needs to be added at the end of this function */
}
/* warning: control reaches end of non-void function */
/* Hence write the following statement */
return total ; /* Always use return for each functions */
}
/*
Add comments here for describing the function main and the parameters.
*/
int main( void)
{
/* int one ; Initialize all variables at the definition location. (To resolve the valgrind reported issues) */
/* int two ; Initialize all variables at the definition location. (To resolve the valgrind reported issues) */
int one = 0 ;
int two = 0 ;
cout << "please type in your base" <<endl ;
cin >> one ;
cout << "please type in your power" <<endl ;
cin >> two ;
CalcPower( one, two) ;
cout << endl ;
cout << CalcPower( one, two) ;
cout << endl ;
return 0 ; /* Always use return for each functions */
}
```

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

you have your return statement inside the loop, so it returns just at he end of the first iteration.

Also you simply assign to total the value of base * base. Given that base is 2 this will always be 4 no matter how often you iterate through the loop.

You should also check wether power is 0 or 1. If it's 0 the return value is always 1 and if it's 1 then the return value is base istself.

Change your code to:

Open in new window

Hope that helps,

Frank