why wont my code compute the mathmatics I Programmed here?

I'm not to knowledgeable in c++ but I'm pretty sure I did the right thing here (well obviously not). When I run the program I get no errors but all the program does is say hit any key to continue. I'm I crazy or did I make a mistake so small here (if it were a snake I would have been bit)I can't see it?

here's my code

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` ``````#include using namespace std; double Power(float X, int N) { cout<<"please enter value for X: "; cin>> X; cout<> N; cout<

what the hell is going on here???
If you think that you are doing right things then the program bahaves correctly. You inserted statement

system ("PAUSE");

and the program executed it. Why are not you satisfied>!
I'm surprised it runs quite honestly. You passed the argument of a declaration of two variables float x and int n which would be undefined I would assume and then prompt for input on those variables.

You should either prompt in main and pass the variables or have a function with no parameters call the function with no parameters.
Line 33: This is not how you call functions, actually this is a forward declaration (C++ actually let you do forward declaration inside functions. Don't ask me why)
Wow, is that what that is. I'm starting to think its because the compiler believes its a function prototype. Therefore it saw it and the function and skipped even calling the function with the function prototype.
By the way what does this line mean?

(X, N) = (X,N/2) * (X,N/2);

In fact it is equivalent to

N = ( N / 2 ) * ( N / 2 );
Last edited on
What I thought I was doing was computing with the function Power(X,N) a recursive algorithm like this
X^n=X^n/2 * X^n/2 if n is even, and
=X^n/2 * X^n/2+1 if n is odd
=X if N =1
I thoght in c++ (x,n) is the way you format x^n
im trying to do this without using the pow function.
What do I do since i was so freakin off with my assumption?
First of all, you need to move the lines that take input outside of Power(), or else you'll find yourself taking input over and over again when the function recurses.

Second, you need to actually recurse by having the function call itself. Say, by doing Power(x, n/2). Notice that instead of doing
 ``1234`` ``````if (n%2==0) result = Power(x, n/2) * Power(x, n/2); else result = Power(x, n/2) * Power(x, n/2 + 1);``````
it's preferable to do
 ``1234567`` ``````if (n%2==0){ result = Power(x, n/2); result = result * result; }else{ result = Power(x, n/2); result = result * result * x; }``````
It's always best to avoid consecutive recursive calls if at all possible.

Another thing is that 1%2==1, so the condition on line 22 will never be evaluated. You should check for the base case of a recursive function as soon as possible:
 ``12345`` ``````double Power(float X, int N){ if (N==1) return X; //... }``````
Generally speaking, it's always best to check for exceptional or degenerate cases early, so that you can concentrate on the general cases in the bulk of the code.
For example, if you have code that draws a circle, you want to check if the radius is 0 as soon as possible so that you can call the point drawing routine and let the circle drawing routine focus on drawing well-behaved circles.
Thanks helios i was thinking i need to check the base first but i thought i had to follow the format of the given example. I'm gonna fix that function to.
Many Thanks!!!
Topic archived. No new replies allowed.