1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
using namespace std;
using std::time;
using namespace std::chrono;
int Menu(); // Menu for sorting and search options.
void bubbleSort(int [], int); // Applies bubble sort algorithm to sort the elements of an unsorted array
void selectionSort(int [], int); // Applies selection sort algorithm to sort the elements of an unsorted array
int linearSearch(int [], double[], int, int ); // Applies the linear search algorithm to search for book ID
int binarySearch(int [], double [], int, int); // Applies the binary search algorithm to search for book ID
void display(string [], int [], double [], int); // To display the contents of parallel array in a tabular format
int main()
{
int size = 10, choice, searchKey, searchKey2, result, result2, quantity=0, quantity2=0;
const int SIZE = 1000; // Constant max for number of numbers random number generator will output
const int MAXRANGE = 500; // Constant range for the range of numbers random number generator will sort through
int selectionsort[ SIZE ] = {0};
int bubblesort[ SIZE ] = {0};
time_t t;
srand((unsigned) time(&t));
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"}; // Parallel Arrays
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, // Use of parallel arrays to match data with book ID and price
120.00, 42.25, 32.11, 123.75};
while (choice != 5) // Loops as long as user does not enter 5
{
choice = Menu();
if (choice == 5){
cout << "Thanks for stopping by! Please, do come again soon :)"; break;} // Program terminates if user enters 5
switch (choice)
{
case 1:
display(bookTitle, bookID, bookPrice, size); // Displays book title, ID, and price using arrays
cout << "***LINEAR SEARCH***" << endl;
cout << "Please enter a book ID to purchase: ";
cin >> searchKey;
result = linearSearch(bookID, bookPrice, size, searchKey);
if (result >=0){ // Searches in the array for value user enters, this way it will find the price of each book.
cout << "The book " << bookTitle[result] << " was chosen." << endl;
cout << "Please enter how many you would like to purchase: ";
result = linearSearch(bookID, bookPrice, size, searchKey);
cin >> quantity; // Quantity used in order to count number of books using the array the user wishes to purchase.
cout << "You have purchased " << quantity << " " << bookTitle[result] << endl << "For the total price of $" << bookPrice[result] * quantity << endl;
cout << "Thank you for your business!" << endl << endl;
high_resolution_clock::time_point t1 = high_resolution_clock::now();
linearSearch(bookID, bookPrice, size, searchKey);
high_resolution_clock::time_point t2 = high_resolution_clock::now();
auto duration = duration_cast<microseconds>( t2 - t1 ).count();
cout << "The time the process took is " << duration << " milliseconds" << endl << endl;
}
|