User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 391,705 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,257 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 436 | Replies: 7 | Solved
Reply
Join Date: May 2008
Posts: 52
Reputation: lich is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 4
lich lich is offline Offline
Junior Poster in Training

Recursion Question. If anyone Can help me

  #1  
May 26th, 2008
Teacher gave a question today to find Base is 2 and power is 3. so that Answer should be 8. so to find that she told us to use Recursion and do it. i done it in the normal way. but when i do it in the Recursion way. one error coming saying too Few Parameters

please check
  1. #include<stdio.h>
  2.  
  3. int multi(int,int);
  4.  
  5. main()
  6. {
  7. int x=0,y=0;
  8.  
  9.  
  10. printf("Enter Your Base Number:");
  11. scanf("%d",&x);
  12.  
  13.  
  14. printf("Enter the Power Number:");
  15. scanf("%d",&y);
  16.  
  17. printf("The power of %d is %d",x,multi(x,y));
  18.  
  19. }
  20.  
  21. int multi (int x, int y)
  22. {
  23. int ans=0;
  24. if(y<=1)
  25.  
  26. return 1;
  27.  
  28. else
  29. {
  30. ans=(x*multi(x,y-1));
  31. return ans;
  32. }
  33. }

PLEASE HELP ME IN THIS
Last edited by Ancient Dragon : May 26th, 2008 at 2:37 pm. Reason: add code tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2008
Location: Seattle
Posts: 699
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 45
jephthah's Avatar
jephthah jephthah is offline Offline
Practically a Master Poster

Re: Recursion Question. If anyone Can help me

  #2  
May 26th, 2008
here's your problem:
Originally Posted by lich View Post
if(y<=1)
return 1;


it should be:
  1. if(y==1)
  2. return x;
  3. if(y<=0)
  4. return 1;


.
Last edited by jephthah : May 26th, 2008 at 1:09 pm.
Why so serious?
Reply With Quote  
Join Date: May 2008
Posts: 52
Reputation: lich is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 4
lich lich is offline Offline
Junior Poster in Training

Re: Recursion Question. If anyone Can help me

  #3  
May 26th, 2008
thanks my bad. in my coding i have put if(y<=1) it should be ( y<1) only . no equal sign.

thanks guys. thanks for the quick reply
Reply With Quote  
Join Date: Feb 2008
Location: Seattle
Posts: 699
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 45
jephthah's Avatar
jephthah jephthah is offline Offline
Practically a Master Poster

Re: Recursion Question. If anyone Can help me

  #4  
May 26th, 2008
yeah, i guess mine was redundant. i still dont think well in recursion.
Why so serious?
Reply With Quote  
Join Date: Mar 2007
Posts: 10
Reputation: articles is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
articles articles is offline Offline
Newbie Poster

Re: Recursion Question. If anyone Can help me

  #5  
May 28th, 2008
The program you have written using recursion is logically wrong..

You have written the code if (y<=1) return=1
How can 2 power -1 will be equal to one..

So correct your program first and then see the results..
Reply With Quote  
Join Date: Feb 2008
Location: Seattle
Posts: 699
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 45
jephthah's Avatar
jephthah jephthah is offline Offline
Practically a Master Poster

Re: Recursion Question. If anyone Can help me

  #6  
May 28th, 2008
because he doesnt have to handle negative powers for his CS 101 class.

silly.
Why so serious?
Reply With Quote  
Join Date: Sep 2006
Posts: 260
Reputation: Colin Mac is on a distinguished road 
Rep Power: 2
Solved Threads: 16
Colin Mac Colin Mac is offline Offline
Posting Whiz in Training

Re: Recursion Question. If anyone Can help me

  #7  
May 28th, 2008
Maybe not. I'd recommend taking initiative even so. It's how students stand out from the rest and it shouldn't be difficult.
http://www.purplemath.com/modules/exponent2.htm
Reply With Quote  
Join Date: Feb 2008
Location: Seattle
Posts: 699
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 45
jephthah's Avatar
jephthah jephthah is offline Offline
Practically a Master Poster

Re: Recursion Question. If anyone Can help me

  #8  
May 28th, 2008
i agree. i thought about including that in my original answer, but decided to stick to the question as presented. some of the posters here (not necessarily this one) sort of implode if you throw too much at them.

anyhow... yeah, change your answer variable to type "double" , use the absolute value of the power to calculate the answer, and then throw this in at the end:

if (pow<0) ans = 1.0/ans;

pretty simple, really.



.
Last edited by jephthah : May 28th, 2008 at 3:11 pm.
Why so serious?
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 2:52 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC