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

Question #189575

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 }

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-08T05:47:19-0400
#include <iostream>
#include <time.h>
using namespace std;
class MeraSet{
    int *member, count;
    public:
    MeraSet(): count(0) {}
    int Insert(int e){
        if(count == 100){
            cout<<"Set is full!\n";
            return -1;
        }
        if(count == 0){
            member = new int[1];
            count++;
            member[0] = e;
            return 1;
        }
        for(int i = 0; i < count; i++){
            if(member[i] == e) return -1;
        }
        member = (int*) realloc(member, ++count * sizeof(int));
        member[count - 1] = e;
        return 1;
    }
    int Remove(int e){
        for(int j = 0; j < count; j++){
            if(member[j] == e){
                int *temp = new int[count];
                for(int i = 0; i < count; i++) temp[i] = member[i];
                member = (int*) realloc(member, --count * sizeof(int));
                for(int i = 0, k = 0; i < count + 1; i++){
                    if(i == j) continue;
                    else{
                        member[k] = temp[i];
                        k++;
                    }
                }
                delete [] temp;
                return 1;
            }
        }
        return -1;
    }
    void Print(){
        if(count == 0) cout<<"{}";
        else{
            cout<<"{";
            for(int i = 0; i < count; i++) cout<<member[i]<<",";
            cout<<"\b}";
        }
        cout<<endl;
    }
    MeraSet(const MeraSet &another){
        this->count = another.count;
        this->member = new int[this->count];
        for(int i = 0; i < this->count; i++) this->member[i] = another.member[i];
    }
};
class MeraSet2{
    int *member, count;
    public:
    MeraSet2(): count(0) {}
    int Insert(int e){
        if(count == 100){
            cout<<"Set is full!\n";
            return -1;
        }
        if(count == 0){
            member = new int[1];
            count++;
            member[0] = e;
            return 1;
        }
        for(int i = 0; i < count; i++){
            if(member[i] == e) return -1;
        }
        member = (int*) realloc(member, ++count * sizeof(int));
        member[count - 1] = e;
        return 1;
    }
    int Remove(int e){
        for(int j = 0; j < count; j++){
            if(member[j] == e){
                int *temp = new int[count];
                for(int i = 0; i < count; i++) temp[i] = member[i];
                member = (int*) realloc(member, --count * sizeof(int));
                for(int i = 0, k = 0; i < count + 1; i++){
                    if(i == j) continue;
                    else{
                        member[k] = temp[i];
                        k++;
                    }
                }
                delete [] temp;
                return 1;
            }
        }
        return -1;
    }
    void Print(){
        if(count == 0) cout<<"{}";
        else{
            cout<<"{";
            for(int i = 0; i < count; i++) cout<<member[i]<<",";
            cout<<"\b}";
        }
        cout<<endl;
    }
    void operator=(const MeraSet2 &another){
        cout<<"\nUsing assignment operator\n";
        this->count = another.count;
        this->member = new int[this->count];
        for(int i = 0; i < this->count; i++) this->member[i] = another.member[i];
    }
    int operator[](int i){
        if(i < count && i >= 0) return this->member[i];
    }
    MeraSet2(const MeraSet2 &another){
        cout<<"\nUsing copy constructor\n";
        this->count = another.count;
        this->member = new int[this->count];
        for(int i = 0; i < this->count; i++) this->member[i] = another.member[i];
    }
};
int main(){
    MeraSet2 A, B;
    srand(time(NULL));
    for(int i = 0; i < 10; i++) A.Insert(rand() % 99 + 1);
    A.Print();
    B = A;
    B.Print();
    MeraSet2 C = MeraSet2(A);
    C.Print();
    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