unhandled exception

I created a fraction class function whose job is to reduce fractions. I am able to match my instructor output with the given fractions 8/9 and 2/3. However, he says the function must be able to reduce any fraction so I replaced 8/9 with 23/17 to test it. Now Visual Studio throws an unhandled exception saying there is division by zero. So I couted the numerator and denominator right before the line with the exception and it is numerator = 46, and denominator = 2. So, I can't understand why the compiler is calling it integer division by zero, and I can't fix it if I don't know what the problem is.

The line with the error is and it's near the bottom: numerator = numerator / reducedNumerator;

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
 void Fraction::simplify()
{
	cout << numerator << endl;
	cout << denominator << endl;
	int reducedNumerator = numerator;
	int reducedDenominator = denominator;
	const int myArray_SIZE = 102, prime_SIZE = 26;
	int prime[prime_SIZE] = { 2,3,5,7,11,13,17,19,23,29,31,37,41,43,
					47,53,59,61,67,71,73,79,83,89,97,101 };
	int myArray1[myArray_SIZE], myArray2[myArray_SIZE];

	for (int i = 0; i < myArray_SIZE; i++)
	{
		myArray1[i] = 0;
		myArray2[i] = 0;
	}
	for (int i = 0; i < prime_SIZE; i++)
	{
		while (reducedNumerator % prime[i] == 0) {
			reducedNumerator = reducedNumerator / prime[i];
			cout << "test counting prime numbers: " << prime[i] << " ," << 1 + myArray1[prime[i]]++;
			cout << endl;
		}
	}
	for (int i = 0; i < prime_SIZE; i++) {

		while (reducedDenominator % prime[i] == 0) {
			reducedDenominator = reducedDenominator / prime[i];
			/*cout << "\n\ntest counting prime numbers: " << prime[i] << " ," << */1 + myArray2[prime[i]]++;
			//cout << endl;
		}
	}
	reducedNumerator = 0, reducedDenominator = 0;
	for (int i = 0; i < prime_SIZE; i++)
	{	//cout << myArray1[prime[i]] << endl;
		//cout << "my array2 at index " << i << " is : " << myArray2[prime[i]] << endl;

		if (myArray1[prime[i]] > 0) {
			reducedNumerator = reducedNumerator + myArray1[prime[i]];
			//cout << " now the reduced numerator is " << reducedNumerator << endl;
		}
		if (myArray2[prime[i]] > 0) {
			reducedDenominator = reducedDenominator + myArray2[prime[i]];
			//cout << "now the reduced denominator is " << reducedDenominator << endl;
		}

	}
	cout << "the numerator is " << numerator << endl;
	cout << "the denominator is " << denominator << endl;
	cout << "now the reduced numerator is " << reducedNumerator << endl;
	cout << "now the reduced denominator is " << reducedDenominator << endl;

	numerator = numerator / reducedNumerator;
	denominator = denominator / reducedDenominator;
	if (numerator == denominator) {
		numerator = reducedNumerator;
		denominator = reducedDenominator;

	}
}
Last edited on
please explain what you are doing.
perhaps provide some examples of the computations too.


8/9, 2/3, 23/17 are all irreducible fractions, your function should be doing nothing.
also if (numerator == denominator) that's 1.
can't you reduce the fractions with a while/gcd() loop?
Okay to clarify there is a constructor that initializes 8/9, 2/3 and these values are sent though numerous functions to get the results as follows.
1
2
3
4
5
The product of 9/8 and 2/3 is 3/4
The quotient of 9/8 and 2/3 is 27/16
The sum of 9/8 and 2/3 is 43/24
The difference of 9/8 and 2/3 is 11/24
The two Fractions are not equal.

I am able to get my output to match and since the simplify function is supposed to reduce any fraction I changed 8/9 to 23/17 as a test. Then the first function call multiplies 23/17 * 2/3 = 46/51 correctly. Inside the simplify function my algorithm is simplifying the numerator and denominator by dividing with prime numbers and there is a counter for both the numerator and the denominator to count how many times a prime divides evenly. Prime factors of 46 are 2 and 23 so the count is two, and when I divide 46/2 this is where the compiler throws an exception saying integer division by zero.

My approach for this division is I'll do the same with the denominator and if the results are equal then the fraction is assigned the reducible numerator and denominator. If the results are not equal then the fraction is not reducible and the numerator and denominator will not be assigned the reduced numerator and denominator.

I am not allowed to use GCF.
1
2
numerator = numerator / reducedNumerator;
denominator = denominator / reducedDenominator;

This is 46/2 and 51/2.

So the comparison if(numerator == denominator) is if(23 == 51/2) . This is not equal so this fraction is not reducible.
I am not sure where /0 is happening, but does this help you? Its going to freak if you give it a big number not factorable by your prime list, but otherwise, I think these simple changes are what you seek?

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
//changed it to a local function to test it.  
 void simplify()
{
	int numerator =  120; 
	int denominator = 33;
	
	cout << numerator << endl;
	cout << denominator << endl;
	int reducedNumerator = numerator;
	int reducedDenominator = denominator;
	const int myArray_SIZE = 102, prime_SIZE = 26;
	int prime[prime_SIZE] = { 2,3,5,7,11,13,17,19,23,29,31,37,41,43,
					47,53,59,61,67,71,73,79,83,89,97,101 };
	int myArray1[myArray_SIZE], myArray2[myArray_SIZE];

	for (int i = 0; i < myArray_SIZE; i++)
	{
		myArray1[i] = 0;
		myArray2[i] = 0;
	}
	for (int i = 0; i < prime_SIZE; i++)
	{
		while (reducedNumerator % prime[i] == 0  && reducedDenominator % prime[i] == 0) {
			reducedNumerator = reducedNumerator / prime[i];
			reducedDenominator /= prime[i];
			cout << "test counting prime numbers: " << prime[i] << " ," << 1 + myArray1[prime[i]]++;
			cout << endl;
		}
	}
	/*
	for (int i = 0; i < prime_SIZE; i++) {

		while (reducedDenominator % prime[i] == 0 && reducedNumerator % prime[i] == 0) {
			reducedDenominator = reducedDenominator / prime[i];
			//cout << "\n\ntest counting prime numbers: " << prime[i] << " ," << 1 + myArray2[prime[i]]++;
			//cout << endl;
		}
	}
	/*
	reducedNumerator = 0, reducedDenominator = 0;
	for (int i = 0; i < prime_SIZE; i++)
	{	//cout << myArray1[prime[i]] << endl;
		//cout << "my array2 at index " << i << " is : " << myArray2[prime[i]] << endl;

		if (myArray1[prime[i]] > 0) {
			reducedNumerator = reducedNumerator + myArray1[prime[i]];
			//cout << " now the reduced numerator is " << reducedNumerator << endl;
		}
		if (myArray2[prime[i]] > 0) {
			reducedDenominator = reducedDenominator + myArray2[prime[i]];
			//cout << "now the reduced denominator is " << reducedDenominator << endl;
		}

	}  */
	cout << "the numerator is " << numerator << endl;
	cout << "the denominator is " << denominator << endl;
	cout << "now the reduced numerator is " << reducedNumerator << endl;
	cout << "now the reduced denominator is " << reducedDenominator << endl;

	numerator = numerator / reducedNumerator;
	denominator = denominator / reducedDenominator;
	if (numerator == denominator) {
		numerator = reducedNumerator;
		denominator = reducedDenominator;

	}
}


your problem is the math not the code, I think. to reduce a fraction, you have to do to the top and bottom the same thing, or is not equal anymore. so if you have 50/100 you divide both by 2. that gives 25/50 and so on. If you have something like 33/50 you can't divide the top by 3 and the bottom by 2, that isn't the same value anymore.
Last edited on
46/51 is in no way equal to 23/25

> my algorithm is simplifying the numerator and denominator by dividing with prime numbers
first caveat, you should use the same factor for both numerator and denominator.
say that your input was 8/9, your algorithm will test 8 by 2 and 9 by 3, that's incorrect

> Prime factors of 46 are 2 and 23 so the count is two, and when I divide 46/2
reduced{Numerator,Denominator} has the count of prime factors, you divide by the count
suppose your input is 25 = 5*5, you the count is 2 and you do 25/2, that's incorrect.

> So the comparison if(numerator == denominator).
> This is not equal so this fraction is not reducible.
again if your fraction is x/x that's equal 1.
now test your input with 8/2, which is 4
you do the count and have reducedNumerator = 3, reducedDenominator = 1
the division gives you 8/3 == 2/1, which is equal and you enter the if
then you return 3/1... that's incorrect


go back to the drawing board.


Edit:
> and when I divide 46/2 this is where the compiler throws an exception saying integer division by zero.
couldn't reproduce, provide a full testcase. http://www.eelis.net/iso-c++/testcase.xhtml
Last edited on
I appreciate the feedback guys. I got it to work and I took it a step further. The function could not reduce any fraction due to the limited amount of prime numbers in the prime array so I got rid of it and the counter arrays. Now I am using a counter controlled loop with the numerator and the denominator as the limit and it can now reduce any fraction.

Thank you!
Topic archived. No new replies allowed.