Thanks in advance for the help. I am nearly done with this project but I want he
ID: 3621694 • Letter: T
Question
Thanks in advance for the help. I am nearly done with this project but I want help to get unstuck with my set_intersection and set_union functions. Can you look at the functions and make corrections? The criteria is that when two sets a and b of rational numbers intersect, it should show their common elements in another set? In set_union, it will combine all the elements without repetition of elements. This is a simple C++ program and without using STL or templates or vectors. I've already written the code for the functions, they just won't work, please help. And while we're at it, I still need another function to work on - increment. But I think I can figure that one out. Thanks again.#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;
int i = 0;
//there should be an if statement that reduces the fraction
for (i = 20; i > 1; i--)
if ((num % i == 0) && (denom % i == 0))
{
num /= i;
denom /= i;
}
}
void Rational::output()
{
cout<< num <<"/"<< denom<<endl;
}
class Set
{
public:
Set();
Set(const Set& a);
~Set();
void operator= (const Set& z);
void add_element( Rational& a);
Set& set_intersection( Set& g);
Set& set_union(Set& g);
friend ostream& operator<<(ostream& output, const Set& a);
public:
int index;
Rational * p;
};
void main()
{
cout<<Set();
Set a;//call to default constructor
Rational r1(2,3);
Rational r2(3,4);
Rational r3;
Rational r4(4,6);
Rational r5(4,6);
Rational r6(3,5);
Rational r7(8,9);
a.add_element(r1);
a.add_element(r2);
a.add_element(r3);
a.add_element(r4);
cout<<a<<endl;
Set b = a;//call to the copy const
Set c(a);//call to copy constructor
Set d; //call to the default constructor
d=a; //call to the overloaded assignment
a.add_element(r5);
cout<<"A default"<<a;//print default
cout<<"B copy cons"<<b;//print copy constructor
cout<<"C default"<<c;//default
cout<<"D overloaded assignment"<<d;//? assignment?
b.add_element(r6);
b.add_element(r7);
cout <<"After r6 was added to B, B now is" << b<<endl;
Set what = a.set_intersection( b);
cout<<what;
}
Set::Set()
{
p = new Rational[100];
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)
{
/*** I will need to reduce Rational in its simple form first; should I go back to Rational class?:*/
//index has to be at least one for the loop to start.
if(index < 1)
{
index = 0; //just to be safe
p[index] = b;
index++;
}
else {
//A bool will be used to check if an element is already added
bool element_present = true;
for(int i =0; i<index; i++) {
if((p[i]).num==b.num && (p[i]).denom == b.denom)
{
element_present = true;
//if it is already added no need to finish the loop
break;
}
else
{
//its not used on this rational, but we keep checking the rest in the set
element_present = 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(!element_present) p[index++] = b;
}
}
Set::Set(const Set& z)
{
index = z.index;
p = new Rational[100];
for (int i =0; i<index; i++)
{p[i].num = z.p[i].num;
p[i].denom = z.p[i].denom;}
}
void Set::operator=( const Set& z)
{
index = z.index;
for (int i = 0; i<index; i++)
{p[i].num = z.p[i].num;
p[i].denom = z.p[i].denom;}
}
Set& Set::set_intersection(Set& g)
{
for(int i = 0; i<index; i++)
for(int j= 0; j<index; j++)
if(p[i].num != g.p[j].num && p[i].denom != g.p[j].denom)
{
g.p[index++].num = p[i].num;
g.p[index++].denom = p[i].denom;
return g;
}
else
cout<<"heck";
}
Set& Set::set_union(Set& g)
{
for(int i = 0; i<index; i++)
for(int j= 0; j<index; j++)
if(p[i].num == g.p[j].num && p[i].denom == g.p[j].denom)
{
g.p[index++].num = p[i].num;
g.p[index++].denom = p[i].denom;
return g;
}
else
cout<<"heck";
}
Set::~Set()
{
}
Explanation / Answer
Dear, In this two functions already the parameters are reference type so need not have return type for it void Set::set_intersection(Set& g) { for(int i = 0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.