C++ friends, ASSEMBLE!

Hi, pretty new here and need a little help. I am a little stuck on this problem right now, but still trying to figure this out myself.

This is the book's problem for reference:
Write a collection of functions to do arithmetic with rational numbers (common fractions). Define functions getRational, displayRational, addRational, and subtractRational whose prototypes are:

1
2
3
4
void getRational (int& numer, int& denom );
void displayRational ( int numer, int denom );
void addRational ( int& ansNum, int& ansDenom, int num1, int denom1, int num2, int denom2);
void subtractRational (int& ansNum, int& ansDenom, int num1, int denom1, int num2, int denom2);


Design main as a driver function that inputs pairs of rationals and displays for each pair the sum and difference. To produce results that are in reduced form, write and call a greatest common divisor (gcd) function and divide the numerator and denominator of each fraction by their gcd.


My first question is am I even doing it right. Is it telling me to add and subtract the fractions, or the way I am doing it by adding the numerators and denominators. I am starting to think it is the way I'm not doing it. And my second question is how do you write a Greatest Common Divisor function. My current code is below.

Thanks


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
using namespace std;

void getRational (int& numer, int& denom );
void displayRational ( int numer, int denom );
void addRational ( int& ansNum, int& ansDenom, int num1, int denom1, int num2, int denom2);
void subtractRational (int& ansNum, int& ansDenom, int num1, int denom1, int num2, int denom2);

int main()
{
int numer;
int denom;
int num1;
int denom1;
int num2;
int denom2;
int ansNum;
int ansDenom;

        cout << "Input the first pair of rational numbers (numerator and denominator):" << endl;
        cout << "First number entered is: ";
        cin >> num1;
        cout << "Second number entered is: ";
        cin >> denom1;


        getRational (num1, denom1);
        displayRational (num1, denom1);

        cout << "Input the second pair of rational numbers (numerator and denominator):" << endl;
        cout << "First number entered is: ";
        cin >> num2;
        cout << "Second number entered is: ";
        cin >> denom2;

        getRational (num2, denom2);
        displayRational (num2, denom2);
        addRational (ansNum, ansDenom, num1, denom1, num2, denom2);
        subtractRational (ansNum, ansDenom, num1, denom1, num2, denom2);



return 0;
}
void getRational (int& numer, int& denom)
        {
                double rational;
                rational = ( numer / denom );
                cout << "Result of numerator divided by the denominator is : " << rational << endl;
        }

void displayRational (int numer, int denom)
        {
                double rational;
                rational = ( numer / denom );
                cout << "The rational numerator and denominator entered is : " << numer << "/" << denom << "  =  " << rational << endl;

        }

void addRational (int& ansNum, int& ansDenom, int num1, int denom1, int num2, int denom2)
        {
                ansNum = ( num1 + num2 ); // should I change it to ((num1/denom1)+(num2/denom2)) and delete the ansDenom, but I need it for the parameter. And same goes for the subtraction part.
                ansDenom = ( denom1 + denom2 );
                cout << "The addition of the numerators entered is : " << ansNum << endl;
                cout << "The addition of the denominators entered is : " << ansDenom << endl;
        }

void subtractRational (int& ansNum, int& ansDenom, int num1, int denom1, int num2, int denom2)
        {
                ansNum = ( num1 - num2);
                ansDenom = ( denom1 - denom2);
                cout << "The subtraction of the numerators entered is: " << ansNum << endl;
                cout << "The subtraction of the denominators entered is: " << ansDenom << endl;
        }

Last edited on
I need to turn in this program today, so can someone help me please.
Ok, so I understand that I need to use fractions in it, but I am a little confused on how to implement a GCD or greatest common divisor code into my program and use both ansNum and ansDenom in the void functions.
The question is asking you to add and subtract the two fractions. The gcd is required to ensure that each fraction sum is properly reduced. e.g.
4/72 + 7/28 = 1/18+1/4=22/72=11/36.//the gcd of 22 and 72 is 2.
4/72- 7/28 = 1/18-1/4 = -14/72=-7/36.//the gcd of 14 and 72 is 2.
I am really confused on what I am doing wrong. My program is due in a couple of hours and I would like some help.

Im confused on how to change my GCD function to have it work for the sum and difference parts. And just the whole thing. And try not to give me too vague of answers please :)

here is the code ive been working on:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
using namespace std;

void getRational (int& numer, int& denom );
void displayRational ( int numer, int denom );
void addRational ( int& ansNum, int& ansDenom, int num1, int denom1, int num2, int denom2);
void subtractRational (int& ansNum, int& ansDenom, int num1, int denom1, int num2, int denom2);

int GCD (int ansNum, int ansDenom);

int main()
{
int numer;
int denom;
int num1;
int denom1;
int num2;
int denom2;
int ansNum;
int ansDenom;

        cout << "Input the first pair of rational numbers (numerator and denominator):" << endl;
        cout << "First number entered is: ";
        cin >> num1;
        cout << "Second number entered is: ";
        cin >> denom1;

        displayRational (num1, denom1);

        cout << "Input the second pair of rational numbers (numerator and denominator):" << endl;
        cout << "First number entered is: ";
        cin >> num2;
        cout << "Second number entered is: ";
        cin >> denom2;

        getRational (num2, denom2);
        displayRational (num2, denom2);
        addRational (ansNum, ansDenom, num1, denom1, num2, denom2);
        subtractRational (ansNum, ansDenom, num1, denom1, num2, denom2);
        GCD (ansNum, ansDenom);
return 0;
}

void getRational (int& numer, int& denom)
        {
                double rational;
                rational = ( numer / denom );
                cout << "Result of numerator divided by the denominator is : " << rational << endl;
        }

void displayRational (int numer, int denom)
        {
                double rational;
                rational = ( numer / denom );
                cout << "The rational numerator and denominator entered is : " << numer << "/" << denom << endl;

        }
int GCD (int ansNum, int ansDenom)
        {
                int t;
                while (ansDenom!=0)
        {
                t=ansDenom;
                ansDenom=ansNum%ansDenom;
                ansNum=t;
        }
        return ansNum;
}


void addRational (int& ansNum, int& ansDenom, int num1, int denom1, int num2, int denom2)
        {
                ansNum = (( num1/denom1) +( num2/denom2 ));
                ansDenom = ( denom1 + denom2 );
                cout << "The addition of the numerators entered is : " << ansNum << endl;
                cout << "The addition of the denominators entered is : " << ansDenom << endl;
        }

void subtractRational (int& ansNum, int& ansDenom, int num1, int denom1, int num2, int denom2)
        {
                ansNum = ( num1 - num2);
                ansDenom = ( denom1 - denom2);
  cout << "The subtraction of the numerators entered is: " << ansNum << endl;
                cout << "The subtraction of the denominators entered is: " << ansDenom << endl;
        }
Your add and subtract methods are wrong.You need a common denominator to add or subtract the fractions, just multiply each by the others denominator ie: 1/4 +1/2 = (1/4)*2 +(1/2)*4
then reduce the result.
Topic archived. No new replies allowed.