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
include <iostream>
usingnamespace std;
class fraction
{
voidfriend sum();
voidfriend 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()
{
}
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
{
voidfriend sum();
voidfriend compare ();
#include <iostream>
usingnamespace std;
class fraction
{
friendvoid sum(fraction a, fraction b);
friendvoid 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()
{
}