Urgent: Program due!

Hi, new here and need a little help. I am a little stuck on this problem right now, but still trying to figure 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. And my second question is how do you write a GCD function. My current code is at the bottom.

Thanks
Last edited on
Here is my uncompleted/lots of holes code so far:

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
#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 numer1;
int denom1;
int numer2;
int denom2;

        cout << "Input the first rational numerator and then the first rational denominator: => ";
        cin >> numer1;
        cin >> denom1;



        cout << "Input the second rational numerator and then the second rational denominator: => ";
        cin >> numer2;
        cin >> denom2;




return 0;
}

void getRational (int& numer, int& denom)
        {
                int rational;
                rational = (numer / denom);
        }

void displayRational (int numer, int denom)
        {

        }

void addRational (int& ansNum, int& ansDenom, int num1, int denom1, int num2, int denom2)
        {

        }

void subractRational (int& ansNum, int& ansDenom, int num1, int denom1, int num2, int denom2)
        {

        }
Last edited on
Man this code is pissing me off. Wish I didn't have the flu last week and miss class.
@BurgerTown :
i writer for request of you , but i dont understand why you use 'int' in fuction getRational and subtractRational ! i think you should 'float' because if you use 'int' reslut nocorrect !
ps : i am vietnamese so my english bad ! sorry !
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
#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 numer1;
int denom1;
int numer2;
int denom2;
int ansNum , ansDenom;

        cout << "Input the first rational numerator and then the first rational denominator: => ";
        cin >> numer1;
        cin >> denom1;
        displayRational(numer1,denom1);
        getRational ( numer1,denom1);
        cout << "Input the second rational numerator and then the second rational denominator: => ";
        cin >> numer2;
        cin >> denom2;
        displayRational(numer2,denom2);
        getRational ( numer2,denom2);
        addRational ( ansNum,  ansDenom,  numer1,  denom1,  numer2,  denom2);
        subtractRational ( ansNum,  ansDenom,  numer1,  denom1,  numer2,  denom2);
        system ("pause");


return 0;
}

void getRational (int& numer, int& denom)
        {
                int rational;
                rational =(numer / denom);
                cout<<" Result of getRational : " << rational << endl;
        }

void displayRational (int numer, int denom)
        { 
			cout<<" Output the rational numerator and then the ratinal denominator  : " << numer << "  , " << denom << endl; 
        }

void addRational (int& ansNum, int& ansDenom, int num1, int denom1, int num2, int denom2)
        {
			ansNum = num1 + num2;
			ansDenom = denom1 + denom2 ;
         cout<<" Result  addratonal of ansNum is :" << ansNum << endl;
         cout<<" Result  addratonal of ansDenom is :" << ansDenom << endl;
        }

void subtractRational (int & ansNum, int& ansDenom, int num1, int denom1, int num2, int denom2)
        { 
     
         cout<<" Result subrsctRationla of number is : " << ansNum << endl;
         cout<<" Result subractRational of denom is : " << ansDenom << endl;
        }


Program:

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
#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 fixed the error message, but now I have a different question.

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 code is above.

Thanks
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;
        }
A couple things I see:
* getRational() shouldn't be printing anything. It looks like it should be doing the input portion (using cin).
* addRational() could become inaccurate because you are relying on floating point arithmetic. I assume the intent was to have you find a common denominator and then add the numerators and avoid that issue.
* subtractRational() is not even correct. Subtract the same way you add; find a common denominator then subtract the numerators.
Topic archived. No new replies allowed.