reducing a fraction

This is a discussion on reducing a fraction within the C Programming forums, part of the General Programming Boards category; how do i write a function that wil reduce the fraction in the out put ... heres my source code. ...

  1. #1
    Unregistered
    Guest

    reducing a fraction

    how do i write a function that wil reduce the fraction in the out put ... heres my source code.


    #include <stdio.h>

    /*global declarations*/
    typedef struct
    {
    int numerator;
    int denominator;
    } FRACTION;

    /*prototype declarations*/
    FRACTION getFr (void);
    FRACTION multFr (FRACTION fr1, FRACTION fr2);

    FRACTION divFr (FRACTION fr1, FRACTION fr2);
    FRACTION addFr (FRACTION fr1, FRACTION fr2);


    void printFr (FRACTION fr1,
    FRACTION fr2,
    FRACTION result,
    FRACTION conclude,
    FRACTION sumup);

    int main (void)
    {
    FRACTION fr1;
    FRACTION fr2;
    FRACTION res;
    FRACTION con;
    FRACTION sum;


    /*statements*/
    fr1 = getFr ();
    fr2 = getFr ();
    res = multFr (fr1, fr2);
    con = divFr (fr1, fr2);
    sum = addFr (fr1, fr2);

    printFr (fr1, fr2, res,con,sum);



    return 0;
    }

    /*===========getFr==========*/
    FRACTION getFr (void)
    {
    /*local definitions*/
    FRACTION fr;

    /*statements*/
    printf("Write a fraction in the form of x/y: ");
    scanf ("%d/%d", &fr.numerator,&fr.denominator);
    return fr;
    } /*getFraction*/

    /*==========multFr==========*/
    FRACTION multFr (FRACTION fr1,
    FRACTION fr2)
    {
    /*local variables*/
    FRACTION res;


    /*statements*/
    res.numerator = fr1.numerator * fr2.numerator;
    res.denominator = fr1.denominator * fr2.denominator;
    return res;

    }/*multFr*/

    any help would be appreciated ..... thanks

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >how do i write a function that wil reduce the fraction in the out put
    Do you know how to reduce a fraction? Because the actual code is relatively simple provided you understand how to solve the problem. Start by finding the factors of each number in the fraction, start with 1 and move up saving each factor that is common to both numbers and greater than the factor previously saved. When you get to the point where both factors are different, the last one that was common to both is your greatest common factor.

    From here, reducing the fraction is completed with a simple division. Take each number in the fraction and divide them by the greatest common factor, the result is your reduced fraction.
    20/60
    20 divided by 20 = 1
    60 divided by 20 = 3
    Result: 1/3

    Just put that into code and you're done.

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

  3. #3
    Unregistered
    Guest
    o ok thats simple enough ill give it a try .... thanx

  4. #4
    Unregistered
    Guest
    i still cant derive the function .... can u help me get started thanx`

  5. #5
    Unregistered
    Guest
    If all your trying to do is manipulate the output so it wont display as many decimals just do:
    printf("%5d.%5d",result.numerator,result.denominat or);
    or whatever one your trying to print.
    Hope this helps

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with fraction division function
    By trippedwire in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2004, 11:38 AM
  2. A tragedy!
    By Aakash Datt in forum C++ Programming
    Replies: 4
    Last Post: 05-01-2003, 06:57 PM
  3. Overloading insertion and extraction problem
    By Curwa in forum C++ Programming
    Replies: 1
    Last Post: 01-15-2003, 08:20 PM
  4. Reducing Fractions/Entering fraction form
    By csmatheng in forum C Programming
    Replies: 4
    Last Post: 09-08-2002, 02:43 PM
  5. Help with Fraction class
    By cheeisme123 in forum C++ Programming
    Replies: 0
    Last Post: 06-04-2002, 07:48 AM

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