Language is C++ Assignment Description : In this assignment you will create a pr
ID: 3770111 • Letter: L
Question
Language is C++
Assignment Description: 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 book store. You have the following information available in your stock (information is stored in the form of parallel arrays):
string bookTitle[] = {“Starting out with C++”, “Java Programming”, “Software Structures”, “Design and Analysis of Algorithms”, “Computer Graphics”, “Artificial Intelligence: A Modern Approach”, “Probability and Statistics”, “Cognitive Science”, “Modern Information Retrieval”, “Speech and Language Processing”};
int bookID[] = {1101, 1211, 1333, 1456, 1567, 1642, 1699, 1755, 1800, 1999};
double bookPrice[] = {112.32, 73.25, 54.00, 67.32, 135.00, 173.22, 120.00, 42.25, 32.11, 123.75};
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. A sample output has been provided to you at the end of this document. 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. A bonus programming part has been provided below for extra points.
Test efficiency: We want to test the efficiency of our searching and sorting algorithms. To test the efficiency I want you to calculate and display the execution (elapsed) time (in seconds) required for each of the Searching and Sorting techniques. Execution time is calculated from the start of a function call to the end of the function. Look up online resources on how to calculate elapsed time and using the system time library. Can you tell which searching technique is better/faster (linear search vs. binary search) and which sorting technique is better/faster (bubble sort vs. selection sort)?
If someone could help me with this assignment I would greatly appreciate it. Right Now I am completely lost.
Explanation / Answer
I hope this helps You
#include<iostream>
#include<string>
#include<iomanip>
#include<cstdlib>
#include<ctime>
using namespace std;
int displayMenu()
{
int choice;
cout << "**** SEARCHING & SORTING MENU **** ";
cout << " 1. Linear Search ";
cout << " 2. Binary Search ";
cout << " 3. Bubble Sort ";
cout << " 4. Selection Sort ";
cout << " 5. Quit ";
cout << "PLease enter your choice: ";
cin >> choice;
return choice;
}
void display(string *bookTitle, int *bookID, double *bookPrice)
{
cout << "**** LINEAR SEARCH **** ";
cout << "Book ID Title Cost ";
for (int i=0; i<10; i++)
{
cout << bookID[i] << " " << setw(40) << left << bookTitle[i] << " $" << bookPrice[i] << " ";
}
cout << endl;
}
int linearSearch(int *bookID, int id)
{
for (int i = 0; i<10; i++)
{
if (bookID[i] == id)
{
return i;
}
}
return -1;
}
int binarySearch(int *bookID, int id)
{
int max = 9, mid = 5, min = 0;
for (int i = 0; i < 10; i++)
{
mid = (max + min)/2;
if (id == bookID[mid])
{
return mid;
}
else if (id > bookID[mid])
{
min = mid + 1;
}
else
{
max = mid;
}
}
return -1;
}
double calcTotalCost(double *bookPrice, int index, int qty)
{
double price = bookPrice[index];
return price *= qty;
}
void searchOutput(int id, string title, int qty, double cost)
{
cout << "Book ID: " << id << endl;
cout << "Book Title: " << title << endl;
cout << "Number of books bought: " << qty << endl;
cout << "Total Cost: $" << setprecision(2) << fixed << cost << endl;
}
void initializeUnsortedArr(int *array, int size)
{
srand (time(NULL));
for(int i=0;i<size;i++)
{
array[i] = rand() % 500 + 1;
}
}
void displayArray(int *array, int size)
{
for(int i=0;i<size;i++)
{
cout << array[i] << " ";
}
}
void bubbleSort(int *array, int size)
{
while (true)
{
bool swapped = false;
for (int i = 0; i < size - 1; i++)
{
if (array[i] > array[i+1])
{
int copy = array[i];
array[i] = array[i+1];
array[i+1] = copy;
swapped = true;
}
}
if (!swapped) break;
}
}
void selectionSort(int *array, int size)
{
int copy, index;
for(int i = 0; i< 500;i++)
{
int min = 500;
for(int j=i;j<500;j++)
{
if(array[j] < min)
{
min = array[j];
index = j;
}
}
copy = array[i];
array[i]=min;
array[index] = copy;
}
}
void navigateMenu(int choice)
{
string bookTitle[] = {"Starting out with C++", "Java Programming", "Software Structures", "Design and Analysis of Algorithms", "Computer Graphics", "Artificial Intelligence: A Modern Approach", "Probability and Statistics", "Cognitive Science", "Modern Information Retrieval", "Speech and Language Processing"};
int bookID[] = {1101, 1211, 1333, 1456, 1567, 1642, 1699, 1755, 1800, 1999};
double bookPrice[] = {112.32, 73.25, 54.00, 67.32, 135.00, 173.22, 120.00, 42.25, 32.11, 123.75};
int id = 0, copies = 0, index;
double totalPrice;
switch(choice)
{
case 1:
{
display(bookTitle, bookID, bookPrice);
cout << "Please enter the book ID you wish to purchase: ";
cin >> id;
cout << "How many copies you wish to purchase: ";
cin >> copies;
index = linearSearch(bookID, id);
while (index == -1)
{
cout << "That is not an existing book ID. Please enter the book ID you wish to purchase: ";
cin >> id;
index = linearSearch(bookID, id);
}
totalPrice = calcTotalCost(bookPrice, index, copies);
searchOutput(id, bookTitle[index], copies, totalPrice);
break;
}
case 2:
{
display(bookTitle, bookID, bookPrice);
cout << "Please enter the book ID you wish to purchase: ";
cin >> id;
cout << "How many copies you wish to purchase: ";
cin >> copies;
index = binarySearch(bookID, id);
while (index == -1)
{
cout << "That is not an existing book ID. Please enter the book ID you wish to purchase: ";
cin >> id;
index = binarySearch(bookID, id);
}
totalPrice = calcTotalCost(bookPrice, index, copies);
searchOutput(id, bookTitle[index], copies, totalPrice);
break;
}
case 3:
{
int arr[500];
initializeUnsortedArr(arr, 500);
cout << "**** BUBBLE SORT **** ";
cout << "Unsorted array: ";
displayArray(arr, 500);
cout << endl;
bubbleSort(arr, 500);
cout << endl << "Sorted array: ";
displayArray(arr, 500);
cout << endl;
break;
}
case 4:
{
int arr[500];
initializeUnsortedArr(arr, 500);
cout << "**** SELECTION SORT **** ";
cout << "Unsorted array: ";
displayArray(arr, 500);
cout << endl;
selectionSort(arr, 500);
cout << endl << "Sorted array: ";
displayArray(arr, 500);
cout << endl;
break;
}
}
}
int main()
{
int choice = 0;
while (choice != 5)
{
choice = displayMenu();
navigateMenu(choice);
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.