simplifiing fractions

This is a discussion on simplifiing fractions within the C++ Programming forums, part of the General Programming Boards category; Im just starting in C++, and im using borland 3.1. im tryin to create a program that simplifies fractions. i ...

  1. #1
    Unregistered
    Guest

    Unhappy simplifiing fractions

    Im just starting in C++, and im using borland 3.1. im tryin to create a program that simplifies fractions. i know im goin to have to use various loops, but im not sure how to go about the problem. do i turn it into a decimal then turn that to the fraction in simpilist form? anyones help would be greatly appreciated.

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Assume N/M is your fraction, then you should search for the largest divisor of N and M. And N and M should be divided by that value, so you get P/Q where P=N/divisor and Q=M/divisor.

    Example: 4/6, the largest divisor is 2. So your simplified fraction is 2/3.

    A strategy to attack this problem is finding all divisors of N and all divisors of M. Then find the greatest divisor which is divisor of both N and M.

    There are more sophisticated methods to do this, for example using recursion.

  3. #3
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Find the greatest common denominator by
    using elucid's algorithm.

    Code:
    int gcd(int a, int b)
    {
        if (b == 0)
            return a;
    
        return gcd(b, a % b);
    }
    Then to simplify write a function which
    makes a fraction from a numerator and denomerator.
    Find the gcd of the numerator and denomerator and divide both by it.

    struct Frac make_frac(int n, int d)
    {
    struct Frac f;
    int g = gcd(n, d);

    f.numer = n / g;
    f.denom = d / g;
    return f;
    }

  4. #4
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Find the greatest common denominator by
    using elucid's algorithm.

    Code:
    int gcd(int a, int b)
    {
        if (b == 0)
            return a;
    
        return gcd(b, a % b);
    }
    Then to simplify write a function which
    makes a fraction from a numerator and denomerator.
    Find the gcd of the numerator and denomerator and divide both by it.

    Code:
    Frac make_frac(int n, int d)
    {
         Frac f;
         int g = gcd(n, d);
         
         f.numer = n / g;
         f.denom = d / g;
         return f;    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fractions
    By zdream8 in forum C Programming
    Replies: 2
    Last Post: 05-21-2008, 09:54 PM
  2. Greatest Common Factors of Fractions
    By sonict in forum C++ Programming
    Replies: 1
    Last Post: 01-15-2003, 03:33 AM
  3. Fractions
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2002, 06:51 AM
  4. simplifiing fractions part 2
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2002, 05:29 PM
  5. Decimals to Fractions
    By ToasterPas in forum C++ Programming
    Replies: 4
    Last Post: 12-28-2001, 11:58 AM

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