class

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:

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)

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).

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.


So thats what im looking at... here is what I have so far... I know its not very much...

#include <iostream>
using namespace std;

class fraction

{

void friend sum();

void friend compare ();

private:

int numerator;

int denominator;

public:

fraction(int num = 0, 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;

}


else {

r = u%v;

u =v;

}

}

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

Thank you!
For a C++17 starter, consider:

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
#include <iostream>
#include <numeric>
using namespace std;

class fraction {
	friend fraction sum(fraction f1, fraction f2);
	friend bool compare(fraction f1, fraction f2);

	friend ostream& operator<<(ostream& os, fraction f) {
		return os << f.numerator << " / " << f.denominator;
	}

private:
	int numerator {};
	int denominator {1};

public:
	fraction(int num = 0, int den = 1) : numerator(num), denominator(den) {
		proper();
	}

	void proper() {
		if (const auto cd {gcd(numerator, denominator)}; cd) {
			numerator /= cd;
			denominator /= cd;
		}
	}
};

fraction sum(fraction f1, fraction f2)
{
	const auto lc {lcm(f1.denominator, f2.denominator)};
	fraction fs(lc / f1.denominator * f1.numerator + lc / f2.denominator * f2.numerator, lc);

	fs.proper();
	return fs;
}

bool compare(fraction f1, fraction f2)
{
	const auto lc {lcm(f1.denominator, f2.denominator)};

	return lc / f1.denominator * f1.numerator < lc / f2.denominator * f2.numerator;
}

void main()
{
	const fraction f1(3, 4);
	const fraction f2(5, 6);

	cout << f1 << " + " << f2 << " = " << sum(f1, f2) << '\n';
	cout << f1 << " < " << f2 << ' ' << boolalpha << compare(f1, f2) << '\n';
}



3 / 4 + 5 / 6 = 19 / 12
3 / 4 < 5 / 6 true

void main() is not valid C++.
Return type must be int. See https://en.cppreference.com/w/cpp/language/main_function

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.

There is no input mentioned in the task description.
Sorry seeplus
Could it be explained in a clearer way for my understanding cause am still a beginner at this and the code above looks weird to me

1
2
3
friend ostream& operator<<(ostream& os, fraction f) {
		return os << f.numerator << " / " << f.denominator;
	}


This is an overload of the operator << so that it works with the fraction class. This makes displaying fractions easier.

 
cout << f1;


will call the operator<< function above as f1 is of type fraction.


gcd() is greater commnon divisor available in C++17
lcm() is lowest common multiple available in C++17


What line(s) don't you understand?
Last edited on
Can the code below be changed because I have not been thought bool so I don’t really understand it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

friend bool compare(fraction f1, fraction f2);

	friend ostream& operator<<(ostream& os, fraction f) {
		return os << f.numerator << " / " << f.denominator;
	}

fraction sum(fraction f1, fraction f2)
{
	const auto lc {lcm(f1.denominator, f2.denominator)};
	fraction fs(lc / f1.denominator * f1.numerator + lc / f2.denominator * f2.numerator, lc);

	fs.proper();
	return fs;
}

bool compare(fraction f1, fraction f2)
{
	const auto lc {lcm(f1.denominator, f2.denominator)};

	return lc / f1.denominator * f1.numerator < lc / f2.denominator * f2.numerator;
}
This is what am working with but I don’t really know how to complete it from here
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

include <iostream>
using namespace std;

class fraction

{

void friend sum();

void friend compare ();

private:

int numerator;

int denominator;

public:

fraction(int num = 0, 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;

}


else {

r = u%v;

u =v;

}

}

return gcd;

}

void sum()

{

}

void compare()
{
}
void main()
{





}


I just need help with it from here
It is good that you did find the code tags, but your whitespace is abysmal. Unnecessary empty lines and total lack of indentation make code hard to read.

The first one called "sum" takes 2 fraction objects as parameters, it returns a fraction object
1
2
void sum() {
}

Your "sum" does not take any parameters and does not return any value.
Seeplus' "sum" is
1
2
fraction sum(fraction f1, fraction f2) {
}


The second one called "compare"... returns 1 or 0.

The 1 and 0 are not void. They are not bool either. What type can hold values, like 0 and 1?

I did already tell that void main() has an error.

1
2
3
4
class fraction
{
    void friend sum();
    void friend compare ();

Q: Is that how friendship is declared? See https://en.cppreference.com/w/cpp/language/friend
A: No. Must be:
1
2
3
4
class fraction
{
    friend void sum();
    friend void compare ();


Programming is very much about paying attention to details.
Last edited on
this is what have got so far but I still need to complete the :
void sum,
void compare and the int main part
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>

using namespace std;

class fraction

{

	friend void sum(fraction a, fraction b);

	friend void compare(fraction a, fraction b);

private:

	int numerator;

	int denominator;

public:

	fraction(int num = 0, int den = 1);

	int GCD();

	void proper();
};

	void fraction::proper()

	{
		
	}

	int fraction::GCD()

	{

		int gcd = 0;

		int r = 0;

		int u = numerator;

		int v = denominator;

		while (true) {

			if (v == 0) {

				gcd = u;

			}


			else {

				r = u % v;

				u = v;

			}

		}

		return gcd;

	}

	void sum(fraction a, fraction b)
	{
		
	}

	void compare(fraction a, fraction b)
	{
	}
	int main()
	{





	}
1
2
friend void sum(fraction a, fraction b);
friend void compare(fraction a, fraction b);


That is wrong. sum() needs to return a type fraction, and compare() needs to return a bool (or an int if you really must).
what am I to do seeplus cause at this rate am confused already.
Am just a beginner
Sounds like the meaning of "return type" has to be clarified.
Read: http://www.cplusplus.com/doc/tutorial/functions/
Topic archived. No new replies allowed.