c++ POW function

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);
}

Open in new window

businessesatozAsked:
Who is Participating?
 
effesConnect With a Mentor Commented:
Hi,

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:

int CalcPower(int base, int power)
{
    if(power == 0)
    {
        return 1;
    }

    int total = base;

    for (int i =2; i<=power; ++i)
    {
        total = total * base;
    }

    return total;
}

Open in new window


Hope that helps,
Frank
0
 
businessesatozAuthor Commented:
Thanks for your help, although i have a question regarding
 int total = base; 

Open in new window

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

Open in new window

is that the same thing your doing here..? thanks
0
 
effesCommented:
you could write
int total;
total = base;

Open in new window

instead of
int total = base;

Open in new window

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;
}

Open in new window

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;

Open in new window

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;
}

Open in new window

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

Open in new window

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
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
Murugesan NagarajanConnect With a Mentor Subject-matter expert at C++ C delivery, implementation, at UNIX oriented operating systems (Windows: CYGWIN_NT MINGW32_NT MINGW64_NT)Commented:
Updated code:

#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 */
}

Open in new window

0
 
ozoConnect With a Mentor Commented:
that returns power*base*base
0
 
businessesatozAuthor Commented:
thank you very much.
0
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.