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

Define an ADT for a bag of integers (remember that a bag may contain duplicates,

ID: 3786636 • Letter: D

Question

Define an ADT for a bag of integers (remember that a bag may contain duplicates, and has no concept of order). Your ADT should consist of the functions that can be performed on a bag 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 bag of integers (remember that a bag may contain duplicates, and has no concept of order). Your ADT should consist of the functions that can be performed on a bag 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>
#include<stdlib.h>
#define MAX 50 //Maximum size defined
using namespace std;
//Class defined
class ADTBag
{
//To store numbers
int set[MAX];
//Current size of the set
int current;

public:
//Default constructor
ADTBag()
{
for(int x = 0; x < MAX; x++)
set[x] = 0;
current = 0;
}
//Returns the availability of the number in the set
//Returns index position if number is available otherwise returns -1
int available(int no)
{
for(int x = 0; x < current; x++)
if(set[x] == no)
return x;
return -1;
}
//Returns the current size
int currentSize()
{
return current;
}
//Adds a number to the set if it is not available
void addData()
{
int no;
//Accepts a number
cout<<" Enter a number: ";
cin>>no;
//Checks if the size is less than the current
if(checkSize())
{
//Adds the number to the set
set[current] = no;
cout<<" Successfully Inserted: "<<no;
//Updates the current size
current++;
}//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;
}
//Search an given data in the set
void searchData()
{
int no, pos;
cout<<" Enter a number to search: ";
cin>>no;
pos = available(no);
if(pos != -1)
cout<<" Number "<<no<<" is available at "<<pos;
else
cout<<" Number "<<no<<" not available in the set";
}

//Delete a specified data from the set
void deleteData()
{
int no, pos;
if(current != 0)
{
cout<<" Enter the number to delete: ";
cin>>no;
pos = available(no);
if(pos != -1)
{
for(int x = pos; x < current; x++)
set[x] = set[x+1];
current--;
}
else
cout<<" Number no available";
}
else
cout<<" Empty Set cannot delete";
}
//To display menu
void menu()
{
cout<<" 1) Show Maximum Size";
cout<<" 2) Show Current Size";
cout<<" 3) Add data";
cout<<" 4) Delete data";
cout<<" 5) Search data";
cout<<" 6) Display set";
cout<<" 7) Exit";
}
};
//Main method
int main()
{
//Object created
ADTBag ad;
//To store user choice
int ch;
//Loops till user choice
do
{
ad.menu();
cout<<" Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1:
cout<<" Maximum length of the set is: "<<MAX;
break;
case 2:
cout<<" Current length of the set is: "<<ad.currentSize();
break;
case 3:
ad.addData();
break;
case 4:
ad.deleteData();
break;
case 5:
ad.searchData();
break;
case 6:
ad.show();
break;
case 7:
exit(0);
}//End of switch
}while(1);
return 0;
}

Output


1) Show Maximum Size
2) Show Current Size
3) Add data
4) Delete data
5) Search data
6) Display set
7) Exit
Enter your choice: 1

Maximum length of the set is: 50
1) Show Maximum Size
2) Show Current Size
3) Add data
4) Delete data
5) Search data
6) Display set
7) Exit
Enter your choice: 2

Current length of the set is: 0
1) Show Maximum Size
2) Show Current Size
3) Add data
4) Delete data
5) Search data
6) Display set
7) Exit
Enter your choice: 3

Enter a number: 10

Successfully Inserted: 10
1) Show Maximum Size
2) Show Current Size
3) Add data
4) Delete data
5) Search data
6) Display set
7) Exit
Enter your choice: 3

Enter a number: 20

Successfully Inserted: 20
1) Show Maximum Size
2) Show Current Size
3) Add data
4) Delete data
5) Search data
6) Display set
7) Exit
Enter your choice: 6

Set contents: 10 20
1) Show Maximum Size
2) Show Current Size
3) Add data
4) Delete data
5) Search data
6) Display set
7) Exit
Enter your choice: 5

Enter a number to search: 15

Number 15 not available in the set
1) Show Maximum Size
2) Show Current Size
3) Add data
4) Delete data
5) Search data
6) Display set
7) Exit
Enter your choice: 5

Enter a number to search: 10

Number 10 is available at 0
1) Show Maximum Size
2) Show Current Size
3) Add data
4) Delete data
5) Search data
6) Display set
7) Exit
Enter your choice: 5

Enter a number to search: 20

Number 20 is available at 1
1) Show Maximum Size
2) Show Current Size
3) Add data
4) Delete data
5) Search data
6) Display set
7) Exit
Enter your choice: 4

Enter the number to delete: 12

Number no available
1) Show Maximum Size
2) Show Current Size
3) Add data
4) Delete data
5) Search data
6) Display set
7) Exit
Enter your choice: 4

Enter the number to delete: 10

1) Show Maximum Size
2) Show Current Size
3) Add data
4) Delete data
5) Search data
6) Display set
7) Exit
Enter your choice: 6

Set contents: 20
1) Show Maximum Size
2) Show Current Size
3) Add data
4) Delete data
5) Search data
6) Display set
7) Exit
Enter your choice: 4

Enter the number to delete: 20

1) Show Maximum Size
2) Show Current Size
3) Add data
4) Delete data
5) Search data
6) Display set
7) Exit
Enter your choice: 6

Set contents:
1) Show Maximum Size
2) Show Current Size
3) Add data
4) Delete data
5) Search data
6) Display set
7) Exit
Enter your choice: 4

Empty Set cannot delete
1) Show Maximum Size
2) Show Current Size
3) Add data
4) Delete data
5) Search data
6) Display set
7) Exit
Enter your choice: 7

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