Like Tree2Likes

Euclid method for solving GCD problem

This is a discussion on Euclid method for solving GCD problem within the C Programming forums, part of the General Programming Boards category; hockeybro12, you're killing me! You really need to read what we post carefully. Take a look again at the outline ...

  1. #16
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,425
    hockeybro12, you're killing me! You really need to read what we post carefully. Take a look again at the outline of the loop I provided in post #12, and take a look at the while loop you posted. What is different between the two?

  2. #17
    Registered User
    Join Date
    Jun 2013
    Posts
    27
    Can anyone help me as it is still not working.

    Code:
    #include <stdio.h>
    #include "genlib.h"
    #include "simpio.h"
    
    
    main()
    {
          int num1, num2, rem, x, y;
    
          printf ("This program calculates the GCD of two numbers.\n");
          printf ("Enter the first number: ");
          num1 = GetInteger();
          printf ("Enter the second number: ");
          num2 = GetInteger();
          x = num1;
          y = num2;
          rem=(num1%num2);
          while (rem != 0)
          {
    
                rem=num2;
                num2=num1%rem;
                num1=rem;
            }
            printf("The GCD of %d and %d is %d",x,y,num1);
          getchar();
    }
    

  3. #18
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,425
    Quote Originally Posted by hockeybro12 View Post
    Can anyone help me as it is still not working.
    Of course it's still not working, because YOU DIDN'T CHANGE ANYTHING!!! It's exactly the same code as post #13. And no, we're not going to just give you the answer. Look at the while loop I provided in post #12, and the one you wrote. What is different?

  4. #19
    Registered User
    Join Date
    Jun 2013
    Posts
    27
    Quote Originally Posted by anduril462 View Post
    Of course it's still not working, because YOU DIDN'T CHANGE ANYTHING!!! It's exactly the same code as post #13. And no, we're not going to just give you the answer. Look at the while loop I provided in post #12, and the one you wrote. What is different?
    I did use your solution. The code posted in #17 and #13 is the improved version of the code according to what you provided in 12. I have commented out the other part of what I had before as you can see.

    Code:
    
    #include <stdio.h>
    #include "genlib.h"
    #include "simpio.h"
    
    
    main()
    {
          int num1, num2, rem, x, y;
           
          printf ("This program calculates the GCD of two numbers.\n");
          printf ("Enter the first number: ");
          num1 = GetInteger();
          printf ("Enter the second number: ");
          num2 = GetInteger();
          x = num1;
          y = num2;
          rem=(num1%num2);
          while (rem != 0)
          {
                /*rem = num1 % num2;
                if (rem == 0)
                {
                    printf ("The GCD of %d and %d is %d", x, y, num2);
                }
                else
                {
                    num1 = num2;
                    num2 = rem;
                }
                */
                rem=num2;
                num2=num1%rem;
                num1=rem;
        	}
        	printf("The GCD of %d and %d is %d",x,y,num1);
          getchar();
    }

  5. #20
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There is a difference, between the below, and your actual code logic.


    Here, a=num1, b=num2, and t=temp (in the below pseudo code), but you are using t as the rem (remainder).

    Note that the while loop is b!= 0, not rem != 0, which is what you have.
    Code:
    function gcd(a, b)
        while b ≠ 0
           t := b
           b := a mod t
           a := t
        return a
    It would help if you changed your variables to these same names, for just a minute. That would eliminate some confusion.

  6. #21
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,425
    Quote Originally Posted by hockeybro12 View Post
    I did use your solution. The code posted in #17 and #13 is the improved version of the code according to what you provided in 12. I have commented out the other part of what I had before as you can see.
    &@*$!@&*$&%#%*^(@#^)#$&*@#@&$ No you didn't!!!

    Me (post #12): Here is a loop to use
    You (post #13): Here's my new code, it doesn't work.
    Me (post #17): You got the loop wrong. Reread my post #12.
    You (post #18): I didn't change my broken code, but it's still broken.
    Me (post #19): Of course, if you don't fix broken code, it remains broken. Fix your while loop, look at my post #12.
    You (post #20): I used your solution. Look (same broken code from post #13 and #17 that uses a different while loop than what I provided).
    Me (in this post): &@*$!@&*$&%#%*^(@#^)#$&*@#@&$ No you didn't!!!

    I'm being a bit of an a-hole here because it seems you're just not paying attention. All you had to do was copy-paste the while (num2 != 0) line from my post #12, and you would have this thing solved. Or you could have looked at the line, and typed it character by character. You had similar lack-of-careful-reading-or-thinking-about-what-you-are-doing issues when you were changing == to = with no regard to which lines were already correct and which were problematic, despite me posting warnings with line numbers. If you can't be bothered to read and think carefully about what you're doing, then programming is not for you (actually, there is very little for you if you can't do those two things). Also, if you fail to follow our suggestions or pay attention to us when you asked us for help, we rapidly lose interest in helping you in the future.

  7. #22
    Registered User
    Join Date
    Jun 2013
    Posts
    27
    Quote Originally Posted by anduril462 View Post
    &@*$!@&*$&%#%*^(@#^)#$&*@#@&$ No you didn't!!!

    Me (post #12): Here is a loop to use
    You (post #13): Here's my new code, it doesn't work.
    Me (post #17): You got the loop wrong. Reread my post #12.
    You (post #18): I didn't change my broken code, but it's still broken.
    Me (post #19): Of course, if you don't fix broken code, it remains broken. Fix your while loop, look at my post #12.
    You (post #20): I used your solution. Look (same broken code from post #13 and #17 that uses a different while loop than what I provided).
    Me (in this post): &@*$!@&*$&%#%*^(@#^)#$&*@#@&$ No you didn't!!!

    I'm being a bit of an a-hole here because it seems you're just not paying attention. All you had to do was copy-paste the while (num2 != 0) line from my post #12, and you would have this thing solved. Or you could have looked at the line, and typed it character by character. You had similar lack-of-careful-reading-or-thinking-about-what-you-are-doing issues when you were changing == to = with no regard to which lines were already correct and which were problematic, despite me posting warnings with line numbers. If you can't be bothered to read and think carefully about what you're doing, then programming is not for you (actually, there is very little for you if you can't do those two things). Also, if you fail to follow our suggestions or pay attention to us when you asked us for help, we rapidly lose interest in helping you in the future.
    I'm really sorry. I forgot to change the while loop to num2 instead of rem. I have changed it now and it is working. I will be more careful later.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 01-21-2013, 10:12 AM
  2. Replies: 2
    Last Post: 01-06-2013, 06:49 AM
  3. Simplifying Numerical Solving before actual solving
    By Vespasian in forum Tech Board
    Replies: 3
    Last Post: 10-14-2012, 11:39 AM
  4. GCD using the euclid method.
    By PYROMANIAC702 in forum C Programming
    Replies: 18
    Last Post: 07-23-2011, 09:46 PM
  5. need for a solving method
    By braddy in forum C++ Programming
    Replies: 8
    Last Post: 08-26-2006, 11:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21