Link to home
Start Free TrialLog in
Avatar of JTatWVU
JTatWVU

asked on

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
Avatar of e12voltsdac
e12voltsdac
Flag of United States of America image

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.
Avatar of JTatWVU
JTatWVU

ASKER

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.
Ok then, i'll give you the eisiest way to do this, With normal functions.
ASKER CERTIFIED SOLUTION
Avatar of Salte
Salte

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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;
addfrac(numer,denom,numer2,denom2);
cout<<"Adding: "<<numer2<<"/"<<denom2<<endl;

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

Answered: Points to Salte: Grade A

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 (https://www.experts-exchange.com/help.jsp#hi73).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer