
January 18th, 2007, 05:46 AM
#1
[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.

January 18th, 2007, 06:11 AM
#2
Re: help creating a power program..

January 18th, 2007, 06:12 AM
#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

January 18th, 2007, 06:23 AM
#4
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 06:15 AM.

January 18th, 2007, 06:24 AM
#5
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

January 18th, 2007, 06:35 AM
#6
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.
Please, correct me. I'm just learning.... and sorry for my english :)

January 18th, 2007, 06:49 AM
#7
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?

January 18th, 2007, 06:55 AM
#8
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
Please, correct me. I'm just learning.... and sorry for my english :)

January 18th, 2007, 07:02 AM
#9
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 06:27 AM.

January 18th, 2007, 07:21 AM
#10
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

January 18th, 2007, 07:24 AM
#11
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.
Please, correct me. I'm just learning.... and sorry for my english :)

January 18th, 2007, 07:40 AM
#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;
}

January 18th, 2007, 07:46 AM
#13
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 06:18 AM.

January 18th, 2007, 07:51 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.

January 18th, 2007, 07:56 AM
#15
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.
Please, correct me. I'm just learning.... and sorry for my english :)
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
This a Codeguru.com survey!
OnDemand Webinars (sponsored)
