I'm just in the planning stages of this program, so I just need to know how to do this. I don't know to know any actual code for it...
This is my assignment: Write four functions, one for each of the operations on fractions: addition, subtraction, multiplication, and division. If the denominator of a result will be zero, the main program should display a message, and NOT call the functions.
Each function has six parameters. The first four: numerator of fraction one, denominator of fraction one, numerator of fraction two, and denominator of fraction two are passed by value.
This is an example of how my program should work:
An example for the function to add two fractions is:
Do you wish to do arithmetic on two fractions? (Y or N): Y
Enter the numerator and denominator of the first fraction: 4 5
Enter the numerator and denominator of the second fraction: 2 6
Right now I just need help in determing what steps I have to take in writing the code to perform the actual arithmetic operations. Could someone please give me a walkthrough of what I'll have to do? I need it for my project but I also need it because I have no idea how to do it either. I especially don't know how to deal with the fractions. Thanks -JT
Ok i kind of see what you wanna do but can you clarify whether or not you want to make a new data type that holds a fraction or you just want to make normal functions that will return the numerator and denominator seperately?
I can help you with either one.
0
JTatWVUAuthor Commented:
I just want to be able to get the program to look like the sample one. I'll take the easiest way to do that. If I just use normal functions my numerator and denominator will have to be returned seperately? I'm not sure I understand what you're asking.
0
e12voltsdacCommented:
Ok then, i'll give you the eisiest way to do this, With normal functions.
0
Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.
I understand you don't want to make a Rational class (the obvious solution) but rather have functions with six arguments so that you have nominator and denomincator of each of the operands and the result, well, the equations are easy enough. Just be aware that you probably need to compute the greatest common divisor:
gcd(a,b)
This is necessary since if you get a fraction a/b as result then you need to compute gcd(a,b) and if the result is a value d > 1 then a = dc and b = de and a/b = c/e where c and e have no common divisors and so probably c/e is the preferred result.
so, step 1:
int gcd(int a, int b);
Once you have that function, the rest is a piece of cake:
add:
a/b + c/d == (a * d + b * c) / (b * d), remember to divide both denominator and numerator by gcd(a*d+b*c,b*d);
sub: same as add really:
a/b - c/d = (a*d - b*c)/(b*d). Again remember to divide by gcd(a*d-b*c,b*d);
multiply: even easier than add/subtract:
a/b * c/d = (a*c)/(b*d), again remember to divide by gcd(a*c,b*d).
divide, same as multiply:
a/b / c/d = a/b * d/c = (a*d)/(b*c)
for all results, remember to first watch out for denominators equal to 0 and next divide both numerators and denominators by gcd(num,denom)
There you go.
As an example I can show the add function:
bool add(int an, int ad, int bn, int bd, int & cn, int & cd)
{
if (ad == 0 || bd == 0)
return false; // error.
int n = an * bd + ad * bn;
int d = ad * bd;
int q = gcd(n,d);
cn = n / q;
cd = d / q;
return true;
}
Make sure you understand what is going on there and then you can write the other functions similarly.
It is even nicer if you make a rational class:
class Rational {
private:
int numerator;
int denominator;
void normalize();
public:
Rational(int n = 0, int d = 1) : numerator(n), denominator(d) { normalize(); }
void Rational::normalize()
{
bool sign = (denominator < 0);
if (sign)
denominator = -denominator;
if (numerator < 0) {
sign = !sign;
numerator = -numerator;
}
int d = gcd(numerator,denominator);
numerator /= d;
denominator /= d;
if (sign)
numerator = -numerator;
}
This version also allowed gcd() to only get positive arguments. If the gcd function can accept negative arguments you don't have to force the values positive before calling gcd:
void Rational::normalize()
{
if (denominator < 0) {
denominator = -denominator;
numerator = -numerator;
}
int d = gcd(numerator,denominator);
numerator /= d;
denominator /= d;
}
This function require gcd() to handle negative numerators.
As you perhaps have guessed, the gcd() function is kinda important here, It isn't that hard to write but I leave that as an exercize for you to figure out. If you still have problems I can give you hints :-)
Alf
0
e12voltsdacCommented:
OK I'm finally done.
You don't need a whole class it can be done eisier:
//start program
#include<iostream.h>
void addfrac(int &a, int &b, int &c, int &d);
void subfrac(int &a, int &b, int &c, int &d);
void mulfrac(int &a, int &b, int &c, int &d);
void divfrac(int &a, int &b, int &c, int &d);
void reduce(int &a, int &b);
int gcd(int a, int b);
void addfrac(int &a, int &b, int &c, int &d)//add a/b + c/d store answer to c/d
{
c = d*a+b*c;
d = b*d;
reduce(c,d);
}
void subfrac(int &a, int &b, int &c, int &d)//subtract a/b - c/d store answer to c/d
{
c = d*a-b*c;
d = b*d;
reduce(c,d);
}
void mulfrac(int &a, int &b, int &c, int &d)//multiply a/b * c/d store answer to c/d
{
c = a*c;
d = b*d;
reduce(c,d);
}
void divfrac(int &a, int &b, int &c, int &d)//divide (a/b) / (c/d) which is equivalent to a/b * d/c store answer to c/d
{
int g=c;
c = a*d;
d = b*g;
reduce(c,d);
}
void reduce(int &a, int &b)//reduce a fraction int a=numerator , b=denominator passed by reference
{
int e=gcd(a,b);
a = a/e;
b = b/e;
}
int gcd(int a, int b)//find the gcd of two int
{
if(a<0)
a=-a;
if(b<0)
b=-b;
This is HOMEWORK - he even says so! He even says he does NOT want you to post source code.
It is against the rules of this board to give him source code. Alf even went too far - general algorithms are one thing, but doing everything but fractional reduction...
c'mon guys, it is bad enough when people try to "hide" their homework, this one actually told us, yet we still did the whole problem for him...
It is possible I went too far, I showed the code for one of the operations and showed the equations for the others.
However, those equations should be in the "public domain" they are very elementary mathematics so I don't think I really spoiled any big secrets in stating them.
and the crucial thing is the gcd function which I purposely didn't show.
Again, it is possible I went too far and if so I apologize, I didn't mean to but I didn't intend to go "wild point hunting" or whatever you called, I was just trying to be nice.
I can help you with either one.