Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Define an ADT for a set of integers (remember that a set has no concept of dupli

ID: 3786634 • Letter: D

Question

Define an ADT for a set of integers (remember that a set has no concept of duplicate elements, and has no concept of order). Your ADT should consist of the functions that can be performed on a set to control its membership, check the size, check if a given element is in the set, and so on. Each function should be defined in terms of its input and output.
Data Structures and Algorithm Analysis in C++ by Clifford Shaffer Define an ADT for a set of integers (remember that a set has no concept of duplicate elements, and has no concept of order). Your ADT should consist of the functions that can be performed on a set to control its membership, check the size, check if a given element is in the set, and so on. Each function should be defined in terms of its input and output.
Data Structures and Algorithm Analysis in C++ by Clifford Shaffer
Data Structures and Algorithm Analysis in C++ by Clifford Shaffer

Explanation / Answer

#include<iostream>
#define MAX 50 //Maximum size defined
using namespace std;
//Class defined
class ADTSet
{
//To store numbers
int set[MAX];
//Current size of the set
int current;
public:
//Default constructor
ADTSet()
{
for(int x = 0; x < MAX; x++)
set[x] = 0;
current = 0;
}
//Returns the availability of the number in the set
//Returns true if number is available otherwise returns false
bool available(int no)
{
for(int x = 0; x < current; x++)
if(set[x] == no)
return true;
return false;
}
//Adds a number to the set if it is not available
void add()
{
int no;
//Accepts a number
cout<<" Enter a number: ";
cin>>no;
//Checks if the size is less than the current
if(checkSize())
{
//Checks if the number is not available in the set
if(!available(no))
{
//Adds the number to the set
set[current] = no;
cout<<" Successfully Inserted"<<no;
//Updates the current size
current++;
//Displays the set
show();
}//End of if
else
cout<<" Number is already available. Cannot insert.";
}//End of if
else
cout<<" Memory is full. Cannot insert.";
}
//Displays the set
void show()
{
cout<<" Set contents: ";
for(int x = 0; x < current; x++)
cout<<set[x]<<" ";
}
//Returns true if the current counter is less than the size otherwise false
bool checkSize()
{
if(current < MAX)
return true;
else false;
}
};
//Main method
int main()
{
//Object created
ADTSet ad;
//To store user choice
char ch;
//Loops till user choice
do
{
ad.add();
cout<<" Would you like to insert (y/n)";
cin>>ch;
}while(ch == 'Y'||ch == 'y');
}

Output:

Enter a number: 10

Successfully Inserted10
Set contents: 10
Would you like to insert (y/n)y

Enter a number: 10

Number is already available. Cannot insert.
Would you like to insert (y/n)y

Enter a number: 20

Successfully Inserted20
Set contents: 10 20
Would you like to insert (y/n)y

Enter a number: 5

Successfully Inserted5
Set contents: 10 20 5
Would you like to insert (y/n)y

Enter a number: 78

Successfully Inserted78
Set contents: 10 20 5 78
Would you like to insert (y/n)y

Enter a number: 5

Number is already available. Cannot insert.
Would you like to insert (y/n)n

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote