Can anyone help me I'm very new to programming and need some help. I need to find a way to +,-,*, and / fractions.

Printable View

- 07-24-2002Unregisteredanyone help a newbie with fractions
Can anyone help me I'm very new to programming and need some help. I need to find a way to +,-,*, and / fractions.

- 07-25-2002dbaryl
Well, first off: you need the algebra of it.

Whether you're trying to add or subtract, it's necessary to know the common denomenator. To do this, you can simply multiply top and bottom of each fraction by the denomenator of the other fraction. This will not be reduced, but we'll get to reducing later.

So to +/-

Say you have 2 fractions, numerator 1: n1, denomenator 1: d1, numerator 2: n2... and n3 & d3 for the final fraction. Assuming you have the values already stored (using a function like scanf):Code:`int n1, n2, n3, d1, d2, d3;`

n3 = n1 * d2 + n2 * d1;

d3 = d1 * d2; //common denomenator, not reduced

To multiply or divide:

To multiply, just multiply the numerators for new numerator, den. for new denomenator.Code:`int n1, n2, n3, d1, d2, d3;`

n3 = n1 * n2;

d3 = d1 * d2; //common denomenator, not reduced

Code:`int n1, n2, n3, d1, d2, d3;`

n3 = n1 * d2;

d3 = d1 * n2; //denomenator not reduced

http://www.cprogramming.com/cboard/s...threadid=22039

If you have any other questions, just ask. - 07-25-2002face_masterQuote:

*Originally posted by Unregistered*

**Can anyone help me I'm very new to programming and need some help. I need to find a way to +,-,*, and / fractions.**

Heres some code that adds one to a fraction (just fixed up that sentence). This fraction that we'll work with is

7

--

10Code:`// our fraction`

double fraction = 7 / 10; // remember, our fraction is equal to 7 divided by ten

double total = 0; // our end result

total = fraction + 1;

- 07-25-2002dbaryl
Man, you have confused ME now :D ...

Code:`total = fraction + 1;`

Also, I was thinking they want it as a fraction, not a decimal, as that's what making it a__double__will do. - 07-25-2002face_master
its adding 1 to the fraction...um, theres no data structure that holds a fraction in any way other than a decimal, is there?

- 07-25-2002black
Algebra is basis, and next you should learn the difference between programming language and mathematics.

for instance, this line below is common in almost all kinds of programming languages but not in math.

PHP Code:`a=a+1;`

Good luck~ :) - 07-25-2002dbarylQuote:

*Originally posted by face_master*

**its adding 1 to the fraction...um, theres no data structure that holds a fraction in any way other than a decimal, is there?**

OK, I guess I need to pead more throughly next time :D