943,915 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2147
  • C++ RSS
Dec 4th, 2008
0

Simple Recursion (Multiplying by using Addition)

Expand Post »
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


C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 8
Solved Threads: 0
Posting Pro in Training
NinjaLink is offline Offline
416 posts
since Mar 2008
Dec 4th, 2008
0

Re: Simple Recursion (Multiplying by using Addition)

You test x, and only change y
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Dec 4th, 2008
0

Re: Simple Recursion (Multiplying by using Addition)

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.
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help (link lists referent)
Next Thread in C++ Forum Timeline: Passing Linked Lists - Syntax





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC