#1




Recursive function power(base,exponent)I have an assignment that I think has some logical errors in the base case and recursive step. My program actually calculates values correctly, but I think it can use some improvements in the logic. Can someone help me fix it so that it will be logically correct?
Here are the instructions given by my instructor: Write a recursive function power(base,exponent) that, when invoked, returns base^exponent For example, power( 3, 4 ) = 3 * 3 * 3 * 3. Assume that exponent is an integer greater than or equal to 1. Hint: The recursion step would use the relationship base exponent = base · base ^ exponent 1 and the terminating condition occurs when exponent is equal to 1 because base^1 = base Below is my code: CPP / C++ / C Code:


#2




Re: Recursive function power(base,exponent)Hi Thu,
Suppose if the user enters 0 as the exponent, the return answer should be 1. So, here is the modified recursion function: CPP / C++ / C Code:
The logic is that you must multiply base until exp becomes zero. i.e: consider that user enters 3 and 4. So, CPP / C++ / C Code:
base * base * base * base * 1 There is one more problem. What if the user enters negative numbers for the exponent value? Think about that and add the code for that also. Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. 
#3




Re: Recursive function power(base,exponent)Quote:
Suppose the reader of posts keep in mind the instructions for the program Quote:
__________________
Definition: Politics Latin, from poly meaning many and tics meaning blood sucking parasites  Tom Smothers 
#4




Re: Recursive function power(base,exponent)Paramesh,
it works like a charm! Thanks, mate. 
«
Previous Thread

Next Thread
»
Thread Tools  Search this Thread 
Rate This Thread  



Similar Threads  
Thread  Thread Starter  Forum  Replies  Last Post 
[Include] Doublylinked List  dsmith  C Programming Language  6  14Apr2006 13:12 
[Tutorial] Function Pointers  aaroncohn  C++ Forum  4  17Feb2006 12:33 
Major problem with recursive function, help..  kakamuti  C Programming Language  4  19Dec2004 08:47 
Revising Script style ??????  pepee  MySQL / PHP Forum  4  14Apr2004 04:59 
Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The
All times are GMT 6. The time now is 08:03.