-
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
-
>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
-
o ok thats simple enough ill give it a try .... thanx
-
i still cant derive the function .... can u help me get started thanx`
-
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