Thread: Problem with reducing fraction program

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    14

    Problem with reducing fraction program

    I have a program where it will take any fraction and reduce it if possible, but my only problem with it is prime numbers. If the user inputs something such as 11/7, it will return 2 + 1/7 instead of 2 + 3/7, or if 7/11 is entered, a big mess is just returned. Everything else works fine though. Not sure where to go from here. Here is my code so far.


    Code:
    #include <stdio.h>
    
    int main (void)
    
    
        {
        int a, b, temp=0,m=0;  
        int gcd(int,int);
        int lcm(int,int);
        int whole, minus, top, bottom, top2, bottom2;
    	
    
        printf ("Enter fraction: ");
        scanf ("&#37;d/%d",&a,&b);
    
        whole = a / b; 
        minus = a - (whole*b);
        top = lcm(a,b) / b;
        bottom = lcm(a,b) / gcd(a,b);
    
        if ((a-b<=0))
    
    
            {
            temp=a;
            a=b;
            b=temp;
        }
    
        if(whole == 0)  
            printf("result: %d/%d\n", top, bottom);
    
        while(whole != 0){ /
        if(a % b == 0) 
            printf("result: %d\n", whole);
            break;
            }
    
        if( a % b > 0 && a <= 9) /
            printf("result: %d + %d/%d\n", whole, minus, b);
    
            top2 = top / top;  
            bottom2 = bottom / top;
    
        if( a % b > 0 && a > 9) 
            printf("result: %d + %d/%d\n", whole, top2, bottom2);
    
        printf("PROGRAM ENDS\n");
        return 0;
    }
    int gcd(a,b)  
    
    
        {
        int t,r;
        r=a%b;
        if (r==0)
        return b;
        else
        return gcd(b,r);
    }
    int lcm(a,b) 
    
    
        {
        int m,n;
        m=a*b;
        n=m/gcd(a,b);
        return n;
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Don't put function prototypes inside main.
    Work on your indenting too.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by Turtal View Post
    ...but my only problem with it is prime numbers.
    I don't think so.

    1) You don't need the lcm() function to do the math you need to do.
    2) You have WAY too many variables
    3) You don't need to swap "a" and "b" before calling gcd().
    4) Your code is poorly structured regardless of poor indentation.

    Here are the steps you need to do:

    1) check for a user supplied denominator of zero. Exit if supplied.
    2) Figure out the whole portion - you got that.
    3) If there is a remaining fraction, (not zero or 1), call gcd with the remaining fraction and the user supplied denominator. Then, reduce both of those values by the gcd.
    4) print out the results.

    Todd

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    P.S. After you gets yours working, I'll post mine.

    Todd

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    14
    I finaly got it to work. I know it's messy and completely drawn out, but the only way I seemed to understand how to do this, is when I had a problem, I created more code to fix the problem rather than just fix what I have, = 0
    hence the spagetti. It still has issues with prime numbers though, such as 7/11 etc, but I got it to work where if the numerator is larger than the denom, such as 11/7, it now works fine.



    Code:
    #include <stdio.h>
    
    int main (void)
    
        {
        int a, b, temp=0,m=0;
        int gcd(int,int);
        int lcm(int,int);
        int whole, minus, top, bottom, top2, bottom2, newnom;
    	
    
        printf ("Enter fraction: ");
        scanf ("&#37;d/%d",&a,&b);
    
        whole = a / b;
        minus = a - (whole*b);
        top = lcm(a,b) / b;
        bottom = lcm(a,b) / gcd(a,b);
    
        if ((a-b<=0))
        {
            temp=a;
            a=b;
            b=temp;
        }
    
        if(whole == 0)
            printf("result: %d/%d\n", top, bottom);
    
        while(whole != 0){ 
        if(a % b == 0) 
            printf("result: %d\n", whole);
            break;
            }
    
        if( a % b > 0 && a <= 9) 
            printf("result: %d + %d/%d\n", whole, minus, b);
    
            newnom = a % b; 
            top2 = newnom / gcd(a,b);
            bottom2 = bottom / top;
    
        if( a % b > 0 && a > 9) 
    	
            printf("result: %d + %d/%d\n", whole, top2, bottom2);
    
        printf("PROGRAM ENDS\n");
        return 0;
    }
    int gcd(a,b)
    {
        int t,r;
        r=a%b;
        if (r==0)
        return b;
        else
        return gcd(b,r);
    }
    int lcm(a,b)
    {
        int m,n;
        m=a*b;
        n=m/gcd(a,b);
        return n;
    }
    [/CODE]

    P.S. I took out the part where if the user entered zero, the program would ask them to enter a nonzero number, because it wasn't necessary and the compiler website my prof. used to grade the program didn't require it.
    Last edited by Turtal; 12-06-2007 at 03:00 PM.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Behold... some beautifully simple, nicely indented, meaningful variable named code:

    Code:
    #include <stdio.h>
    
    int gcd(int,int);
    
    int main (void) {
    	
    	int numerator, denominator = 1 ; 
        int whole, portion , divisor = 1 ; 
    	
        printf ("Enter fraction: ") ;
        scanf ( "&#37;d/%d" , &numerator, &denominator ) ;
    
    	if (denominator == 0) { 
    		printf("Invalid Denominator of 0\n") ; 
    		return 8 ; 
    	} 
    	
        whole = numerator / denominator ; 
        portion = numerator - (whole * denominator);
    	
    	// reduce the fraction. 
    	if (portion != 0 ) divisor = gcd(portion, denominator) ; 
    
    	portion /= divisor ; 
    	denominator /= divisor ; 
    
    	printf("results: ") ; 
    	if ( whole > 0) printf("%d ", whole); 
    	if ( portion > 0) printf("%d/%d", portion, denominator ) ;
    	else if (whole == 0) printf("0") ; 
    	printf("\n") ; 
     
        printf("PROGRAM ENDS\n");
        return 0;
    }
    
    int gcd(a,b)  {
        int r ;  // Remainder
    	r= a % b ;
        if (r==0) return b;
        else return gcd(b,r);
    }
    Todd

    (EDIT: forum software - so much for my well indented code...)
    Last edited by Dino; 12-06-2007 at 03:35 PM.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    14
    Thanks for the help. I'm going to clean mine up and resubmit it.

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    The big question is: Do you understand the differences?

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    14
    Now I do. I think I, and probly 99% of the world, learn off examples.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Todd Burch View Post
    (EDIT: forum software - so much for my well indented code...)
    It's not wise to mix spaces and tabs. Use one, but not both. That will avoid the code messing up.
    And function prototypes are not supposed to be inside functions! They're supposed to be in headers, or at the very least, at the beginning of your .c or .cpp file in case you don't have one.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Your code doesn't have to be that long/complex.. Use some ternary and some division and modulo, and voila.. And yes, I like ternary.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
         int nr1 = 0, nr2 = 0, i = 0, gcd = 0;
         
         
         printf("Enter a fraction(x/y): ");
         scanf("&#37;d%*c%d", &nr1, &nr2);
    
         if(!(nr1&&nr2)) return -1;
         
         if(nr2 % nr1 == 0) { nr2 /= nr1; nr1 = 1; }
         
         else
              for(i = 2; i <= ((nr1<nr2) ? (nr1/2) : (nr2/2) ); i++)
                   gcd = ( !(nr1%i) && !(nr2%i) ) ? i : gcd;
    
         
         if(gcd != 0) { printf("GCD = %d\n", gcd);  nr1 /= gcd; nr2 /= gcd; }
        
         
         (nr1 > nr2) ? printf("%d ", nr1/nr2) : 0;
         
         (nr1%nr2) ? printf("%d/%d\n", nr1%nr2, nr2) : 0;
         
         return 0;
    }
    Last edited by IceDane; 12-06-2007 at 04:28 PM. Reason: Shuts down now if either numerator or denominator are zero.

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    IceDane you Sicko, that's horrible!

    Your code doesn't have to be obfuscated AND inefficient.
    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"

  13. #13
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by iMalc View Post
    IceDane you Sicko, that's horrible!

    Your code doesn't have to be obfuscated AND inefficient.
    Haha.. Well, it looks nice IMO. =(

    Anyway, is it the lack of nested if statements and extreme usage of modulus that makes it inefficient? Is modulus actually an inefficient operation?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem w/ doubles in friend's program
    By mkylman in forum C Programming
    Replies: 16
    Last Post: 11-22-2008, 10:45 AM
  2. I have finished my program, one problem
    By sloopy in forum C Programming
    Replies: 4
    Last Post: 11-29-2005, 02:10 AM
  3. Replies: 2
    Last Post: 04-25-2005, 11:59 AM
  4. simple frontend program problem
    By gandalf_bar in forum Linux Programming
    Replies: 16
    Last Post: 04-22-2004, 06:33 AM
  5. Problem with Program not Quitting
    By Unregistered in forum Windows Programming
    Replies: 20
    Last Post: 06-11-2002, 11:06 PM