# Need some help with functions to do arithmetic

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

4/5 + 2/6 = 34/30 = 17/15
4/5 - 2/6 = 14/30 = 7/15
4/5 * 2/6 = 8/30 = 4/15
4/5 / 2/6 = 24/10 = 12/5

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
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
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
Author 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
Commented:
Ok then, i'll give you the eisiest way to do this, With normal functions.
0
Commented:
It is easy enough to do it.

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:

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(); }

.....
static Rational add(const Rational & a, const Rational & b);

};

inline Rational operator + (const Rational & a, const Rational & b)
{ return Rational::add(a,b); }

Rational Rational::add(const Rational & a, const Rational & b)
{
return Rational(a.numerator * b.denominator +
a.denominator * b.numerator,
a.denominator * b.denominator);
}

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
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);

int main()
{
int numer,denom,numer2,denom2,a,b;

cout<<"Fraction 1:"<<endl<<"Numerator?";
cin>>numer;
cout<<"Denominator?";
cin>>denom;

cout<<"Fraction 2:"<<endl<<"Numerator?";
cin>>a;
cout<<"Denominator?";
cin>>b;
cout<<endl<<endl;

numer2=a;
denom2=b;

numer2=a;
denom2=b;
subfrac(numer,denom,numer2,denom2);
cout<<"Subtracting: "<<numer2<<"/"<<denom2<<endl;

numer2=a;
denom2=b;
mulfrac(numer,denom,numer2,denom2);
cout<<"Multiplying: "<<numer2<<"/"<<denom2<<endl;

numer2=a;
denom2=b;
divfrac(numer,denom,numer2,denom2);
cout<<"Dividing: "<<numer2<<"/"<<denom2<<endl;

}

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;

if(a>b)
{
int c=b;
b=a;
a=c;
}
int i,d=0;
float nylist[200];
for(i=1;i<=a;++i)
{
if((double(a)/i == a/i) && (double(b)/i == b/i))
{
nylist[d]=i;
d++;
}
}

int mx=nylist[0];
int mxpos=0;

for(int j=0;j<=d-1;j++)
{
if (nylist[j]>mx)
{
mx=nylist[j];
mxpos=j;
}
}

return nylist[mxpos];
}
//end program

and thats how you do it.
commented out lines have explainations
if you need more just ask.
hope this helps.
0
Commented:
e12voltsdac

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...

jeesh!
0
Commented:
I'd say this is another case of wild "hunting for points". But what do you think you'll do with these?
0
Commented:
JTatWVU, I advise you to use Salte's post only, and when everything will be working fine, accept HIS comment as an answer!
0
Commented:
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.

Alf
0
Commented:
No comment has been added lately, so it's time to clean up this TA. I will
leave a recommendation in the Cleanup topic area that this question is:

Please leave any comments here within the next seven days.

Experts: Silence means you don't care. Grading recommendations are made in light
of the posted grading guidlines (http://www.experts-exchange.com/help.jsp#hi73).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

EE Cleanup Volunteer
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.