I need to write a program that simplifies an array of fractions, converts them into decimal form and then adds them up.
So for 4/8 and 9/15, the output would be 1/2 3/5, 0.5 0.6 and 1.1
Sorry for not showing any work in progress, I'm at loss how to approach it and wondering if you could help.
Have you used a struct or a class before? I assume your assignment wants you to make a fraction class?
Have a simple struct like:
1 2 3 4
struct Fraction {
int numerator;
int denominator;
}
Then, you can have an array of Fractions... Fraction fractions[100];
To simplify a fraction, you need to find the gcd of the numerator and the denominator, and then divide them by that gcd. (Equivalently, but a little less efficiency, you can just loop through all the numbers between the larger number and 2 and see which are divisible using the % operator.)