I created a class Set with elements made up of class Rational. In adding Rationa
ID: 3621660 • Letter: I
Question
I created a class Set with elements made up of class Rational. In adding Rational elements in my class Set, NO element should be repeated. Right now, my problem is that all the loops I tried would not work. Please help.******
#include <iostream>
using namespace std;
class Rational
{
public:
Rational();
Rational(int n);
Rational(int n , int d );
void output();
public:
int num;
int denom;
};
Rational::Rational()
{
num = 0;
denom = 1;
}
Rational::Rational(int n)
{
num = n;
denom = 1;
}
Rational::Rational(int n , int d )
{
num = n;
denom = d;
}
void Rational::output()
{
cout<< num <<"/"<< denom<<endl;
}
class Set
{
public:
Set();
void add_element( Rational& a);
void Set::check(Set& a, Rational& b);
friend ostream& operator<<(ostream& output, const Set& a);
public:
int index;
Rational * p;
};
void main()
{
cout<<Set();
Rational r1(2,3);
Rational r2(2,3);
Rational r3(4,7);
Rational r4(4,7);
Rational r5(4,6);
Rational r6(7,4);
Set a;
a.add_element(r1);
a.add_element(r2);
a.add_element(r3);
a.add_element(r4);
cout<<a<<endl;
a.add_element(r5);
cout<<a;
}
Set::Set()
{
p = new Rational[20];
index=0;
}
ostream& operator<<(ostream& output, const Set & a)
{
///SOMETHING IS WRONG HERE, PLEASE HELP!
for(int i = 0; i<a.index; i++)
output<<a.p[i].num<<"/"<<a.p[i].denom<<" ";
cout<<endl;
return output;
}
void Set::add_element(Rational& b)
{
///SOMETHING IS WRONG WITH THIS LOOP AS WELL
int i = 0;
//for(int i =0; i<index; i++)
if(p[i].num!=b.num && p[i].denom != b.denom)
{ p[index++] = b;}
}
Thanks in advance...
Explanation / Answer
#include <iostream>
using namespace std;
class Rational
{
public:
Rational();
Rational(int n);
Rational(int n , int d );
void output();
public:
int num;
int denom;
};
Rational::Rational()
{
num = 0;
denom = 1;
}
Rational::Rational(int n)
{
num = n;
denom = 1;
}
Rational::Rational(int n , int d )
{
num = n;
denom = d;
}
void Rational::output()
{
cout<< num <<"/"<< denom<<endl;
}
class Set
{
public:
Set();
void add_element( Rational& a);
void check(Set& a, Rational& b);
friend ostream& operator<<(ostream& output, const Set& a);
public:
int index;
Rational * p;
};
Set::Set()
{
p = new Rational[20];
index=0;
}
ostream& operator<<(ostream& output, const Set & a)
{
for(int i = 0; i<a.index; i++) {
output<<(a.p[i]).num<<"/"<<(a.p[i]).denom<<" ";
}
cout<<endl;
return output;
}
void Set::add_element(Rational& b)
{
//index has to be at least one for the loop to start.
if(index < 1) {
index=0; //just to be safe
index++;
p[index] = b;
}
else {
//The bool will be used to check if it is already used
bool alreadyUsed = true;
for(int i =0; i<index; i++) {
if((p[i]).num==b.num && (p[i]).denom == b.denom) {
alreadyUsed = true;
//if it is already used no need to finish the loop
break;
}
else {
//its not used on this rational, but we keep checking the rest in the set
alreadyUsed = false;
}
}
//if it wasn't used we can add it to the set
//note you will need to do some bounds checking here later
if(!alreadyUsed) p[index++] = b;
}
}
int main()
{
cout<<Set();
Rational r1(2,3);
Rational r2(2,3);
Rational r3(4,7);
Rational r4(4,7);
Rational r5(4,6);
Rational r6(7,4);
Set a;
a.add_element(r1);
a.add_element(r2);
a.add_element(r3);
a.add_element(r4);
cout<<a<<endl;
a.add_element(r5);
cout<<a;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.