The objective of this lab is to search, sort, & modify a 1D array. This lab covers Ch. 8 and we may be using some functions from Lab 7.
Write your program in your 2010/9/ on Odin. Name it lab9.cpp. You will write a single program that does all of the functions below.
Create an integer array with a size of 25. Call your random() function before your do while loop so that, at the beginning of the program, your array will be filled with random numbers.
Create a menu using a do-while loop and a switch case, similar to your menu in Lab 7.
If option '0' is chosen, display the current array. You may use your display function from Lab 7.
Function prototype: void display(int arr[], int size); or void display(int *arr, int size);
If option '1' is chosen, fill the array with a new set of numbers and display them. Create a random() function that fills your array with unique numbers within range[5, 50]. No duplicate numbers allowed. (Hint: See linear search algorithm in Ch.8 and see how it works.) You may use the same function parameters as your Lab 7 random function.
Reminder: This is the format for the rand() function: rand % (range) + min
Function prototype: void random(int *arr, int size); or void random(int arr[], int size, int min, int max);
If option '2' is chosen, prompt the user for the number that they want to search for and display the array. Call both linear and binary search functions and output the index position that both algorithms returned. Let the user know if the linear search and the binary search found the number. Your search functions do not print anything to the screen. You have your display function for that. (See Ch. 8 for linear and binary search algorithms.)
Function prototype: int linear_search(int *arr, int size, int number);
Function prototype: int binary_search(int *arr, int size, int number);
If option '3' is chosen, print the current array, the ascending bubble sorted array, descending bubble sorted array, ascending selection sorted array, & descending selection sorted array in this order. Your sort functions do not print anything to the screen. You have your display function for that. (See Ch. 8 for bubble and selection sort algorithms. See Gordon's bubble sort algorithm.)
Function Prototype: void bubblesort_Asc(int *arr, int size);
Function Prototype: void selectsort_Asc(int *arr, int size);
Function Prototype: void bubblesort_Desc(int *arr, int size);
Function Prototype: void selectsort_Desc(int *arr, int size);
If option '4' is chosen, print the correct answers to the following statements:
cout << "Search & Sort Facts:\n";
cout << "1. Linear Search [does | does not] require the data to be ordered.\n";
cout << "2. Linear Search has a worst case time complextiy of [O(n) | O(n^2) | O(log n)].\n";
cout << "3. Binary Search [does | does not] require the data to be ordered.\n";
cout << "4. Binary Search has a worst case time complextiy of [O(n) | O(n^2) | O(log n)].\n";
cout << "5. [Linear | Binary] Search is better than [Linear | Binary] Search when the data is not ordered.\n";
cout << "6. [Linear | Binary] Search is better than [Linear | Binary] Search when the data is ordered.\n";
cout << "7. The Bubble Sort is [efficient | inefficient] for large arrays.\n";
cout << "8. The [Bubble | Selection] Sort moves items by one element at a time.\n";
cout << "9. The [Bubble | Selection] Sort moves items immediately to their final position in the array.\n";
Answers
1. does not 2. O(n) 3. does 4. O(log n) 5. Linear, Binary 6. Binary, Linear 7. inefficient 8. Bubble 9. Selection
You will have to make a decision which search algorithm you want to use and if you need to sort the array before searching. You may write a function for each of the operations below.
Note: It is optional to create a case '9' that allows you to toggle doing a linear or a binary search for cases '5'-'8', if you wish to implement the tasks below using both searches. When option '9' is selected, your menu will update so that it says "Binary Search for 5-8" or "Linear Search for 5-8". You can do this by adding the following codes to the right parts of your program:
//If-Else Shorthand: (condition) ? True case:False case cout << "9. " << ((flag) ? "Linear":"Binary") <<" Search for 5-8\n"; //flag's default value is set to True //Toggle case '9': flag = !flag; //Makes False to True & True to False when '9' is selected break;
If option '5' is selected, display the current array, ask the user what number they want to replace and what number to replace it with, and display the new array. The number they want to replace must exist in the array. Also, the number they want to enter in the array must be unique.
If option '6' is selected, display the current array, ask the user for two numbers to swap, and display the new array. The numbers they entered must exist in the array. The numbers they entered can't be the same.
If option '7' is selected, display the current array, ask the user for a number to be inserted and the index where it will be placed, and display the new array. The number inserted must be unique. The index entered must be a valid index. Starting from the index the user entered, the elements in the array must shift 1 index position more than their previous index. There won't be room for a 26th number in the array, so the element that used to be in index 24 will be gone.
If option '8' is selected, display the current array, ask the user for a number to be deleted, and display the new array. The number to be deleted must exist. Starting from the index of the deleted number, the elements in the array must shift 1 index position less than their previous index. There will be room for a new 25th number, so you must generate a new element in index 24. This new number must be unique.
If option 'a' is chosen, display the current array and display the newly arranged array. You must first separate the evens and the odds of the array. Then, you must arrange the evens in ascending order and the odds in descending order. Finally, put them back into a single array, with the ascending evens in the front and the descending odds in the back. (You may create a function for this or you can do it in your main() function.)
Try it on your own before you take a peek at a possible solution below.
Possible Psuedocode for Ascending Even & Descending Odd
void even_odd(int *arr, int size) { //Create int array even[size], int array odd[size] //Set even_idx to 0, odd_idx to 0, tmp_idx to 0 //FOR each idx of arr //Compute remainder as arr[idx] mod 2 //IF 'remainder' is 0 //Set even[even_idx] to arr[idx] //INCREMENT even_idx //ELSE //Set odd[odd_idx] to arr[idx] //INCREMENT odd_idx //END IF //END FOR //CALL bubblesort_asc with even, even_idx //CALL bubblesort_desc with odd, odd_idx //FOR each idx of even //Set arr[tmp_idx] to even[idx] //INCREMENT tmp_idx //END FOR //FOR each idx of odd //Set arr[tmp_idx] to odd[idx] //INCREMENT tmp_idx //END FOR }
This lab is out of 10 points and it's possible to get more than 100%. List of what was checked: