#1




Recursive pow(base,exponent) miniprogram.Hey everyone, i'm new to this forum so I wasn't sure how to format the code. (I read the howto, but it was slightly confusing) Its very simple so I figured it was ok to leave it like this.
The object of my code (in the c programming language) is to basically to write a program that does what pow(x,n) does. The only difference is that I am using two different formulas. One for even exponents and one for odd exponents. In this code I am only dealing with even exponents right now. The formula is: (x^(n2))^2. I am having trouble dealing with the last ^2. This should be the final value of total multiplied by itself. So x^4 * x^4 = x^16. Lastly, should I be separating my main (program that calls pow) and my function into different programs.. even though my main is so small? My Code: CPP / C++ / C Code:


#2




Re: Recursive pow(base,exponent) miniprogram helpQuote:
Actually you have implemented a function that returns x * pow(x, n2), not pow(x, n2) * pow(x, n2) at each recursion for n > 0. The important thing is that neither the function that you defined nor the function that you implemented can be applied recursively to obtain anything remotely related to pow(x, n) for n > 0 whether n is even or not. For either one, step through a couple of cases ("by hand," not on the computer) and see what you get for pow(2, 2), pow(2, 4), etc. Wasn't the statement of the recursion somewhat like the following? (In addition to using a temporary variable to reduce the number of recursion calls to one at each step, I have renamed the function to foo, instead of using the name pow. I have my reasons, but don't ask...) Code:
I mean, there may be other ways of defining a power function recursively (other than the obvious one of returning foo(x, n1)) but this is the usual "fast power" function recursive definition. (See Footnote.) Quote:
In C, you can't have functions embedded in other functions. So, they would be separate. Not necessarily in separate files, but separate functions. I might do something like the following: CPP / C++ / C Code:
Then, if you do it right, a run could look like: Code:
If your function gives different (wrong) values, you might be able to see why if you put a print statement at the beginning of the function: CPP / C++ / C Code:
Regards, Dave Footnote: My pseudocode implements the parts of the Exponention by squaring algorithm that hold for n >= 0. __________________
Sometimes I just can't help myself... Last edited by davekw7x : 29Oct2011 at 15:36.

#3




Re: Recursive pow(base,exponent) miniprogram helpOk, thanks a lot Dave! I have not been programming for very long and so recursion was something that was a little tougher to grasp. Your suggestion to write it down helped a lot as I was able to trace through the recursion calls much more easily than through my head. I have a much better understanding of recursion now which helped me to finish the question. Here's the code I wrote. It only applies to positive integers (base and exponent). I tried to brainstorm some other ways to write the algorithm but the algorithm is so short and simple that I couldn't figure it out anymore. (Please suggest! As I want to become as openminded as possible.)
Also on a side note, I tried to check if the base and exponent value were negative in the first part of the code. However, it asks me to enter the values twice when I reach this case. I cannot see why. p.s. My formatting is bad. CPP / C++ / C Code:

#4




Re: Recursive pow(base,exponent) miniprogram helpQuote:
CPP / C++ / C Code:
to CPP / C++ / C Code:
Quote:
So: Fix it. Get into the habit of entering code the way that you want it to look. What editor are you using? "Real" programming editors have "autoindent" functionality that might help. For starters here are a couple of suggestions: Begin with lines starting at the lefthand margin. For each function put the opening '{' brace on a separate line after the function header definition, and indent the following lines four spaces. Then... In the code, whenever you have a '{' brace, indent the following lines four spaces. Whenever you have a '}' brace, unindent (outdent?) four spaces before intering the '}' brace. Besides making it look neater (and easier for us mere humans to follow your organization) it can help you spot unbalanced pairs, since each function begins and ends with lines starting at the left margin. Regards, Dave Footnote: If you go back to the main() function that I showed in my example and if you eliminate the testing that you added to your exponent function, what happens if you use a negative base and a positive exponent value? Does it work? Why would it not work? Do you really need to guard against negative base values? __________________
Sometimes I just can't help myself... 
#5




Re: Recursive pow(base,exponent) miniprogram helpUnderstood. I will start to program in a more organized manner.
The programming editor I am using is EMACS. I am using the editor within a unix terminal that connects to my school network. I have no graphical interface on my labtop. I usually sketch out my algorithms in notepad / java eclipse (for indentation / formatting) and then finish it in EMACS. CPP / C++ / C Code:
 Getting rid of the \n eliminated the problem. Thanks. With regards to your main function. Testing it out, negative numbers still work for the base simply because multiplying the bases together eventually cancels the negatives out. Negatives do not work for the exponents. The main reason being that we use n == 0 as our base case. As well, our recursive calls use n as well. Starting off with a negative exponent, changes the recursive calls and therefore the final value we receive. 
#6




Re: Recursive pow(base,exponent) miniprogram helpQuote:
Well, here's my take on it: The function could be midified so that the value of anything raised to a negative exponent would be calculated as 1/foo(x,n). (Since, if n is negative, then n is positive, and that's how the power function would work.) The main problem (if you want to call it a problem) is that the negative integer power of any nonzero base value other than 1 will be have a fractional value (less than 1). So, for example, pow(2, 3) is 8 but pow(2, 3) is 1/8. This is zero if you stick with integer arithmetic, but is 0.125 if you were doing floating point arithmetic. However...the whole point (I think) is to do everything with integer arithmetic, so things like foo(2, 3) should give zero as its value (or maybe in main(), users would be forbidden to enter negative values for the exponent). Regards, Dave __________________
Sometimes I just can't help myself... 
#7




Re: Recursive pow(base,exponent) miniprogram helpO, ok. One last thing. Is it possible for you or some moderator to remove my code in my second post? (For various reasons) I am unable to edit it anymore. Once again, thanks for your input / advice. It helped a lot.

#8




Re: Recursive pow(base,exponent) miniprogram helpQuote:
Please read this. 
#9




Re: Recursive pow(base,exponent) miniprogram helpQuote:
O ok, can I request to have this post removed then please Thanks admin. 
#10




Re: Recursive pow(base,exponent) miniprogram helpI'm sorry, that's not how it works. You have to write in explaining your reasons for the request to delete.
If others have replied or helped in even a small way here, it is very likely that nothing will be deleted. 
«
Previous Thread

Next Thread
»
Thread Tools  Search this Thread 
Rate This Thread  



Similar Threads  
Thread  Thread Starter  Forum  Replies  Last Post 
Binary Tree recursive Functions  90fmm  C++ Forum  3  27Apr2011 18:37 
NetBeans  Inventory Program  biglez123  Java Forum  1  23Jan2011 20:48 
[TUTORIAL] Calling an external program in C (Linux)  dsmith  C Programming Language  4  22Apr2005 13:30 
Need help with a C program (Long)  McFury  C Programming Language  3  29Apr2004 20:06 
Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The
All times are GMT 6. The time now is 08:07.