Answer to Question #190514 in C++ for zain ul abdeen

Question #190514

Implement a class MeraSet, which abstracts a mathematical Set of integral numbers. Maximum members can be 100. Implement following Member functions:


int Insert(int e) ; // adds value e to the set and returns 1, if the value already exists or the set has already 100 members, then it does not add and return -1.

int Size(); // return size of the set

int Remove(int e); // removes e from Set, and return 1. If e is not member of set, returns -1.

void Print(); // prints the members of the set in set notation e.g. Set = {1,2,3,8,-3 }

operator + ; // returns union of two sets

operator -; // returns difference set of two sets i.e. A-B


Make private member variables and more functions as you like.

Implement MeraSet2 class similar to last assignment, Add following members:

operator =; // assignment operator, makes deep copy

Copy constructor 

operator []; // implement subscript operator, to display individual element of a set.



1
Expert's answer
2021-05-09T11:50:03-0400


#include <iostream>
#include <string>
using namespace std;


class MeraSet{
private:
	int MAXIMUM_NUMBERS;
	int totalNumbers;
	int values[100];
public:


	MeraSet(){
		this->MAXIMUM_NUMBERS=100;
		this->totalNumbers=0;
	}
	// adds value e to the set and returns 1, if the value already exists or the set has already 100 members, 
	//then it does not add and return -1.
	int Insert(int e){
		if(totalNumbers<MAXIMUM_NUMBERS){
			if(valueExists(e)==-1){
				values[totalNumbers]=e;
				totalNumbers++;
				return 1;
			}			
		}
		return -1;
	}
	int valueExists(int e){
		for(int i=0;i<totalNumbers;i++){
			if(e==values[i]){
				return i;
			}
		}
		return -1;
	}


	//return size of the set
	int Size(){
		return totalNumbers;
	}
	// removes e from Set, and return 1. If e is not member of set, returns -1.
	int Remove(int e){
		if(totalNumbers<MAXIMUM_NUMBERS){
			int index=valueExists(e);
			if(index!=-1){
				for(int i=index;i<totalNumbers-1;i++){
					values[i]=values[i+1];	
				}
				totalNumbers--;
				return 1;
			}
		}
		return -1;
	}
	// prints the members of the set in set notation e.g. Set = {1,2,3,8,-3 }
	void Print(){
		if(totalNumbers>0){
			cout<<"Set = {";
			//Set = {1,2,3,8,-3 }
			for(int i=0;i<totalNumbers-1;i++){
				cout<<values[i]<<",";
			}
			cout<<values[totalNumbers-1]<<"}";
		}else{
			cout<<"Set is empty\n";
		}


		cout<<"\n";
	}
	// returns union of two sets
	MeraSet operator+(const MeraSet& meraSet) const{
		 MeraSet result;
		 for(int i=0;i<totalNumbers;i++){
			 result.Insert(values[i]);
		 }
		 for(int i=0;i<meraSet.totalNumbers;i++){
			 result.Insert(meraSet.values[i]);
		 }
		 return result;
	}
	// returns difference set of two sets i.e. A-B
	MeraSet operator-(const MeraSet& meraSet) const{
		 MeraSet result;
		 for(int i=0;i<totalNumbers;i++){
			 bool isTheSame=false;
			 for(int j=0;j<meraSet.totalNumbers;j++){
				 if(meraSet.values[j]==values[i]){
					isTheSame=true;
				}
			 }
			 if(!isTheSame){
				result.Insert(values[i]);
			 } 
		 }
		 return result;
	}
};


int main(){
	MeraSet meraSet1;
	if(meraSet1.Insert(1)==-1){
		cout<<"\nThe value already exists or the set has already 100 members\n\n";	
	}
	if(meraSet1.Insert(1)==-1){
		cout<<"\nThe value already exists or the set has already 100 members\n\n";	
	}
	if(meraSet1.Insert(2)==-1){
		cout<<"\nThe value already exists or the set has already 100 members\n\n";	
	}
	if(meraSet1.Insert(3)==-1){
		cout<<"\nThe value already exists or the set has already 100 members\n\n";	
	}
	if(meraSet1.Insert(8)==-1){
		cout<<"\nThe value already exists or the set has already 100 members\n\n";	
	}
	if(meraSet1.Insert(-3)==-1){
		cout<<"\nThe value already exists or the set has already 100 members\n\n";	
	}
	cout<<"Set 1:\n";
	meraSet1.Print();


	MeraSet meraSet2;
	if(meraSet2.Insert(8)==-1){
		cout<<"\nThe value already exists or the set has already 100 members\n\n";	
	}
	if(meraSet2.Insert(9)==-1){
		cout<<"\nThe value already exists or the set has already 100 members\n\n";	
	}
	if(meraSet2.Insert(10)==-1){
		cout<<"\nThe value already exists or the set has already 100 members\n\n";	
	}
	if(meraSet2.Insert(11)==-1){
		cout<<"\nThe value already exists or the set has already 100 members\n\n";	
	}
	if(meraSet2.Insert(12)==-1){
		cout<<"\nThe value already exists or the set has already 100 members\n\n";	
	}
	if(meraSet2.Insert(-13)==-1){
		cout<<"\nThe value already exists or the set has already 100 members\n\n";	
	}
	cout<<"Set 2:\n";
	meraSet2.Print();


	cout<<"Set 1 + Set 2 :\n";
	MeraSet meraSetSum=meraSet1+meraSet2;
	meraSetSum.Print();


	cout<<"Set 1 - Set 2 :\n";
	MeraSet meraSetDiff=meraSet1-meraSet2;
	meraSetDiff.Print();


	system("pause");
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS