Thread: NEED help with returning the GCD

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    18

    NEED help with returning the GCD

    umm, i need help with this, i have no idea how to make it return the greatest common divisor, this code works without returning anything, but i need to make the code to RETURN it......i gotten everything down, just donno how to return the answer....



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int	GCD( int a,int b, int r);
    int main()
    {
    	int	r,a,b;
    
          printf("Please enter two integers separated by a space: ");
          scanf("%d %d",&a,&b);
    
          printf("\nYou entered: %d %d ",a,b);
        
          if(a+b>0)
          {
            while (b)
            {
                  r = a % b;
                  a = b;
                  b = r;
    	    }
             r=GCD(a,b,r);
             printf("\n\n\nGCD: %d\n\n",r);
          }
          else
          {
             return 0;
          }
    
          system("PAUSE");
          return 0;
    }
    
    int GCD(int a,int b, int r)
    {
    
       return ;
    
    }

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    First off you need to put the algorithm code inside the function.
    Second, a GCD function only takes two parameters. r should simply be a local variable.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    well, i think i fixed it but its not giving me the answer correctly...for example if u put 32 and 12, the answer is supposed to be 4, but it gives it as 0

    but if u "return a" it happens to work correctly, but isnt this program supposed to return gcd or r since it's being called..? why isnt it returning r correctly?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int	GCD( int a,int b);
    int main()
    {
    	int	r,a,b;
    
          printf("Please enter two integers separated by a space: ");
          scanf("%d %d",&a,&b);
    
          printf("\nYou entered: %d %d ",a,b);
        
          if(a+b>0)
          {
             r=GCD(a,b);
             printf("\n\n\nGCD: %d\n\n",r);
          }
          else
          {
             return 0;
          }
    
          system("PAUSE");
          return 0;
    }
    
    int GCD(int a,int b)
    {
        int r;
        while (b)
        {
              r = a % b;
              a = b;
              b = r;
        }
    
       return r;
    }

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    That looks much better!
    It does need to return a instead of r. Afterall b is set equal to r, and we only exit the loop when b is zero, so at that point r will be zero too.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Gcd on three numbers
    By Ideswa in forum C Programming
    Replies: 7
    Last Post: 03-19-2009, 01:57 PM
  2. function returning hour in either 12 or 24 hour format
    By stanlvw in forum C Programming
    Replies: 4
    Last Post: 01-01-2008, 06:02 AM
  3. Recursion: base case returning 1, function returning 0
    By yougene in forum C Programming
    Replies: 5
    Last Post: 09-07-2007, 05:38 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. GCD For all of You!
    By Argentina in forum C Programming
    Replies: 0
    Last Post: 02-14-2002, 04:38 PM