Thread: Simplfiying proper fractions

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    19

    Simplfiying proper fractions

    So i wrote code to simplify a proper fraction in the form a/b provided by the user.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int newnum, num, den, t, a, b;
    int main()
    {
        printf(">> ");
        scanf("%d/%d", &num, &den);
        a=num;
        b=den;
        while(num!=den)
    			{
    				if(den>num)
    					{
    						t=den%newnum;
    						den=t;
    						if(den==1)
    						{
    							break;
    						}
    					}
    				if(den<num)
    					{
    						t=num%den;
    						num=t;
    						if(num==1)
    						{
    							break;
    						}
    					}
    			}
    			den=b/t;
    			num=a/t;
    			printf("%d/%d", num, den);
    			system ("PAUSE");
    			return 0;
    }
    When i compiled the code, it went through without any errors. But when I ran the .exe file and typed in 2/4, i didnt get 1/2. An error just popped up saying that the program needs to close. I compiled it and ran it on another computer to make sure but the same thing happened.

    BTW, the algorithm used within the code to get the gcf (which is t in my code) is Euclids Algorithm.

    Could there be something wrong with the code?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Well run it in a debugger and find out.
    Code:
    $ gcc -Wall -Wextra -g foo.c
    $ ./a.out 
    >> 2/4
    Floating point exception
    $ gdb ./a.out 
    GNU gdb (GDB) 7.1-ubuntu
    Copyright (C) 2010 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
    and "show warranty" for details.
    This GDB was configured as "x86_64-linux-gnu".
    For bug reporting instructions, please see:
    <http://www.gnu.org/software/gdb/bugs/>...
    Reading symbols from /home/sc/Documents/coding/a.out...done.
    (gdb) run
    Starting program: /home/sc/Documents/coding/a.out 
    >> 2/4
    
    Program received signal SIGFPE, Arithmetic exception.
    0x0000000000400608 in main () at foo.c:16
    16	                        t=den%newnum;
    (gdb) print newnum
    $1 = 0
    zero you say - mmmm, that can't be good.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    19
    that's the thing. i can't figure out what's wrong with the code

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int newnum, num, den, t, a, b;
    int main()
    {
        printf(">> ");
        scanf("%d/%d", &num, &den);
        a=num;
        b=den;
        while(num!=den)
                {
                    if(den>num)
                        {
                            t=den%newnum;
                            den=t;
                            if(den==1)
                            {
                                break;
                            }
                        }
                    if(den<num)
                        {
                            t=num%den;
                            num=t;
                            if(num==1)
                            {
                                break;
                            }
                        }
                }
                den=b/t;
                num=a/t;
                printf("%d/%d", num, den);
                return 0;
    }
    What value does newnum have to begin with?
    What value you you assign to it?
    What value will it have when you attempt to use it in a division?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You may wish to look the Euclidean Algorithm, for computation of the greatest common divisor (gcd) of two integers.

    A fraction may be simplified by dividing both denominator and numerator by their greatest common divisor. This works whether the fraction is proper or not.
    Last edited by grumpy; 01-21-2012 at 07:01 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    19
    I made a mistake in the variables. Newnum wasn't supposed to be a variable. My bad. Here is the updated code which still doesn't work. And yes I used the Euclidean Algorithm for this code. Doesn't work though :|

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int num, den, t, a, b;
    int main()
    {
        printf(">> ");
        scanf("%d/%d", &num, &den);
        a=num;
        b=den;
        while(num!=den)
    			{
    				if(den>num)
    					{
    						t=den%num;
    						den=t;
    						t=den;
    						if(den==1)
    						{
    							break;
    						}
    					}
    				if(den<num)
    					{
    						t=num%den;
    						num=t;
    						t=num;
    						if(num==1)
    						{
    							break;
    						}
    					}
    			}
    			num=a/t;
                den=b/t;
    			printf("%d/%d", num, den);
    			system ("PAUSE");
    			return 0;
    }

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    There's no point in assigning den to t straight after you've assigned t to den. Same goes for t=num; you can remove both of those statements.

    Next, think about what the result of den%num is going to be when den is 6 and num is 2. 6 divided by 2 equals 3 with 0 remainder. You then proceed to check for a result equal to 1, but it's not going to be equal to one, it's going to be equal to zero when you reach the end of the iterations. It's not division by one that you need to protect against.

    When written properly this algorithm doesn't care which variable is larger and only needs one possible condition to become true to end the iterations. You should delete the whole loop and start over. It can be calculated in about a quarter of the number of lines of code. You only need three lines of code inside the loop body.
    See Euclidean algorithm - Wikipedia, the free encyclopedia.
    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"

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by haroldco View Post
    And yes I used the Euclidean Algorithm for this code. Doesn't work though :|
    Really? I would barely have picked it. You've made the code much more complicated than it needs to be.

    Anyway, if you follow the link given by iMalc, you will find advice on how to properly implement it.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fractions are just = to 0
    By Macca in forum C Programming
    Replies: 1
    Last Post: 05-10-2011, 05:41 AM
  2. fractions
    By zdream8 in forum C Programming
    Replies: 2
    Last Post: 05-21-2008, 09:54 PM
  3. fractions
    By joeshmoe1337 in forum C Programming
    Replies: 21
    Last Post: 09-11-2004, 01:34 AM
  4. Fractions
    By Aakash Datt in forum C++ Programming
    Replies: 8
    Last Post: 04-30-2003, 07:36 PM
  5. Fractions
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2002, 07:51 AM