943,480 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3312
  • C RSS
May 2nd, 2004
0

I dont see any difference between these 2 functions, DO YOU?

Expand Post »
  1. int gcd (int num1, int num2)
  2. {
  3. int remainder;
  4. if (num1 > num2)
  5. remainder = num1 % num2;
  6. else
  7. remainder = num2 % num1;
  8. if(remainder!=0)
  9. {
  10. return gcd(remainder, num1);
  11.  
  12. }
  13. return num1;
  14. }
  15. int gcd2 (int num1, int num2)
  16. {
  17. int remainder;
  18. return ( remainder = ( num1 > num2 ? num1 % num2 : num2 % num1)==0 ? num1
  19. : gcd2(remainder, num1));
  20. }

its bothering me when passed the same parameters to these two above functions, they give different results.

any idea whats up with them
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
AmericanD is offline Offline
3 posts
since May 2004
May 2nd, 2004
0

Re: I dont see any difference between these 2 functions, DO YOU?

u need extra parentheses around the = assignment
  1. return ( (remainder = ( num1 > num2 ? num1 % num2 : num2 % num1) )==0 ? num1
  2. : gcd2(remainder, num1));
Reputation Points: 47
Solved Threads: 2
Junior Poster in Training
infamous is offline Offline
77 posts
since Mar 2004
May 2nd, 2004
0

Re: I dont see any difference between these 2 functions, DO YOU?

You are right. Thank you. program fixed
Reputation Points: 10
Solved Threads: 0
Newbie Poster
AmericanD is offline Offline
3 posts
since May 2004

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: XP style in DOS !
Next Thread in C Forum Timeline: Can you add pictures/sounds in a win32 console app?





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


Follow us on Twitter


© 2011 DaniWeb® LLC