Need help struggling for hours on this program C++, submit a header file, an imp
ID: 3832371 • Letter: N
Question
Need help struggling for hours on this program
C++, submit a header file, an implementation file, and a main file.
Create a class called Sets for positive integers that will have the following functions:
addElement , will take a positive integer and add it to the set
getElement, will take a position number and return the element at the position (return -1 if bad position)
getSize, return the size of the set
isSubset, takes a sets object (call it b) and see if the current set (call it a) is a subset of b. If so, return a Boolean true, else return a Boolean false
isProper, takes a sets object (call it b) and see if the current set (call it a) is a proper subset of b. Note, use the isSubset function first then see if b has at least one more element than a
printOrderedPairs, takes a sets object (call it b) and takes the current set (call it a) and prints the ordered pairs of of A X B
Have main create two objects: setA and setB.
Input the values into setA (end with a 0 or negative) and input the values into setB (end with 0 or negative).
Print the ordered pairs using printOrderedPairs.
Print if setA is a subset of setB.
Then print if setA is a proper subset of setB.
C++, submit a header file, an implementation file, and a main file.
Explanation / Answer
//sets.h
//here, size of set must be passed as an argument;
#ifndef sets_h
#define sets_h
#include<iostream>
using namespace std;
class sets
{
unsigned int *a;
unsigned int size;
public:
sets(unsigned int siz)
{
a=new unsigned int[siz];
size=siz;
}
void adddata(unsigned int pos, unsigned int data)
{
a[pos]=data;
}
unsigned int getsize()
{
return size;
}
unsigned int getelement(unsigned int pos)
{
if(pos<=size)
return a[pos];
return -1;
}
bool issubset(sets s)
{
int i,j,there=0;
for(i=0;i<s.getsize();i++)
{
there=0;
for(j=0;j<size;j++)
{
if(a[j]==s.getelement(i))
{
there=1;
break;
}
}
}
if(there==1)
return 1;
return 0;
}
bool isproper(sets s)
{
if(size==s.getsize())
return 0;
return issubset(s);
}
void printorderedpairs(sets s)
{
int i,j;
for(i=0;i<size;i++)
{
for(j=0;j<s.getsize();j++)
{
cout<<endl<<a[i]<<" X "<<s.getelement(j);
}
}
}
~sets()
{
delete[] a;
}
};
#endif /* sets_h */
_________________________________________________________
main.cpp
#include "sets.h"
int main(int argc, const char * argv[])
{
unsigned int n,i,temp;
cout<<"Howmany elements you have in set 1? ";
cin>>n;
sets s1(n);
for(i=0;i<n;i++)
{
cout<<" Enter element "<<i+1<<": ";
cin>>temp;
s1.adddata(i,temp);
}
cout<<"Howmany elements you have in set 2? ";
cin>>n;
sets s2(n);
for(i=0;i<n;i++)
{
cout<<" Enter element "<<i+1<<": ";
cin>>temp;
s2.adddata(i,temp);
}
if(s1.issubset(s2))
cout<<" s2 is a subset of s1";
else
cout<<" s2 is not a subset of s1";
s1.printorderedpairs(s2);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.