calculation problem using struct with fractions

I'm not getting the correct calculation for the floating point conversion of each of the fractions. I don't know what i'm doing wwrong.

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
// Case Project 1.cpp : main project file.


#include "stdafx.h"
#include <iostream>

using namespace std;

struct Fraction
{
	int numerator, denominator;
};

int main()
{
	float equivalent1, equivalent2 = 0.0;
	Fraction fraction1, fraction2, sum1, sum2;

	cout << "Enter fraction 1 numerator ";
	cin >> fraction1.numerator;
	cout << "Enter fraction 2 denominator ";
	cin >> fraction2.denominator;
	cout << "The floating-point equivalent of " << fraction1.numerator << "/"
	<< fraction1.denominator << " is " << equivalent1 << endl;

	cout << "Enter fraction 2 numerator ";
	cin >> fraction2.numerator;
	cout << "Enter fraction 2 denominator ";
	cin >> fraction2.denominator;
	cout << "The floating-point equivalent of " << fraction2.numerator << "/"
	<< fraction2.denominator << " is " << equivalent2 << endl;
		
	equivalent1 = ((float)fraction1.numerator / fraction1.denominator);
	equivalent2 = ((float)fraction2.numerator / fraction2.denominator);


	return 0;
}


1. You need to cast fraction1.numerator and fraction1.denominator to floats separately, because otherwise you'll end up with incorrect results because integer division is truncating.

equivalent1 = ((float)fraction1.numerator / fraction1.denominator);
will result in e.g. 2.0 where fraction1.numerator is 5 and fraction1.denominator is 2 (even though the result should be 2.5, you're diving two integers and then casting the result to a float, which gives 2.0). You want sometihng more like
equivalent1 = ((float)fraction1.numerator) / ((float)fraction1.denominator);

2. Why are equivalent1 and equivalent2 being assigned after you print them?
I'm still getting the wrong answer.


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
// Case Project 1.cpp : main project file.


#include "stdafx.h"
#include <iostream>

using namespace std;

struct Fraction
{
	int numerator, denominator;
};

int main()
{
	float equivalent1, equivalent2 = 0.0;
	Fraction fraction1, fraction2, sum1, sum2;

	equivalent1 = (float)fraction1.numerator / (float)fraction1.denominator;
	equivalent2 = (float)fraction2.numerator / (float)fraction2.denominator;


	cout << "Enter fraction 1 numerator ";
	cin >> fraction1.numerator;
	cout << "Enter fraction 2 denominator ";
	cin >> fraction2.denominator;
	cout << "The floating-point equivalent of " << fraction1.numerator << "/"
	<< fraction1.denominator << " is " << equivalent1 << endl;

	cout << "Enter fraction 2 numerator ";
	cin >> fraction2.numerator;
	cout << "Enter fraction 2 denominator ";
	cin >> fraction2.denominator;
	cout << "The floating-point equivalent of " << fraction2.numerator << "/"
	<< fraction2.denominator << " is " << equivalent2 << endl;
		


	return 0;
}
Its because of lines 16 and 17. You are assigning to equivalent1/2 the answer of the division of two variables which do not hold a value that you have assigned.

Put line 16 in between lines 26 and 27.
Put line 17 in between lines 33 and 34.
I got what you're saying about the two variables not holding assigned value, but why would I want to move the declaration statements ?
Ok I think you got your lines 16 and 17 confused with 19 and 20. Moving the declaration or the equivalent variables down into the process this time causes the program to execute the second fraction perfectly but the first one still doesn't come out right. I don't understand why it seems like the problem is in the fraction1.denominator variable. It's getting passed first, it seems like it shouldn't have any issue with the value stored in 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
28
29
30
31
32
33
34
35
36
37
38
// Case Project 1.cpp : main project file.


#include "stdafx.h"
#include <iostream>

using namespace std;

struct Fraction
{
	int numerator, denominator;
};

int main()
{
	float equivalent1, equivalent2 = 0.0;
	Fraction fraction1, fraction2, sum1, sum2;

	cout << "Enter fraction 1 numerator ";
	cin >> fraction1.numerator;
	cout << "Enter fraction 2 denominator ";
	cin >> fraction2.denominator;
	equivalent1 = (float)fraction1.numerator / (float)fraction1.denominator;
	cout << "The floating-point equivalent of " << fraction1.numerator << "/"
	<< fraction1.denominator << " is " << equivalent1 << endl;

	cout << "Enter fraction 2 numerator ";
	cin >> fraction2.numerator;
	cout << "Enter fraction 2 denominator ";
	cin >> fraction2.denominator;
	equivalent2 = (float)fraction2.numerator / (float)fraction2.denominator;
	cout << "The floating-point equivalent of " << fraction2.numerator << "/"
	<< fraction2.denominator << " is " << equivalent2 << endl;
		


	return 0;
}
Last edited on
Why not make a class with a constructor and an equivalent method? Maybe add operator<, operator==, and use rel_ops?
Yes you're correct I did mean lines 19 and 20, I apologize. I don't understand why you are casting to double; it has no effect on the number whatsoever. The numerator/denominator values are of type int so even if you type in a value such as 4.33333333 it gets truncated to 4. So when you cast it to float later on it is still 4. Change the members to type float and remove the casts.
I don't understand why you are casting to double; it has no effect on the number whatsoever.


It has an effect on the resulting division. If both numbers are integers, then it will do integer division, but if one (or both) is floating point, the result will be floating point.

IE:

1 / 4 == 0 (integer division)
(float)1 / (float)4 = 0.25f (floating point division)


Anyway I'm jumping into this thread late, but I see something:

lines 21,22 probably meant to be setting fraction1.denominator .. not fraction2.

Other than that I don't see a problem in that latest post.
It has an effect on the resulting division. If both numbers are integers, then it will do integer division, but if one (or both) is floating point, the result will be floating point.

I thought that if the variable being assigned the result was type float it wouldn't matter; guess I was wrong...
No, that's why I said
I wrote:
1. You need to cast fraction1.numerator and fraction1.denominator to floats separately, because otherwise you'll end up with incorrect results because integer division is truncating.

lines 21,22 probably meant to be setting fraction1.denominator .. not fraction2.


I can't believe I didn't see this. I could've swore I looked over it 10 times and didn't see it. Apparantly no one else caught it either! lol That fixed that problem!
Ok. Now I'm adding the second part of the program, which is to calculate the sum of both the fractions by multiplication. Could one of you run the code to see if you can identifiy the calculation error? I'm also considering possibly calculating the common denominator in somehow, any ideas?

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
// Case Project 1.cpp : main project file.
// Convert two fractions to floating-point
// Perform addition of two fractions.
#include "stdafx.h"
#include <iostream>

using namespace std;

struct Fraction
{
	int numerator, denominator;
};

int main()
{
	int commonDenominator = 0;
	float equivalent1, equivalent2 = 0.0;
	Fraction fraction1, fraction2, fraction3, fraction4, sum;
	
	//Collect variables and display floating-point equivalent
	cout << "Enter fraction 1 numerator ";
	cin >> fraction1.numerator;
	cout << "Enter fraction 2 denominator ";
	cin >> fraction1.denominator;
	equivalent1 = (float)fraction1.numerator / (float)fraction1.denominator;
	cout << "The floating-point equivalent of " << fraction1.numerator << "/"
	<< fraction1.denominator << " is " << equivalent1 << endl;

	cout << "Enter fraction 2 numerator ";
	cin >> fraction2.numerator;
	cout << "Enter fraction 2 denominator ";
	cin >> fraction2.denominator;
	equivalent2 = (float)fraction2.numerator / (float)fraction2.denominator;
	cout << "The floating-point equivalent of " << fraction2.numerator << "/"
	<< fraction2.denominator << " is " << equivalent2 << endl;

	//Perform addition by finding a common denominator and multiplying
	//multiplication
	fraction3.numerator = fraction1.numerator * fraction2.denominator;
	fraction3.numerator = fraction1.denominator * fraction2.denominator;
	fraction4.numerator = fraction2.numerator * fraction1.denominator;
	fraction4.denominator = fraction2.denominator * fraction1.denominator;
	//addition
	sum.numerator = fraction3.numerator + fraction4.numerator;
	sum.denominator = fraction3.denominator + fraction4.denominator;

	//Output
	cout << "The sum of the two fraction is " << sum.numerator << "/" << 
		sum.denominator << endl;	
	return 0;
}
Probably,
fraction3.numerator = fraction1.numerator * fraction2.denominator;
fraction3.numerator = fraction1.denominator * fraction2.denominator;
fraction4.numerator = fraction2.numerator * fraction1.denominator;
fraction4.denominator = fraction2.denominator * fraction1.denominator;

Should be
1
2
3
4
fraction3.numerator = fraction1.numerator * fraction2.denominator;
fraction3.denominator = fraction1.denominator * fraction2.denominator;
fraction4.numerator = fraction2.numerator * fraction1.denominator;
fraction4.denominator = fraction2.denominator * fraction1.denominator;
wow. i can't believe i overlooked that. Now i understand that with programming, learning to catch your logic errors can be as much a task as writing the code itself. thank you
Topic archived. No new replies allowed.