I have an assignment that requires alot of use with classes, the idea of the code is to manipulate fractions to find greatest common denominator (GCD) and other things.
What I need (I know this is long): Create a Fraction class with 2 private member variables numerator and denominator....one constructor with 2 integer parameters num and den with default values 0 and 1......one display function that prints out a fraction in the format numerator/denominator in the proper form such as 2/3 or 1/2. (2/4 for example should be displayed as 1/2)
HINTS FROM PROFESSOR:
1. Add a public membeer function called void proper().... that sets numerator and denomonator to the proper form by dividing both by the GCD(greatest common denominator) of them (example... you declare 12/18... after calling proper()...it becomes 2/3 (as the GCD of these is 6)
2. use public member function GCD() to get the greatest common denominator of the numerator and demoniator...(example 2/4... GCD() returns 2....for 12/18...GCD() returns 6).
[b]Code provided for GCD()
int GCD()
{
int gcd = 0;
int r=0;
u = numerator;
v = denominator;
while (true) {
if (v==0){
gcd = u;
break;
}
else {
r = u%v;
u =v;
v=r;
}
}
return gcd;
}
THEN
Create 2 functions as FRIEND functions of class fraction.
The first one called "sum" takes 2 fraction objects as parameters, it returns a fraction object with the result of the sum of the 2 fraction objects.(example... suppose a=1/4...b=1/2... the result of sum(a,b) should be 3/4.
The second one called "compare"...takes 2 fraction objects as parameters.... it returns 1 if the 2 are equal... and 0 if not. (example....a=1/2...b=2/4.. the result of compare(a,b) is 1)
Write main function to declare fraction objects and demonstrate a working code.
[/b]
So thats what im looking at... here is what I have so far... I know its not very much...
class fraction
{
void friend sum();
void friend compare ();
private:
int numerator;
int denominator;
public:
fraction(const int num = 0, const int den = 1);
int GCD();
void proper();
void fraction::proper()
{
}
int GCD()
{
int gcd = 0;
int r=0;
u = numerator;
v = denominator;
while (true) {
if (v==0){
gcd = u;
break;
}
else {
r = u%v;
u =v;
v=r;
}
}
return gcd;
}
void sum()
{
}
void compare()
{
}
void main()
{
}
I am VERY confused about how to set this up with a "1/2" type input... im use to simple inputs like 1 or 2. If someone could help fill in the gaps a bit I would appreciate it... (keep in mind Im a beginner..)... this is my first time working with anything like this and I am in way over my head
You should put a private: before the private section. I know class access modifiers default to private if not specified otherwise, but it won't kill you to write it explicitly, and it will make it yet a bit more readible. It also will prevent you from making mistakes like in this code:
1 2 3 4 5 6 7 8 9 10 11 12
class fraction
{
voidfriend sum();
voidfriend compare ();
private:
int numerator;
int denominator;
public:
fraction(constint num = 0, constint den = 1);
int GCD();
void proper();
//there should be a }; here.
You are missing the closing braces }; here.
At the end of your code there is a closing brace that doesn't belong there. void main() is stone age C++. It's illegal, main should always return an int.
1 2 3 4
int GCD()
{
//GCD stuff
}
should start with int fraction::GCD()
I am VERY confused about how to set this up with a "1/2" type input
Nowhere in the task description does your prof say you need to do that. "1/2" (etc) is what the output should look like.
Well that makes sense with the "};" I was missing.. haha (my bad)
Obviously the GCD section has been completed by the prof... but I wasnt sure how to use it in the other areas... like I said Im not too great at this stuff.
void fraction::proper() // needs to use GCD to change a variable... so for example.. the user inputs 15 and 20.... then it will run that through GCD... and in turn give a greatest common denominator of 5... and Proper() would produce 3/4. But Im really not sure how to write that.
I know that the "compare" friend is simple enough... with some sort of an if/else statement.
Like if they input 1/3 and 2/6 (a and b, for example)
it would be like...
if a==b
return 1;
else
return 0;
But again... I feel like it would have to run through the functions to bring them down to lowest form (2/6 = 1/3)
Uh wait, the math behind fractions shouldn't be a problem for you. You should definitely understand how to get from 2/6 to 1/3, otherwise all questions directed at programming issues would be pointless.
No its the programming part that i dont understand... my proper() function needs to be set up to reduce the numerator and denominator that is entered by the user (using the GCD function).... but again.. its the coding I dont understand... obviously I understand how to go from 2/6 to 1/3
Well yeah, I am not sure if you really understand, because the code is less than trivial if you understood the math. To get from 2/6 to 1/3 you only need to divide both the numerator and the denominator by the GCD of numerator and denominator. The only thing you really have to look out for is to make sure not to divide by zero (GCD returns 0 if numerator AND denominator are 0).
lol thats what Im saying... I know thats what I need to do... I just dont know how to write it.. like I said im just a beginner in a school course and I dont really understand that all too well. I understand the idea and what it should DO... just not what to CODE..
I cant simply just write numerator/gcd.... I dont know what else to do from there is my problem.
hm... so you are like... a beginner beginner? Like, you don't even have the basics like assignment and function calling? Were you missing in school? Cause if you are already at defining own datatypes with classes, you should have covered those a LONG time ago.
you can't do numerator/gcd, but what you can do is this:
1 2 3 4 5 6 7 8 9 10 11 12
numerator = numerator / GCD();
denominator = denominator / GCD();
//or a slightly less error prone version
int gcd = GCD()
if(! (gcd == 0))
{
numerator = numerator / gcd;
denominator = denominator / gcd;
}
If you have troubles understanding, by any means, ask.
Here is what I have at this point... but I know there is more I need to include in the sum(), compare() and main() sections... I just need to call on the functions I think..... and Im also not too sure where to put the "cout" and "cin" lines to ask the user for a number to represent the fractions... any quick help would be appreciated!
class fraction
{
friend fraction sum (fraction a, fraction b);
friend int compare (fraction a, fraction b);
private:
int numerator;
int denominator;
public:
fraction(const int num = 0, const int den = 1);
int GCD();
void proper();
};
void fraction::proper()
{
int gcd = GCD();
numerator = numerator/gcd;
denominator = denominator/gcd;
}
fraction::fraction(int n, int d)
{
numerator =n;
if (d !=0)
denominator = d;
else {cout<<"error!";exit(1);}
}
int fraction::GCD()
{
int gcd = 0;
int r=0;
int u = numerator;
int v = denominator;
while (true) {
if (v==0){
gcd = u;
break;
}
else {
r = u%v;
u =v;
v=r;
}
}
return gcd;
}
fraction sum(fraction a, fraction b)
{
fraction f(0,1);
return f;
}
int compare (fraction a, fraction b)
{
int result = 0;
return result;
}
wait nevermind about the cin thing... I think I declared that already with the line in the main code... but again... Im just getting a black screen with no outputs at this point
I realize I need to utilize one of the functions to provide a display and cout the information... just not sure where or how
I did haha.. its just above my last post... I did 2 seperate ones... Thats all the coding I have thus far...... I realize I need some work in the fraction sum(), compare(), and main() functions... but Im not really sure what to add... I know that sum must say that you add the 2 together... but I've made so many variables Im not even sure how to write that.... same with compare although thats a bit easier with an if else statement (I think)...
Im not sure if I need to add a "display" function or if I can use one of the existing functions to write in like... Cout<<"the fractions are the same if they are a 1, different if they are a 0"<<this would be the "result" thing i think<<"the fractions added together are"<<This would be the result of the "sum"<<.... and then it would display the fraction in lowest form.
obviously they are not equal... that would result in a "0" on the screen. and it would be some sort of if else statement like...
if fraction a ==fraction b;
return "1";
else
return "0";
but like I said.. I know that doesnt work.. I need help with the coding aspect... and again.. the amount of variables I have used has got me unsure of what to use... a,b,f,u,v... etc.
As for sum...
It would be something like...
f=a+b.... but again.. I know that doesnt work..... I need help to code the rest... I understand what Im supposed to do.. just not how to do it.
If you could just provide me with the code I would need for the sections I am missing info in.... I know its more about learning it but I need to submit this by 630 this afternoon (in about an hour)... I've done this far on my own ... i just need the finishing touches..
Writing the code is more than trivial. Really. You have all that you need, you just gotta think a bit about what you want to do, what you already have, and what you can do with that.
For example, adding fractions together - to add fractions together, you just have to find a common denominator. What is the easier way to get a common denominator? Multiply them!
For example, 2/5 + 3/4 = (2*4 + 5*3)/(5*4) = 23/20
1 2 3 4 5
fraction sum(fraction a, fraction b)
{
return fraction(a.numerator*b.denominator+b.numerator*a.denominator,
a.denominator*b.denominator);
}
Then, equality. When are two fractions equal? When their proper form is equal!
1 2 3 4 5 6
int compare(fraction a, fraction b)
{
a.proper();
b.proper();
return (a.numerator==b.numerator) && (a.denominator == b.denominator);
}
There, all done. Now don't tell me you couldn't have thought of that yourself.