Help finishing code

Is anybody capable of finishing up my code.

If anybody is able to complete this part for me, you will be greatly appreciated.

This is my code. . This is suppose to take in a numerator and denominator, do the math, then reduce it to lowest terms.

So far the code runs fine, but I am not able to finish it.

I need the answer to come out in this form.

The number 2/8 should be reduced to 1/4.

9/6 should be reduced to 3/2, 3/2 should be displayed as 1 1/2.


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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
using namespace std;
void showFrac();
void add(int N1, int N2, int D1, int D2, int numrator, int denominator);
void subtract(int N1, int N2, int D1, int D2, int numrator, int denominator);
void multiply(int N1, int N2, int D1, int D2, int numrator, int denominator);
void divide(int N1, int N2, int D1, int D2, int numrator, int denominator);
void Quit();
int main()
{
    showFrac();
    system ("pause");
}
void showFrac()
{
    int N1, N2, D1, D2, numrator = 0, denominator = 0;
    int choose( '\0' );
    while (choose != 5)
    {
        cout<<" \n 1 : Add fraction";
        cout<<" \n 2 : Subtract fraction";
        cout<<" \n 3 : Multiply fraction";
        cout<<" \n 4 : Divide fraction";
        cout<<" \n 5 : Quit";
		cout<<" \n Your choice: ";
        cin >> choose;
        if (choose <=4)
        {
            cout <<"\n Enter first numerator: ";
            cin >> N1;
            cout <<"\n Enter first denominator: ";
            cin >> D1;
            cout <<"\n Enter second numerator: ";
            cin >> N2;
            cout << "\n Enter second denominator: ";
            cin >> D2;
        }
        switch(choose)
        {
        case 1:
            add(N1, N2, D1, D2, numrator, denominator);
            break;
        case 2:
            subtract(N1, N2, D1, D2, numrator, denominator);
            break;
        case 3:
            multiply(N1, N2, D1, D2, numrator, denominator);
            break;
        case 4:
            divide(N1, N2, D1, D2, numrator, denominator);
            break;
        case 5:
            Quit();
			break;
        default:
            cout<<"Enter correct input"<<endl;
        }
    }
}
void add(int N1, int N2, int D1, int D2, int numrator, int denominator)
{
    if(D1 != 0 && D2 != 0)
    {
        numrator = ((N1 * D2) + (N2 * D1));
        denominator = D1 * D2;
        cout << N1 << "/" << D1 << " " << "+" << " " << N2 << "/" << D2 << "=" << numrator << "/" << denominator;
    }
}
void subtract(int N1, int N2, int D1, int D2, int numrator, int denominator)
{
    if(D1 != 0 && D2 != 0)
    {
        numrator = ((N1 * D2) - (N2 * D1));
        denominator = D1 * D2;
        cout << N1 << "/" << D1 << " " << "-" << " " << N2 << "/" << D2 << "=" << numrator << "/" << denominator;
    }
}
void multiply(int N1, int N2, int D1, int D2, int numrator, int denominator)
{
    if(D1 != 0 && D2 != 0)
    {
        numrator = N1 * N2;
        denominator = D1 * D2;
        cout << N1 << "/" << D1 << " " << "*" << " " << N2 << "/" << D2 << "=" << numrator << "/" << denominator;
    }
}
void divide(int N1, int N2, int D1, int D2, int numrator, int denominator)
{
    if(D1 != 0 && D2 != 0)
    {
        numrator = N1 * D2;
        denominator = N2 * D1;
        cout << N1 << "/" << D1 << " " << "/" << " " << N2 << "/" << D2 << "=" << numrator << "/" << denominator;
    }
}
void Quit()
{
    cout << "\n\tThank You\n\n";
}
Try this as an alternative, what do you mean but you're not able to finish it?


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
int main()
{
    int a, b;
    cout << "Enter numerator then denominator:\n";
    cin >> a >> b;

    int d;
    d = 2;
    while (d <= a)
    {
        while (d <= a)
        {
            if (a % d == 0 && b % d == 0)
            {
                a /= d;
                b /= d;
                d = 2;
                continue;
            }
            else d++;
        }
    }

    cout << a << " / " << b << endl;
    return 0;
}


Could you now modify it so it can work on improper fractions too (numerator > denominator) ?
Last edited on
I mean I can't complete what I am trying to do lol which is have the answer come out in a mixed fraction or whole number if possible.

so if the answer is 1/1 then it should display just 1, not 1/1.

and if the answer is 9/6, the it should reduce it to 3/2, and finally display 1 1/2.
Can anybody please help with this ?
I can honestly say that i am stumped, , please help out if you can.

so if the answer is 1/1 then it should display just 1, not 1/1.



Can you use an if statement for that, or a switch statement to account for all 3 cases, for example?
Here is a code snippet for you to apply to your application about the output of the fraction:
1
2
3
4
5
6
7
8
9
// Check to see if it's a whole number
if (numerator % denominator == 0)
   ;// Display whole number
// Check to see if it's a mixed fraction
else if (numerator / denominator > 0)
   ; // Display mixed fraction
// Anything else would be a simplified fraction
else
   ; // Display simplified fraction 


I didn't look at the variables that you used, but I'm not going to write the entire thing for you either. Hope this helps you get your problem solved.
Last edited on
1
2
3
// Check to see if it's a mixed fraction
else if (number / denominator > 0)
   ; // Display mixed fraction 


Shouldn't that be:
else if (numerator / denominator) > 1)
Why should it be greater than one? numerator and denominator are ints, do you know how int division works?
Duh, brain fart. Integer division discards the fractional part so yes, 0 will work fine. Of course greater than 1 will work too and it seemed a bit more logical for a human reading the code. At least for me anyway...
No it wouldn't 3/2 would return 1 which would be false. I understand your logic though. But since C++ does it's division a certain way, it has to be like that.
Note that for positive a and b, a/b>1 if and only if a>b.
Last edited on
@helios
That only applies to common math, not C++ math. According to your logic, 3 > 2 which satisfies your last criteria, but 3/2 is not > 1, it's equal to 1, which makes your second criteria incorrect, and 3 and 2 are both positive, which means your first is correct. The same is true for any two numbers where:
a > b
a < 2b
a / b = 1

Note: This only applies to non floating point numbers.
Last edited on
And your point is...? OP is trying to duplicate the algebraic properties of rational numbers, not of integers.
My point is that the OP has the numerator and denominator defined as ints and int division returns an int, therefore he must use C++'s int division to test their answers. That is why your post was irrelevant to hit problem.

Edit: Without having the op change their program and without introducing something they may/may not know or understand, my equation is the correct one.
Last edited on
OP has the numerator and denominator defined as ints and int division returns an int, therefore he must use C++'s int division to test their answers
That's a non sequitur.

1. It's possible to predict to some extent the result of an operation without actually carrying it out. For example, a/b>1 if and only if a>b:
1
2
3
4
5
inline int abs(int x){
    return (x>=0)?x:-x;
}
//...
if (abs(a)>abs(b)) //... 


2. Less relevantly, there's more than one way to perform integer division.
You're suggesting instead of doing the division, simply checking if the two values are greater? That would be right, and I'm sorry for misunderstanding. I thought you were trying to say that the division should be greater than 1. And without casting one of the ints to a floating point, how else can you do int division?
Topic archived. No new replies allowed.