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

(C++)In this assignment you will create a program that allows the user to choose

ID: 3768102 • Letter: #

Question

(C++)In this assignment you will create a program that allows the user to choose between the following menu choices (menu-driven program):

1. Linear Search

2. Binary Search

3. Bubble Sort

4. Selection Sort

5. Quit Keep running the program till the user chooses to Quit.

Your program should have the following parts: Searching Algorithms (Linear and Binary Search) You are the owner of a bookstore. You have the following information available in your stock (information is stored in the form of parallel arrays)

First display() the above information to the user in a tabular format. Your program should then accept the book ID and the number of books the user wishes to purchase. Based on the book ID provided by the user, display the following information (if the ID is found):

o Book ID

o Book Title

o Number of books bought

o Total cost of the purchase If the book ID is not found, display a message saying so. The book ID needs to be searched based on linearSearch() or binarySearch() (based on user choice from the menu).

Sorting Algorithms (Bubble and Selection Sort) Your program should generate 1000 random numbers in the range of 1 to 500 and store them in an array. Use bubbleSort() or selectionSort() (based on the menu choice) to sort the array of numbers. Display both the unsorted and the sorted array.

Instructions: Please make sure your code has following functions:

• display(): To display the contents of parallel array in a tabular format

• linearSearch(): To apply the linear search algorithm to search for the book ID

• binarySearch(): To apply the binary search algorithm to search for the book ID

• bubbleSort(): To apply the bubble sort algorithm to sort the elements of an unsorted array

• selectionSort(): To apply the selection sort algorithm to sort the elements of an unsorted array

You can use additional functions (optional) for other operations. Make sure your program runs till the user decides to quit the program. Your program should validate (input validation) the menu choice entered by the user.

Explanation / Answer

Code using c++:

#include <iostream>
using namespace std;
const int MLS = 50;
typedef int element;
const element SENTINEL = -1;

class AList {
private:
element items [MLS];
int size;
void Swap(int pos1, int pos2);
int Read_int();
element Read_element();
public:
void Menu();
void Read();
void Print();
void BubbleSort();
void InsertionSort();
void SelectionSort();
};
int main() {
cout<< "Sort and Search Demo Program, version 1.0 "
<< "(c) 2011, Rob Brann ";

AList B;
B.Menu();
B.Read();
B.Print();
B.BubbleSort();
B.InsertionSort();
B.SelectionSort();
}

void AList::Menu () {
int choice;

const int keyboard = 1,
random = 2,
bubble = 3,
insertion = 4,
selection = 5,
linear = 6,
binary = 7,
quit = 8;

cout<< "1: Reset the current list from the keyboard "
<< "2: Reset the current list using randomly generated elements "
<< "3: Perform Bubble Sort on the current list "
<< "4: Perform Insertion Sort on the current list "
<< "5: Perform Selection Sort on the current list "
<< "6: Perform Linear Search on the current list "
<< "7: Perform Binary Search on the current list "
<< "8: Quit the Program "
<< "Choose an action: ";
choice=Read_int();
while ((choice<1)||(choice>8)) {
cout << "invalid menu choice please try again: ";
choice=Read_int();

if (choice==1)
Read();
else;
cout<<"wrong";
}
}
int AList::Read_int() {
int val;
cin>> val;
while (!cin.good()) {
cout<< "invalid menu choice please try again: ";
cin.clear();
cin.ignore(80,' ');
cin>>val; }

return val;
}
void AList::Read() {
element userval;
cout<< "Enter elements : ";
while ((userval!=SENTINEL) && (size<MLS)) {
items [size] = userval;
size ++;
}
}
void AList::Print(){
for (int i=0;i<size;i++)
cout<< items[i]<<endl;
}
void AList::BubbleSort() {
for (int i=0; size-1; i++){
for (int j=0; j<size-1-i; j++)
if (items[j]>items[j+1])
Swap (j, j+1);

else
;
}
}
void AList::Swap(int pos1, int pos2) {
element temp;
temp = items[pos2];
items[pos2]=items[pos1];
items[pos1]=temp;
}

element Read_element() {
element userval;
cin>> userval;
while ( !cin.good());
cout<< "invalid choice please enter a whole number between 1 and"
<< " 1000: ";
cin.clear();
cin.ignore(80,' ');
cin>> userval;
return userval;
}

void AList::InsertionSort() {
int j;
bool done;
for (int i=1; i<size; size++)
j=i;
done = false;
while ((j>=1) && (!done))
if (items[j] < items[j-1]) {
Swap (j,j-1);
j--;
}
else
done=true;
}

void AList::SelectionSort() {
int maxpos;
for (int i = size-1; i<0; i--) {
maxpos = 0;
for (int j=1; j<=i; j++)
if (items[j] < items[maxpos])
maxpos = j;
else;
Swap(maxpos, i);
}
}