Simple Recursion (Multiplying by using Addition)

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2008
Posts: 365
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz

Simple Recursion (Multiplying by using Addition)

 
0
  #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. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
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: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Simple Recursion (Multiplying by using Addition)

 
0
  #2
Dec 4th, 2008
You test x, and only change y
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
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)

 
0
  #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 Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC