Fraction Help!

I am writing a program with Visual Basic 2010 C++. I have almost finished but I can't figure out my logic for the Euclidean algorithm. Here is the code I have 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
#include "stdafx.h"
#include <iostream>
using namespace std;

class Fraction
{
public:
	void output1()
	{
	double decimal = static_cast<double> (numerator) / denominator;
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(4);
	cout << "The fraction as a decimal is : " << decimal << ".\n";
	}
	void output2()
	{
		int temp, gcd;
		while (denominator != 0) 
		{
			temp = denominator;
			denominator = numerator % denominator;
			numerator = temp;
			gcd = numerator;
		}
		cout << "The lowest term of the fraction is : " << (numerator / gcd) << "/" << (denominator / gcd) << ".\n";
	}
	void input()
	{
		cout << "Enter a integer for the numerator: ";
		cin >> numerator;
		cout << "Enter a integer for the denominator: ";
		cin >> denominator;
	}
private:
	int numerator;
	int denominator;
};
int main()
{
	Fraction number1;
	number1.input();
	number1.output1();
	number1.output2();
	return 0;
}
Last edited on
When it shows the output I get a numerator as 1 and the denominator as 0. I can't figure out if i am messing up finding the GCD then dividing the numerator and denominator by that or i am not finding the GCD at all.
1.) Why does your Fraction class do IO?
2.) Why do convert your Fraction to double before printing it?
3.) Why can't Fractions be constructed from 2 numbers (int in your case)?
4.) Why is GCD a member function of Fraction?
5.) Why does GCD mutate denominator and numerator?
Last edited on
My first output takes two integers and divides them to make a double.
I needed to make it a double to show the decimal answer.
I just made GCD a function cause I was not sure what to name it, but it I think i am going to change it to output 2 because i need it to spit out the lowest form of the fraction.

I am not sure what you mean by mutate denom and num, but I was trying to use the Euclidean algorithm to find the Greatest common divisor then take that divisor and divide it by the numerator then the denominator to find the lowest term but I messed up my code.
Last edited on
My first output takes two integers and divides them to make a double.
I needed to make it a double to show the decimal answer.


I know you're doing that, I asked you WHY you're doing that.

I just made GCD a function cause I was not sure what to name it, but it I think i am going to change it to output 2 because i need it to spit out the lowest form of the fraction.


I've been trying to tell you that your class shouldn't do either input or output, except for printing itself in a form it can read back.

I am not sure what you mean by mutate denom and num


1
2
3
4
5
6
7
while (denominator != 0) 
{
	temp = denominator;
	denominator = numerator % denominator;
	numerator = temp;
	gcd = numerator;
}


Get yourself a sheet of paper and a pen and play computer. Execute this code please.
Last edited on
Here is the problem I am working through.

Define a class for a type called Fraction. This class is used to represent a ratio of two integers. Include a member function that returns the value of the numerator divided by the denominator as a double. Include an additional member function that outputs the value of the fraction reduced to lowest terms. For example, instead of outputting 20/60 the function should output 1/3. This will require finding the greatest common divisor for the numerator and denominator, and then dividing bot by that number.



oh okay i see I was just switching the numbers around and not finding a number that divides into that.
I think i need a for loop to find the gcd, probably going to have to be nested


Include a member function that returns the value of the numerator divided by the denominator as a double.

That's reasonable. But it's not what you're doing.

This will require finding the greatest common divisor for the numerator and denominator


Exactly. This should be a free function (not belong to your class).
Alright, so i move it down by the main
1
2
3
4
5
6
7
8
9
10
 void fractionReduce(int a, int b)
{



}

int main()
....
....

Last edited on
I was trying to find a way to make two integers a double and i found type casting. is there another way to do it?
No, casting is fine. Stop using void functions all the time though. Just write a gcd function that returns the gcd rather than printing it.
Alright I have fixed it up i am just haveing one last problem.
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

#include "stdafx.h"
#include <iostream>
using namespace std;

class Fraction
{
public:
	void output1()
	{
	double decimal = static_cast<double> (n) / d;
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(4);
	cout << "The fraction as a decimal is : " << decimal << ".\n";
	}
	void output2()
	{
		int temp, temp2, temp3;
	
	while (d != 0)
	{
		temp = n % d;
		temp2 = n;
		temp3 = d;
		n = d;
		d = temp;
	}
	 cout << "The lowest term of the fraction is : " << (temp3 / n) << "/" << (temp2 / n) << "." << endl;
	}
	void input()
	{
		cout << "Enter a integer for the numerator: ";
		cin >> n;
		cout << "Enter a integer for the denominator: ";
		cin >> d;
	}
private:
	int n;
	int d;
};

int main()
{
	Fraction number1;
	number1.input();
	number1.output1();
	number1.output2();
	return 0;
}




I have gotten the GCD, but I need to figure out what if i put in 3/7 which 1/3 comes out and that doesnt have a GCD, so how would i stop that or when i put in 26/28 and it pops out 1/13.
The only fractions that work are the ones that the numerator is the gcd. But for fractions like 26/28 the gcd is 2 reducing it to 13/14 but I don't know how to put that in code.
Topic archived. No new replies allowed.