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

C++ You main program should be menu driven, prompting the user with the followin

ID: 3744552 • Letter: C

Question

C++

You main program should be menu driven, prompting the user with the following list of options:

1 - Insert 2- remove 3- insertAt 4- insertEnd 5- ReplaceAt 6- SeqSearch 7- PrintList 8- Quit (Unordered Sets)

As explained in this chapter, a set is a collection of dis- tinct elements of the same type. Design the class unorderodsetType, derived from the class unorderedarrayListType, to manipulate sets. Note that you need to redefine only the functions insertAt, insertBnd, and replaceAt. If the item to be inserted is already in the list, the functions insertat and insertknd output an appropriate message. Similarly, if the item to be replaced is already in the list, the function replaceAt outputs an appropriate message. Also, write a program to test your class. 12

Explanation / Answer

#include <iostream>
#include <string>
#include <stdlib.h>
#define MAX 5
using namespace std;

// Class unorderedarrayListType definition
class unorderedarrayListType
{
// Data member to store data
string itemName;
int quantity;
double price;
public:
// Default constructor to initialize data members
unorderedarrayListType()
{
itemName = "";
quantity = 0;
price = 0.0;
}// End of default constructor

// Function to accept data
void accept()
{
// Accepts data from user
cout<<" Enter Item Name: ";
cin>>itemName;
cout<<" Enter Quantity: ";
cin>>quantity;
cout<<" Enter Price: ";
cin>>price;
}// End of function

// Function to set item name
void setItemname(string na)
{
itemName = na;
}// End of function

// Function to set item quantity
void setQuantity(int qty)
{
quantity = qty;
}// End of function

// Function to set item price
void setPrice(double pr)
{
price = pr;
}// End of function

// Function to return item name
string getItemname()
{
return itemName;
}// End of function

// Function to return item quantity
int getQuantity()
{
return quantity;
}// End of function

// Function to return item price
double getPrice()
{
return price;
}// End of function

// Function to display information
void show()
{
cout<<" Item Name: "<<itemName<<" Quantity: "<<quantity<<" Price: $"<<price;
}// End of function
};// End of class

// class unorderodsetType derived from class unorderedarrayListType
class unorderodsetType : public unorderedarrayListType
{
// Creates an array of object of class unorderedarrayListType of size MAX
unorderedarrayListType list[MAX];
// Counter for number of records
static int counter;
public:
// Function to return current record counter
int getRecordCount()
{
return counter;
}// End of function

// Function to insert a record at the beginning
void Insert(unorderedarrayListType data)
{
// Loops till number of records
for(int c = counter; c >= 0; c--)
// Shifts record to its next position
list[c+1] = list[c];
// Assigns the record at pos index position
list[0] = data;
// Increase the record counter by one
counter++;
}// End of function

// Function to remove a record at given parameter position
void remove(int pos)
{
// Loops till number of records
for(int c = pos; c < counter; c++)
// Shifts record to its previous position
list[c] = list[c + 1];
// Decrease the record counter by one
counter--;
}// End of function

// Function to insert a record at given parameter position
void insertAt(int pos, unorderedarrayListType data)
{
// Loops till insert record position passed as parameter
for(int c = counter; c >= pos; c--)
// Shifts record to its next position
list[c+1] = list[c];
// Assigns the record at pos index position
list[pos] = data;
// Increase the record counter by one
counter++;
}// End of function

// Function to insert a record at end
void insertEnd(unorderedarrayListType data)
{
// Assigns the record at counter index position
list[counter] = data;
// Increase the record counter by one
counter++;
}// End of function

// Function to replace a record at the given parameter position
void ReplaceAt(int pos, unorderedarrayListType data)
{
// Replaces the record by calling set functions
list[pos].setItemname(data.getItemname());
list[pos].setQuantity(data.getQuantity());
list[pos].setPrice(data.getPrice());
}// End of function

// Function to search a record
void SeqSearch(unorderedarrayListType data)
{
int found = -1;
// Loops till number of records
for(int c = 0; c < counter; c++)
{
// Checks if current index position data is equals to the parameter object data
if(list[c].getItemname().compare(data.getItemname()) == 0 && list[c].getQuantity() == data.getQuantity() && list[c].getPrice() == data.getPrice())
{
// Set the found status to one
found = 1;
// Display the index position
cout<<" Item available at "<<c<<" index position.";
}// End of if condition
}// End of for loop

// Checks if found value is -1 display record not found
if(found == -1)
cout<<" Item not found.";
}// End of function

// Function to display all the records
void PrintList()
{
// Loops till number of records
for(int c = 0; c < counter; c++)
// Calls the function to display information
list[c].show();
}// End of function
};// End of class
// Initializes static data member
int unorderodsetType::counter = 0;

// Function to display menu and return user choice
int menu()
{
// To store choice
int ch;
// Displays menu
cout<<" 1 - Insert 2- remove 3- insertAt 4- insertEnd 5- ReplaceAt 6- SeqSearch 7- PrintList 8- Quit ";
// Accepts user choice
cout<<" Enter your choice: ";
cin>>ch;
// Returns user choice
return ch;
}// End of function

// main function definition
int main()
{
// Declares class unorderedarrayListType object
unorderedarrayListType data;
// Declares class unorderodsetType object
unorderodsetType ut;
int no;

// Loops till user choice is not 8
do
{
// Calls the function to accept user choice
// Calls the appropriate function based on use choice returned by the function menu()
switch(menu())
{
case 1:
// Checks if current record counter is equals to MAX then display error message
if(ut.getRecordCount() == MAX)
cout<<" ERROR: Not sufficient memory to insert.";
// Otherwise insert the record
else
{
// Calls the function to accept data
cout<<" Enter the data to insert ";
data.accept();
// Calls the function to insert record
ut.Insert(data);
}// End of else
break;
case 2:
// Accepts the position
cout<<" Enter the position to delete: ";
cin>>no;
// Checks for valid record position
if(no >= 0 && no <= ut.getRecordCount())
// Calls the function to delete record
ut.remove(no);
// Otherwise invalid position
else
cout<<" ERROR: Invalid record position.";
break;
case 3:
// Checks if current record counter is equals to MAX then display error message
if(ut.getRecordCount() == MAX)
cout<<" ERROR: Not sufficient memory to insert.";
// Otherwise insert the record
else
{
// Accepts position
cout<<" Enter the position to insert: ";
cin>>no;
// Checks for valid record position
if(no >= 0 && no <= ut.getRecordCount())
{
// Calls the function to accept data
cout<<" Enter the data to insert at "<<no<<" position ";
data.accept();
// Calls the function to insert record
ut.insertAt(no, data);
}// End of if condition
// Otherwise invalid position
else
cout<<" ERROR: Invalid record position.";
}// End of outer else
break;
case 4:
// Checks if current record counter is equals to MAX then display error message
if(ut.getRecordCount() == MAX)
cout<<" ERROR: Not sufficient memory to insert.";
// Otherwise insert the record
else
{
// Calls the function to accept data
cout<<" Enter the data to insert at end ";
// Calls the function to insert record
data.accept();
// Calls the function to insert the record at end
ut.insertEnd(data);
}// End of else
break;
case 5:
// Accepts position
cout<<" Enter the position to replace data: ";
cin>>no;
// Checks for valid record position
if(no >= 0 && no <= ut.getRecordCount())
{
// Calls the function to accept data
cout<<" Enter the data to replace ";
data.accept();
// Calls the function to replace record
ut.ReplaceAt(no, data);
}// End of if condition
// Otherwise invalid position
else
cout<<" ERROR: Invalid record position.";
break;
case 6:
// Calls the function to accept data
cout<<" Enter the data to search ";
data.accept();
// Calls the function to search record
ut.SeqSearch(data);
break;
case 7:
// Calls the function to display data
ut.PrintList();
break;
case 8:
exit(0);
default:
cout<<" Invalid choice!";
}// End of switch - case
}while(1); // End of do - while loop
}// End of main function

Sample Output:

1 - Insert
2- remove
3- insertAt
4- insertEnd
5- ReplaceAt
6- SeqSearch
7- PrintList
8- Quit
Enter your choice: 1

Enter the data to insert

Enter Item Name: Lix

Enter Quantity: 10

Enter Price: 23.22

1 - Insert
2- remove
3- insertAt
4- insertEnd
5- ReplaceAt
6- SeqSearch
7- PrintList
8- Quit
Enter your choice: 7

Item Name: Lix
Quantity: 10
Price: $23.22
1 - Insert
2- remove
3- insertAt
4- insertEnd
5- ReplaceAt
6- SeqSearch
7- PrintList
8- Quit
Enter your choice: 1

Enter the data to insert

Enter Item Name: Dove

Enter Quantity: 23

Enter Price: 56.32

1 - Insert
2- remove
3- insertAt
4- insertEnd
5- ReplaceAt
6- SeqSearch
7- PrintList
8- Quit
Enter your choice: 7

Item Name: Dove
Quantity: 23
Price: $56.32
Item Name: Lix
Quantity: 10
Price: $23.22
1 - Insert
2- remove
3- insertAt
4- insertEnd
5- ReplaceAt
6- SeqSearch
7- PrintList
8- Quit
Enter your choice: 4

Enter the data to insert at end

Enter Item Name: Liril

Enter Quantity: 20

Enter Price: 78.22

1 - Insert
2- remove
3- insertAt
4- insertEnd
5- ReplaceAt
6- SeqSearch
7- PrintList
8- Quit
Enter your choice: 7

Item Name: Dove
Quantity: 23
Price: $56.32
Item Name: Lix
Quantity: 10
Price: $23.22
Item Name: Liril
Quantity: 20
Price: $78.22
1 - Insert
2- remove
3- insertAt
4- insertEnd
5- ReplaceAt
6- SeqSearch
7- PrintList
8- Quit
Enter your choice: 3

Enter the position to insert: 5
ERROR: Invalid record position.
1 - Insert
2- remove
3- insertAt
4- insertEnd
5- ReplaceAt
6- SeqSearch
7- PrintList
8- Quit
Enter your choice: 3

Enter the position to insert: 1
Enter the data to insert at 1 position

Enter Item Name: Cinthol

Enter Quantity: 32

Enter Price: 55.66

1 - Insert
2- remove
3- insertAt
4- insertEnd
5- ReplaceAt
6- SeqSearch
7- PrintList
8- Quit
Enter your choice: 7

Item Name: Dove
Quantity: 23
Price: $56.32
Item Name: Cinthol
Quantity: 32
Price: $55.66
Item Name: Lix
Quantity: 10
Price: $23.22
Item Name: Liril
Quantity: 20
Price: $78.22
1 - Insert
2- remove
3- insertAt
4- insertEnd
5- ReplaceAt
6- SeqSearch
7- PrintList
8- Quit
Enter your choice: 5

Enter the position to replace data: 9

ERROR: Invalid record position.
1 - Insert
2- remove
3- insertAt
4- insertEnd
5- ReplaceAt
6- SeqSearch
7- PrintList
8- Quit
Enter your choice: 5

Enter the position to replace data: 3

Enter the data to replace

Enter Item Name: Colget

Enter Quantity: 40

Enter Price: 89.99

1 - Insert
2- remove
3- insertAt
4- insertEnd
5- ReplaceAt
6- SeqSearch
7- PrintList
8- Quit
Enter your choice: 7

Item Name: Dove
Quantity: 23
Price: $56.32
Item Name: Cinthol
Quantity: 32
Price: $55.66
Item Name: Lix
Quantity: 10
Price: $23.22
Item Name: Colget
Quantity: 40
Price: $89.99
1 - Insert
2- remove
3- insertAt
4- insertEnd
5- ReplaceAt
6- SeqSearch
7- PrintList
8- Quit
Enter your choice: 6

Enter the data to search

Enter Item Name: Pep

Enter Quantity: 10

Enter Price: 20

Item not found.
1 - Insert
2- remove
3- insertAt
4- insertEnd
5- ReplaceAt
6- SeqSearch
7- PrintList
8- Quit
Enter your choice: 6

Enter the data to search

Enter Item Name: Dove

Enter Quantity: 23

Enter Price: 56.32

Item available at 0 index position.
1 - Insert
2- remove
3- insertAt
4- insertEnd
5- ReplaceAt
6- SeqSearch
7- PrintList
8- Quit
Enter your choice: 1

Enter the data to insert

Enter Item Name: Pendrive

Enter Quantity: 40

Enter Price: 110.32

1 - Insert
2- remove
3- insertAt
4- insertEnd
5- ReplaceAt
6- SeqSearch
7- PrintList
8- Quit
Enter your choice: 7

Item Name: Pendrive
Quantity: 40
Price: $110.32
Item Name: Dove
Quantity: 23
Price: $56.32
Item Name: Cinthol
Quantity: 32
Price: $55.66
Item Name: Lix
Quantity: 10
Price: $23.22
Item Name: Colget
Quantity: 40
Price: $89.99
1 - Insert
2- remove
3- insertAt
4- insertEnd
5- ReplaceAt
6- SeqSearch
7- PrintList
8- Quit
Enter your choice: 1

ERROR: Not sufficient memory to insert.
1 - Insert
2- remove
3- insertAt
4- insertEnd
5- ReplaceAt
6- SeqSearch
7- PrintList
8- Quit
Enter your choice: 2

Enter the position to delete: 3

1 - Insert
2- remove
3- insertAt
4- insertEnd
5- ReplaceAt
6- SeqSearch
7- PrintList
8- Quit
Enter your choice: 7

Item Name: Pendrive
Quantity: 40
Price: $110.32
Item Name: Dove
Quantity: 23
Price: $56.32
Item Name: Cinthol
Quantity: 32
Price: $55.66
Item Name: Colget
Quantity: 40
Price: $89.99
1 - Insert
2- remove
3- insertAt
4- insertEnd
5- ReplaceAt
6- SeqSearch
7- PrintList
8- Quit
Enter your choice: 8

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