Hi! I'm having a problem to compute a power funtion. A user inputs x and n,so I have to compute X to power N. This program should not use any build in functions at all.
Since this is apparently the thread people will respond to I'll post what I said here as well.
Use a loop that multiplies x by itself n times. I don't want to answer the question for you, but if you need more help let me know and I'll try to point you in the right direction.
Now I'm a double threat as well I suppose. Better neutralize that right quick.
Hey! "I'll try to point you in the right direction."
Please do,I was thinking...er...I'm glueless it just seems so difficult without using build in functions. Please give me an example, it will. And I'll modify it myself.
Well he wants a specific (user defined?) input for x and n I'm guessing.
You need to set up a loop that says
1 2 3 4 5 6 7 8
int result=1;
for(int i=0;i<n;i++)
{
result= result*x;
}
You have to think in math terms of what an exponent is. All it really represents is a number multipled by itself. Thus after the first iteration of this loop, result will equal x^1 (since result starts at 1). Every iteration that follows, result will go up by 1 power.
Now the initilizing of result is VERY important for this to work properly. Result must equal one (at first) because for the case that n=1 the result is x, thus if result=0 was used you would result in 0 for x^1 not x.
I'll leave you to get the x and n from cin as well, since it shouldn't be a problem. If you need help with anything else let me know.
I know, but yours seemed pretty confusing for such a simple assignment
Oh yeah, it will be correct without checks. I forgot that my code from my homework never initilized the variable until it did the if checks. Oh how far I've come...and how much farther there is to go.
I told him to set up a loop that multiplies x by itself n times in my first comment. I'd say that was a fairly good start. It would also have told me if he even knew what a loop was.