Thread: Simplifying fractions

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    12

    Simplifying fractions

    I have compiled a database for rational numbers. Integers are used to represent the numerator and denominator of rational numbers. Subtractions, additions, multiplications, and divisions can be performed. Can anyone please help me include simplication code for the output answers? In addition, the answer for 1/2-1/2 is 0/4, which is quite weird.

    Any help would be greatly appreciated.

    My code:

    Code:
    #include<stdio.h> 
    
    int gcd(int a, int b)
    {
    if (a == 0)
    return b;
    return gcd(b%a, a);
    }
    int addFraction(int n1, int d1, int n2,int d2,int d3,int n3)//Add
    {
    d3 = gcd(d1,d2);
    d3 = (d1*d2) / d3;
    n3 = (n1)*(d3/d1) + (n2)*(d3/d2);
    printf("\n %d/%d + %d/%d \t = \t %d/%d\n", n1, d1,n2, d2, n3, d3);
        return 0;
    }
    int subFraction(int n1, int d1, int n2,int d2,int d3,int n3)//Subtract
    {
    d3 = d1*d2;
    n3 = (n1*d2 - n2*d1);
    printf("\n %d/%d - %d/%d \t = \t %d/%d\n", n1, d1,n2, d2, n3, d3);
        return 0;
    }
    int mulFraction(int n1, int d1, int n2,int d2,int d3,int n3)//Multiply
    {
    n3 = n1*n2;
    d3=d1*d2;
    printf("\n %d/%d * %d/%d \t = \t %d/%d\n", n1, d1,n2, d2, n3, d3);
        return 0;
    }
    int divFraction(int n1, int d1, int n2,int d2,int d3,int n3)//division
    {
    n3 = n1*d2;
    d3=d1*n2;
    printf("\n %d/%d / %d/%d \t = \t %d/%d\n", n1, d1,n2, d2, n3, d3);
        return 0;
    }
    int main()
    {
        int n1, d1, n2, d2, d3 = 0, n3 = 0;
    printf("\n Enter the values of numerator 1 and denominator 1: ");
    scanf("%d %d",&n1,&d1);
    printf("\n Enter the values of numerator 2 and denominator 2: ");
    scanf("%d %d",&n2,&d2);
    addFraction(n1, d1, n2, d2,d3,n3); //calling the function to perform addition of rational values
    subFraction(n1, d1, n2, d2,d3,n3); //calling the function to perform subtraction of rational values
    mulFraction(n1, d1, n2, d2,d3,n3); //calling the function to perform multiplication of rational values
    divFraction(n1, d1, n2, d2,d3,n3); //calling the function to perform division of rational values
    return 0;
    }
    Some examples of my output:
    Simplifying fractions-screenshot-2020-11-16-3-51-18-pm-png


    Simplifying fractions-screenshot-2020-11-16-3-51-35-pm-png

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,304
    You already have a function for computing GCD, so given a numerator and denominator, you compute the GCD and then divide both by the GCD, simplifying the fraction.

    Quote Originally Posted by Eve Wein
    In addition, the answer for 1/2-1/2 is 0/4, which is quite weird.
    In addFraction, you're already doing some simplification for the resulting denominator. You just didn't do the same for subFraction.

    A few other things to note:
    • Indent your code more consistently.
    • Putting a blank line or two in between function definitions can help make it easier to read your code. You can also use a single blank line sparingly to separate parts of your code within a function.
    • Since you're not using them as what we call output parameters (or out parameters), your n3 and d3 parameters really should be local variables instead.
    • Do you plan to make use of the return value of these functions? If not, declare their return type as void instead of pointlessly returning an unused 0.
    • You don't need comments that basically say what the function name already tells the reader. You could use comments to do things like say, document that the denominator cannot be 0.
    Last edited by laserlight; 11-16-2020 at 03:40 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help simplifying for loop
    By tmdm in forum C Programming
    Replies: 3
    Last Post: 03-15-2011, 12:58 PM
  2. Simplifying Fractions
    By kemiga in forum C Programming
    Replies: 7
    Last Post: 10-20-2010, 07:49 AM
  3. Need help simplifying
    By yigster in forum C Programming
    Replies: 8
    Last Post: 04-01-2009, 07:28 AM
  4. Simplifying code
    By Taka in forum C Programming
    Replies: 1
    Last Post: 10-22-2006, 02:12 AM
  5. Simplifying fractions in c++
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 09-01-2002, 06:58 PM

Tags for this Thread