Solved

c++ POW function

Posted on 2011-09-08
6
704 Views
Last Modified: 2012-08-14
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

0
Comment
Question by:businessesatoz
6 Comments
 
LVL 6

Accepted Solution

by:
effes earned 400 total points
ID: 36504485
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
 

Author Comment

by:businessesatoz
ID: 36504614
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
 
LVL 6

Expert Comment

by:effes
ID: 36505601
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 7

Assisted Solution

by:Murugesan Nagarajan
Murugesan Nagarajan earned 50 total points
ID: 36508661
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
 
LVL 84

Assisted Solution

by:ozo
ozo earned 50 total points
ID: 36508697
that returns power*base*base
0
 

Author Closing Comment

by:businessesatoz
ID: 36508750
thank you very much.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sorting efficency of sorting algorithm 30 121
IIS Log files on Exchange 2013 server 6 183
Test the speeds on my PC Drives 12 63
How do i run a c++ file? 15 46
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question