Why doesnt my code simplify fractions properly?

closed account (2L3UpfjN)
It always reduced fractions with the denominator or numerator having a 0, where it shouldnt.
Thank You for your help
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
#include <iostream>
using namespace std;

void add(int n, int d, int n1, int d1, int& numerator, int& denominator, int& x, int& y, int& a, int& b);
void subtract(int n, int d, int n1, int d1, int& numerator, int& denominator, int& x, int& y, int& a, int& b);
void multiply(int n, int d, int n1, int d1, int& numerator, int& denominator, int& x, int& y, int& a, int& b);
void divide(int n, int d, int n1, int d1, int& numerator, int& denominator, int& x, int& y, int& a, int& b);
void simplify(int& numerator, int& denominator, int& x, int& y, int& a, int& b);

int main()
{
	int n, d, n1, d1, numerator, denominator, x, y, a, b;
	char operation;
	cout << "Please enter the first fractions numerator: ";
	cin >> n;
	cout << endl << "Please enter the first fractions denominator: ";
	cin >> d;
	cout << endl << "Please enter the mathematical operation (+, -, *, /): ";
	cin >> operation;

	cout << endl << "Please enter the second fractions numerator: ";
	cin >> n1;
	cout << endl << "Please enter the second fractions denominator: ";
	cin >> d1;
	cout << endl;

	if (operation == '+')
	{
		add(n, d, n1, d1, numerator, denominator, x, y, a, b);
		cout << "The sum of the fractions is: " << a << '/' << b << endl;
	}

	else if (operation == '-')
	{
		subtract(n, d, n1, d1, numerator, denominator, x, y, a, b);
		cout << "The difference of the fractions is: " << y << "/" << x << endl;
	}

	else if (operation == '*')
	{
		multiply(n, d, n1, d1, numerator, denominator, x, y, a, b);
		cout << "The product of the fractions is: " << y << "/" << x << endl;
	}

	else if (operation == '/')
	{
		divide(n, d, n1, d1, numerator, denominator, x, y, a ,b);
		cout << "The quotient of the fractions is: " << y << "/" << x << endl;
	}
}

void add(int n, int d, int n1, int d1, int& numerator, int& denominator, int& x, int& y, int& a, int& b)
{
	denominator = d * d1;
	numerator = (n * d1) + (n1 * d);
	simplify(numerator, denominator, x, y, a, b);
}

void subtract(int n, int d, int n1, int d1, int& numerator, int& denominator, int& x, int& y, int& a, int& b)
{
	denominator = d * d1;
	numerator = (n* d1) - (n1* d);
	simplify(numerator, denominator, x, y, a, b);
}

void multiply(int n, int d, int n1, int d1, int& numerator, int& denominator, int& x, int& y, int& a, int& b)
{
	denominator = d * d1;
	numerator = n * n1;
	simplify(numerator, denominator, x, y, a, b);
}

void divide(int n, int d, int n1, int d1, int& numerator, int& denominator, int& x, int& y, int& a, int& b)
{
	denominator = d * n1;
	numerator = n * d1;
	simplify(numerator, denominator, x, y, a, b);
}

void simplify(int& numerator, int& denominator, int& x, int& y, int& a, int& b)
{
	a = numerator;
	b = denominator;
	do
	{
		int t = b;
		b = a % b;
		a = t;
	} while (b != 0);
}
closed account (48T7M4Gy)
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
#include <iostream>
using namespace std;

void add(int, int, int, int, int&, int&);

int simplify(int& , int&);

int main()
{
	int n1, d1, n2, d2;

	int numerator;
	int denominator;
	int whole;
	char operation;

	cout << "Please enter the first fractions numerator: ";
	cin >> n1;
	cout << endl << "Please enter the first fractions denominator: ";
	cin >> d1;
	cout << endl << "Please enter the mathematical operation (+, -, *, /): ";
	cin >> operation;

	cout << endl << "Please enter the second fractions numerator: ";
	cin >> n2;
	cout << endl << "Please enter the second fractions denominator: ";
	cin >> d2;
	cout << endl;

	if (operation == '+')
	{
		add(n1, d1, n2, d2, numerator, denominator);
		whole = simplify(numerator, denominator);
		cout << "The sum of the fractions is: " << whole << '-' << numerator << '/' << denominator << endl;
	}

	return 0;
}

void add(int n1, int d1, int n2, int d2, int& numerator, int& denominator)
{
	numerator = (n1 * d2) + (d1 * n2);
	denominator = d2 * d1;
}

int simplify( int& n, int& d )
{
	int whole = n / d;
	n = n % d;

	// add some lines for the fraction common factors
	
	return whole;	
}


Try simplifying your code a bit. Your functions have a complicated parameter structure and it is easier to narrow down problems of passing parameters by relocating simplify(). Your algorithm in simplify is wrong dare I say it. I hope this helps. :)
Last edited on
Topic archived. No new replies allowed.