Can anyone help me I'm very new to programming and need some help. I need to find a way to +,-,*, and / fractions.
This is a discussion on anyone help a newbie with fractions within the A Brief History of Cprogramming.com forums, part of the Community Boards category; Can anyone help me I'm very new to programming and need some help. I need to find a way to ...
Can anyone help me I'm very new to programming and need some help. I need to find a way to +,-,*, and / fractions.
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):To subtract, just change the + to - in code above.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.To divide, just multiply by inverse.Code:int n1, n2, n3, d1, d2, d3; n3 = n1 * n2; d3 = d1 * d2; //common denomenator, not reducedOne thing that's left is to reduce... to do this, we can get the greatest common factor first, then divide top/bottom of the fraction by this value. You can find how to do this here: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.
This is my signature. Remind me to change it.
Gee, dbaryl, dont cofuse him.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
--
10Hopefully, I'm not tellin ya anything incorrect...just give that example a go.Code:// 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;
Last edited by face_master; 07-25-2002 at 06:17 AM.
Man, you have confused ME now ...
what in the world is that? sorry, I don't get that.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.
This is my signature. Remind me to change it.
its adding 1 to the fraction...um, theres no data structure that holds a fraction in any way other than a decimal, is there?
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~
Never end on learning~
>>Heres some code that adds one to a fraction (just fixed up that sentence).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
This is my signature. Remind me to change it.