Ouputing GCD and simplest fractions!?!?!?!?!?!?!?!

This is a discussion on Ouputing GCD and simplest fractions!?!?!?!?!?!?!?! within the C Programming forums, part of the General Programming Boards category; How can i make it find the GCD. My function i have does not reduce it. How can i make ...

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

    Question HELP ME!?!?!

    How can i make it find the GCD. My function i have does not reduce it. How can i make it reduce. Please HELP!

    Code:
    #include <stdio.h>
    #include <math.h>
    
    typedef struct fraction{
        int n;
        int d;
    }Frac;
    
    
    Frac getFrac(void);
    void addFrac(Frac a,Frac b,Frac c,Frac e,Frac *tot);
    Frac gcd(Frac tot);
    
    //////////////////////////////////////////////MAIN/////////////////////////////////////////////
    int main()
    {
        Frac a={0,1},b={0,1},c={0,1},e={0,1},tot;
        a=getFrac();
        b=getFrac();
        c=getFrac();
        e=getFrac();
        addFrac(a,b,c,e,&tot);
    	gcd(tot);
    	printf("GCD is:%d",a);
    	return 0;
    }
    
    ////////////////////////////////////////////FUNCTIONS//////////////////////////////////////////
    
    //getting fractions
    Frac getFrac(void)
    {
        Frac a;
    
        printf( "Enter Fraction\n" );
        scanf( "%d%*c%d",&a.n,&a.d );
        while ( a.d==0 ) {
            printf( "Enter Fraction Again\n" );
            scanf( "%d%*c%d",&a.n,&a.d );
        }
        return a;
    }
    
    
    //adding
    void addFrac(Frac a,Frac b,Frac c,Frac e,Frac *tot)
    {
        int sum1,sum2,numer1,denom1,sum3,sum4,sum5,sum6,numer2,denom2,totalnumer,totaldenom;
    	float z;
        sum1=a.d*b.n;
        sum2=a.n*b.d;
        numer1=sum1+sum2;
        denom1=a.d*b.d;
        printf( "\na+b= %d/%d",numer1,denom1 );
    
        sum3=c.d*e.n;
        sum4=c.n*e.d;
        numer2=sum3+sum4;
        denom2=c.d*e.d;
    
        printf( "\nc+e= %d/%d\n",numer2,denom2 );
    
        sum5=denom1*numer2;
        sum6=denom2*numer1;
        totalnumer=sum5+sum6;
        totaldenom=denom1*denom2;
        tot->n=totalnumer;
        tot->d=totaldenom;
        //total frac result
    	printf("\nTotal Fraction Added is: %d/%d\n",tot->n,tot->d );
    	//decimal
    	z=(float)tot->n/tot->d;
    	printf("\nTotal Decimal is: %.5f\n",z);
    }
    
    
    //greatest common denominator
    int gcd(Frac tot)
    {
    	if(tot.d==0)
    	{
    		return tot.n;
    	}
    	else
    	{
    		return gcd(tot);
    	}
    }
    thanks for the help!
    Last edited by SledMan2002; 11-26-2002 at 04:58 PM.
    Dave

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >How can i make it reduce.
    Code:
    int gcd ( int m, int  n )
    {
      return ( n != 0 ) ? gcd ( n, m % n ) : m;
    }
    
    void reduce ( int *n, int *d )
    {
      int div = gcd ( *n, *d );
    
      if ( div != 0 ) {
        *n /= div;
        *d /= div;
      }
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    18

    ok?

    where do i put that in my code and get it 2 work?
    Dave

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >where do i put that in my code and get it 2 work?
    Just call it wherever you need to reduce a fraction and place the definition before the first use. :P

    -Prelude
    My best code is written with the delete key.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    18

    Ouputing GCD and simplest fractions!?!?!?!?!?!?!?!

    everything else works, besides the dang GCD and the simplfy, i jus dont get it, please help me as much as u can!

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <conio.h>
    
    
    typedef struct fraction{
        int n;
        int d;
    }Frac;
    
    
    Frac getFrac(void);
    int addFrac(Frac a,Frac b,Frac c,Frac e,Frac *tot);
    int gcd(d,r,denom1,denom2);
    int simp(reduceNumer,reduceDenom,d);
    
    //////////////////////////////////////////////MAIN/////////////////////////////////////////////
    int main()
    {
        Frac a={0,1},b={0,1},c={0,1},e={0,1},tot,d,r,denom1,denom2,reduceNumer,reduceDenom;
        a=getFrac();
        b=getFrac();
        c=getFrac();
        e=getFrac();
    
    //printing inputed
    	printf("Input Reads...\n");
    	printf("\t\t%d/%d\t",a.n,a.d);
    	printf("\n\t\t%d/%d\t",b.n,b.d);
    	printf("\n\t\t%d/%d\t",c.n,c.d);
    	printf("\n\t\t%d/%d\t",e.n,e.d);
        
    	addFrac(a,b,c,e,&tot);
    	gcd(d,r,denom1,denom2);
    	printf("GCD is: %d\n",d);
    	simp(reduceNumer,reduceDenom,d);
    	return 0;
    }
    
    ////////////////////////////////////////////FUNCTIONS//////////////////////////////////////////
    
    //getting fractions
    Frac getFrac(void)
    {
        Frac a;
    
        printf( "Enter Fraction\n" );
        scanf( "%d%*c%d",&a.n,&a.d );
        while ( a.d==0 ) {
            printf( "Enter Fraction Again\n" );
            scanf( "%d%*c%d",&a.n,&a.d );
        }
    	system("cls");
        return a;
    
    }
    
    
    //adding
    int addFrac(Frac a,Frac b,Frac c,Frac e,Frac *tot)
    {
    
        int sum1,sum2,numer1,denom1,sum3,sum4,sum5,sum6,numer2,denom2,totalnumer,totaldenom;
    	float z;
        sum1=a.d*b.n;
        sum2=a.n*b.d;
        numer1=sum1+sum2;
        denom1=a.d*b.d;
    
        sum3=c.d*e.n;
        sum4=c.n*e.d;
        numer2=sum3+sum4;
        denom2=c.d*e.d;
    
        sum5=denom1*numer2;
        sum6=denom2*numer1;
        totalnumer=sum5+sum6;
        totaldenom=denom1*denom2;
        tot->n=totalnumer;
        tot->d=totaldenom;
        //total frac result
    	if(tot->d==1)
    	{
    		printf("Fraction is: %d",(tot->n/tot->d));
    	}
    	else
    	{
    	printf("\nTotal Fraction Added is: %d/%d\n",tot->n,tot->d );
    	}//decimal
    	z=(float)tot->n/tot->d;
    	printf("\nTotal Decimal is: %.5f\n",z);
    return denom1,denom2;
    }
    
    //greatest common denominator 
    int gcd(d,r,denom1,denom2) 
    { 
        d = denom2; 
        r = denom1; 
         
        while( r != 0 ) 
         {  
            d = r; 
            r = d % r; 
        } 
        return r,d; 
    }
    
    //simplify
    int simp(Frac tot,int d)
    {
    int reduceNumer,reduceDenom;
    reduceNumer=tot.n=tot.n/d;
    reduceDenom=tot.d=tot.d/d;
    
    printf("\nLowest Terms is: %d/%d\n",reduceNumer,reduceDenom);
    return 0;
    }
    thanks for your help. I appreciate it VERY much. Jus edit my code and tell me what it is u are doing, comments are cool. Thanx!
    Dave

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >everything else works, besides the dang GCD and the simplfy, i jus dont get it, please help me as much as u can!
    No, your question has been answered in at least three different threads all started by you. I even gave you the FULL code to reduce a fraction AND told you how to use it. If you still can't solve your problem then I suggest you pick up a book on C and start reading because everything has been handed to you already, all you have to do is cut and paste. Last time I checked that wasn't a difficult thing to do.

    Do NOT start another thread asking the same thing just because we didn't write your program for you.

    -Prelude
    My best code is written with the delete key.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I had to merge the threads.

    As I said already, and as Prelude said, don't not start more threads. Keep to one all the time you're on the same topic.

    Any more new threads on this matter may be deleted without warning.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

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