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.
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?
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.
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
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.
#include "stdafx.h"
#include <iostream>
usingnamespace 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.