[RESOLVED] help creating a power program..
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: [RESOLVED] help creating a power program..

1. Member
Join Date
Jan 2007
Posts
28

## [RESOLVED] help creating a power program..

hi
i need help in writing a function
integerPower(base,exponent) that returns the value base^exponent with assuming that the exponent is a positive, nonzero integer and that the base is an integer. the function integerPower should use for or while loop to control the calculations, without using any math library functions.

3. ## Re: help creating a power program..

I assume this is homework so I wont give you any codes. But, basically you can just make some kind of loop that multiplies base with itself, exponent times.

What have you got so far?

- petter

4. Member
Join Date
Jan 2007
Posts
28

## Re: help creating a power program..

i thought of it like this..

Code:
```int integerPower(int power,int exponent)
{
for(i=1;i<=exponent;i++)
return x^exponent;
}```
but it is not true ofcourse
Last edited by elsa; January 19th, 2007 at 05:15 AM.

5. Senior Member
Join Date
Nov 2006
Location
Essen, Germany
Posts
1,344

## Re: help creating a power program..

Hi,

Originally Posted by elsa
i thought of it like this..

int integerPower(int power,int exponent)
{
for(i=1;i<=exponent;i++)
return x^exponent;
}
but it is not true ofcourse
How would you calculate 4^5 using pen and paper?

Guido

6. Member
Join Date
Nov 2006
Location
Barcelona - Catalonia
Posts
364

## Re: help creating a power program..

Originally Posted by elsa
i thought of it like this..

int integerPower(int power,int exponent)
{
for(i=1;i<=exponent;i++)
return x^exponent;
}
but it is not true ofcourse
^ is the XOR operation in C++, not for exponentiation!!! Answer the Guido post:
Originally Posted by GNiewerth
How would you calculate 4^5 using pen and paper?
and you'll have the solution.

Albert.

7. Member
Join Date
Jan 2007
Posts
28

## Re: help creating a power program..

i know the idea..4^5=4*4*4*4*4
but i dont know how to transfer this into a c++ program..
how can i stop the loop at the number of the exponent?

8. Member
Join Date
Nov 2006
Location
Barcelona - Catalonia
Posts
364

## Re: help creating a power program..

So, with this idea take the code you left here (changing the ^ operation for the correct one), and that's all. Right?

Well, the return is not in the right place, either :-)

Albert

9. Member
Join Date
Jan 2007
Posts
28

## Re: help creating a power program..

that is what i thought..

Code:
```int integerPower(int power,int exponent)
{
for(i=1;i<=exponent;i++)
return x*i;
}```
is that right?
and what do u mean by the return is not in the right place? plz explain
Last edited by elsa; January 19th, 2007 at 05:27 AM.

10. Senior Member
Join Date
Nov 2006
Location
Essen, Germany
Posts
1,344

## Re: help creating a power program..

Hi Elsa,

when you look at your code, what do you think does it do?

Code:
```int integerPower(int power,int exponent)
{
for(i=1;i<=exponent;i++)
return x*i;
}```
What is the math done by that function? What results do you get for sample function calls? Maybe you should spend some hours of reading to get familiar with programming before asking.

Regards,
Guido

11. Member
Join Date
Nov 2006
Location
Barcelona - Catalonia
Posts
364

## Re: help creating a power program..

No, try to know what your function makes?
Code:
```int integerPower(int power,int exponent)
{
for(i=1;i<=exponent;i++)
return x*i;
}```
Imagine you want to calculate 4^5.
When your programs goes into integerPower function.

power = 4 and exponent = 5. Right?

Inside for:
First step:
i = 1;
x*i = ? What is x? Is undefined, and i variable is also undefined!!! I guess you wanted power.
Anyway, power*i = power = 4; Rigth?
And then, there's a return. So, your funtion will return 4, without doing any other step. Is that what you want? Try to think a little bit more. We'll help you.
And use code tags to put your code. It's easy to read :-)

Albert.

12. ## Re: help creating a power program..

if you call return it will exit from the function immediately.

You need to keep a temporary store of the number.

Loop counter i must also be declared, although you can actually manage without a loop counter because you can decrement your local copy of the "exponent" parameter until it goes down to 0. I note you don't need to allow for the case where exponent is 0 but it wouldn't do any harm.
Code:
```int integerPower( int base, int exponent )
{
int result = 1;
// do the loop here

return result;
}```

13. Member
Join Date
Jan 2007
Posts
28

## Re: help creating a power program..

ok..so will that work?
Code:
```int integerPower(int power,int exponent)
{
for(i=1;i<=exponent;i++)
integerPower=power*exponent;
return integerPower;
}```
Last edited by elsa; January 19th, 2007 at 05:18 AM.

14. ## Re: help creating a power program..

That is the Pascal way of doing it but in C++ the name of the function is not automatically a variable of the return type.

I showed you how to create the result variable.

You should also put any code you post here into [code] [/code] tags

Your first parameter name should be base. It is a very important part of programming to use meaningful descriptors.

15. Member
Join Date
Nov 2006
Location
Barcelona - Catalonia
Posts
364

## Re: help creating a power program..

Can you answer yourself? What do you think?

First, intergerPower variable is undefined. Error!!
Second, what this makes?
Code:
` integerPower=power*exponent;`
integerPower = 4 * 5 = 20
and then return.
Is 4^5 = 20? I don't think so.

Read NMTop40 post!! So, return must be out of for!!!

Albert.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•