Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Fraction Help
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
tivanenk




PostPosted: Thu Oct 14, 2010 11:32 am   Post subject: Fraction Help

I need to write a program, but ran into a problem. My teacher didn't teach us how to substract/divide/multiply/add fractions. Can anybody help me? I need some help in this.
Sponsor
Sponsor
Sponsor
sponsor
DtY




PostPosted: Thu Oct 14, 2010 9:16 pm   Post subject: RE:Fraction Help

You mean from a math perspective? This is... grade five math?

Multiplying fractions a/b * c/d = (a*c)/(b*d)
Dividing is multiplying by the inverse of the second: (a/b)/(c/d) / (a*d)/(b*c)

Adding is done by adding the numerators once a common denominator is found. Subtraction is the same as adding the negative of the second.
Coldkick




PostPosted: Thu Oct 14, 2010 9:23 pm   Post subject: RE:Fraction Help

Maybe he means he wants to calculate but is getting an error because it is the wrong type.
Two ways to fix this.
Change the variable type to real OR put round () around the equations. Some times integers are required, thus you must use the latter (for graphics etc).
tivanenk




PostPosted: Fri Oct 15, 2010 12:11 pm   Post subject: RE:Fraction Help

actually, the help I need is the teacher told me that Turing will HAVE to output a fraction as an answer, but mine outputs the decimal
here is the formula I use:

var ra : real
var rb : real
var rc : real
var rd : real
var re : real
get ra
get rb
get rc
get rd
re := ((ra*rc)/(rb*rd))
put re
DtY




PostPosted: Fri Oct 15, 2010 2:54 pm   Post subject: RE:Fraction Help

re := ((ra*rc)/(rb*rd))

/ is division, when I used it in my post, I was meaning a literal slash (or fraction bar).
tivanenk




PostPosted: Fri Oct 15, 2010 3:19 pm   Post subject: RE:Fraction Help

I understand, but I still don't understand how to add fractions so the answer will BE a fraction
TerranceN




PostPosted: Fri Oct 15, 2010 3:50 pm   Post subject: RE:Fraction Help

I think DtY means something like this:
Turing:

var a : int := 3
var b : int := 5

put (intstr(a) + " / " + intstr(b) + " = " + realstr(a/b, 2))


Since Turing will automatically evaluate any fractions you type, you have to manually make it look like a fraction by outputting a "/".
tivanenk




PostPosted: Fri Oct 15, 2010 5:25 pm   Post subject: RE:Fraction Help

that's better, by I need to make it so you can input ANY fraction, it is like an equation.
Example:
a/b + c/d = e/f
You can replace a, b, c, d with anything
Sponsor
Sponsor
Sponsor
sponsor
DtY




PostPosted: Fri Oct 15, 2010 8:35 pm   Post subject: RE:Fraction Help

You have to store the numerator and denominator separately, then when you want to output it you would output numerator "/" denominator (where the slash is a literal slash (or fraction bar) and not the division operator).

You would have separate formulae for the numerator and denominator. The multiplication and division are easy enough, I listed them up there. If you're not sure about addition or subtraction, find a piece of paper and try it using variables, that will give you a generic formula for rational addition.

After you do that you will probably want to reduce the fraction (if you dread this, ask your teacher if it's necessary, it might not be if you're in a lower grade CS class), if you do the most efficient way of finding the greatest common divisor is the equation gcd(a,b) = gcd(b, a mod b). More on that on Wikipedia.
tivanenk




PostPosted: Sat Oct 16, 2010 11:16 am   Post subject: RE:Fraction Help

ok, thanks!
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: