RSS Forums RSS

Simple Recursion (Multiplying by using Addition)

Please support our C++ advertiser: Programming Forums
Thread Solved
Reply
Posts: 219
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz in Training

Simple Recursion (Multiplying by using Addition)

  #1  
Dec 4th, 2008
Hello all,

My objective is simple. I have to take in 2 positive integers and multiply them by using addition. This is a recursive problem.

Example of Output:


Please enter 2 positive integers to multiply:

4 2

4 * 2 = 8



The problem is whenever the user types in both numbers, the program window just disappears.

To receive my result, I am suppose to use something like:

x + multiply(x,--y);


Please help me with this...Thank you


  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int multiply(int x, int y);
  6.  
  7. int main()
  8. {
  9.  
  10. int x;
  11. int y;
  12.  
  13.  
  14. cout<<"Please enter 2 positive integers to multiply"<<endl;
  15. cin>>x;
  16. cin>>y;
  17.  
  18. cout<<x<<" * "<<y<<" = "<<multiply(x,--y)<<endl;
  19.  
  20.  
  21. system ("PAUSE");
  22. return 0;
  23. }
  24.  
  25.  
  26. int multiply(int x, int y)
  27. {
  28.  
  29.  
  30. if (x == 0)
  31. {
  32. return 0;
  33. }
  34.  
  35. if (x == 1)
  36. {
  37. return x;
  38. }
  39.  
  40. if (x > 1)
  41.  
  42. {
  43. return (x + multiply(x,--y));
  44. }
  45.  
  46. }
AddThis Social Bookmark Button
Reply With Quote  
Posts: 5,133
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 634
Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Simple Recursion (Multiplying by using Addition)

  #2  
Dec 4th, 2008
You test x, and only change y
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
UK Voter? Please send a message to Incapability Brown and the rest of Zanu-Labour
Up to 8Mb PlusNet broadband from only £5.99 a month!
Reply With Quote  
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: Simple Recursion (Multiplying by using Addition)

  #3  
Dec 4th, 2008
As long as you are decrementing y you should put y in your base cases checks, otherwise multiply(2, wathever); would result in *endless* recursion.

EDIT: Sorry, Salem beated me in speed.
Reply With Quote  
Reply

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



Other Threads in the C++ Forum
Views: 460 | Replies: 2 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 3:01 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC