Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Q: Write a program that populates a 10-element array with random numbers (in the range 1..100) by an appropriate function that we've used in class.

Q: Write a program that populates a 10-element array with random numbers (in the range 1..100) by an appropriate function that we've used in class. (C++)

Present the user with a menu: (1) to demonstrate the bubble sort, and (2) to demonstrate the selection sort or (3) to quit. If they continue, they then can select another menu choice and a new 10-element array of random numbers is generated each time.

Your program must call a function that you create for each of these sort functions, and you pass the array to it. Use the code we've already seen in class for these sort functions. Modify your sort functions such that these features/functionalities are added:

In addition to the array being sorted...

Add code to keep a count of how many exchanges/swaps take place to accomplish the sort. Display the results at the end.

Add code to print out the array contents after each pass of the sort.

The code flow will look like this:

User picks 1 for bubble sort or 2 for selection sort or 3 to quit.

If either 1 or 2 is selected, a 10-element array is populated with random numbers, and that array is passed as an argument to either the bubble sort or selection sort function.

The function then processes the sorting but with two extra features thrown in:

Count of exchanges

Printout of the array contents after each pass through the sort

Here is example code we learned in class.

=======================BUBBLE SORT======================== // This program uses the bubble sort algorithm to sort an // array in ascending order. #include  #include  #include  using namespace std; void bubbleSort(int [], int); void showArray(int [], int); const int MAXRAND = 1000, MINRAND = 1, SIZE = 60; int main() { int values[SIZE] = {0}; unsigned seed = time(0); srand(seed); for (int i = 0; i < SIZE; i++) values[i] = rand() % (MAXRAND - MINRAND) + MINRAND; cout << "The unsorted values are: "; showArray(values, SIZE); bubbleSort(values, SIZE); cout << "The sorted values are: "; showArray(values, SIZE); return 0; } void bubbleSort(int array[], int size) { bool swap; int temp; do { swap = false; for (int count = 0; count < (size - 1); count++) { if (array[count] > array[count + 1]) { temp = array[count]; array[count] = array[count + 1]; array[count + 1] = temp; swap = true; } } } while (swap); } void showArray(int array[], int size) { for (int count = 0; count < size; count++) cout << array[count] << " "; cout << endl; } =============SELECT SORT==================== // This program uses the selection sort algorithm to sort an // array in ascending order. #include  #include  #include  using namespace std; void selectionSort(int [], int); void showArray(int [], int); const int MAXRAND = 1000, MINRAND = 1, SIZE = 60; int main() { int values[SIZE] = {0}; unsigned seed = time(0); srand(seed); for (int i = 0; i < SIZE; i++) values[i] = rand() % (MAXRAND - MINRAND) + MINRAND; cout << "The unsorted values are "; showArray(values, SIZE); selectionSort(values, SIZE); cout << "The sorted values are "; showArray(values, SIZE); return 0; } void selectionSort(int array[], int size) { int startScan, minIndex, minValue; for (startScan = 0; startScan < (size - 1); startScan++) { minIndex = startScan; minValue = array[startScan]; for(int index = startScan + 1; index < size; index++) { if (array[index] < minValue) { minValue = array[index]; minIndex = index; } } array[minIndex] = array[startScan]; array[startScan] = minValue; } } void showArray(int array[], int size) { for (int count = 0; count < size; count++) cout << array[count] << " "; cout << endl; } ==================== LINEAR SEARCH================ // This program demonstrates the searchList function, which // performs a linear search on an integer array. #include  using namespace std; // Function prototype int searchList(int [], int, int); const int SIZE = 5; int main() { int tests[SIZE] = {87, 75, 98, 100, 82}; int results; // Search the array for 100. results = searchList(tests, SIZE, 100); // If searchList returned -1, then 100 was not found. if (results == -1) cout << "Your search term as not found. "; else { // Otherwise results contains the subscript of // the first 100 in the array. cout << "Your search element was found in array index "; cout << results << endl; } return 0; } int searchList(int list[], int numElems, int value) { int index = 0; // Used as a subscript to search array int position = -1; // To record position of search value bool found = false; // Flag to indicate if the value was found while (index < numElems && !found) { if (list[index] == value) // If the value is found { found = true; // Set the flag position = index; // Record the value's subscript } index++; // Go to the next element } return position; // Return the position, or -1 } ===============BINARY SEARCH========================== // This program demonstrates the binarySearch function, which // performs a binary search on an integer array. #include  using namespace std; // Function prototype int binarySearch(const int [], int, int); const int SIZE = 20; int main() { // Array with employee IDs sorted in ascending order. int idNums[SIZE] = {101, 142, 147, 189, 199, 207, 222, 234, 289, 296, 310, 319, 388, 394, 417, 429, 447, 521, 536, 600}; int results; // To hold the search results int empID; // To hold an employee ID // Get an employee ID to search for. cout << "Enter the employee ID you wish to search for: "; cin >> empID; // Search for the ID. results = binarySearch(idNums, SIZE, empID); // If results contains -1 the ID was not found. if (results == -1) cout << "That number does not exist in the array. "; else { // Otherwise results contains the subscript of // the specified employee ID in the array. cout << "That ID is found at element " << results; cout << " in the array. "; } return 0; } int binarySearch(const int array[], int size, int value) { int first = 0, // First array element last = size - 1, // Last array element middle, // Mid point of search position = -1; // Position of search value bool found = false; // Flag while (!found && first <= last) { middle = (first + last) / 2; // Calculate mid point if (array[middle] == value) { found = true; position = middle; } else if (array[middle] > value) // If value is in lower half last = middle - 1; else first = middle + 1; // If value is in upper half } return position; } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Management An Organizational Perspective

Authors: Richard T. Watson

1st Edition

0471305340, 978-0471305347

More Books

Students also viewed these Databases questions

Question

What are the characteristics of a fragmented industry?

Answered: 1 week ago

Question

Define Management or What is Management?

Answered: 1 week ago

Question

What do you understand by MBO?

Answered: 1 week ago