Question: C++ Coding Help! Code a program with functions to sequentialSearch, binarySearch and sort an array with 2 different algorithms, along with a getSize function, print
C++ Coding Help!
Code a program with functions to sequentialSearch, binarySearch and sort an array with 2 different algorithms, along with a getSize function, print menu function, a printTheArray function, fillWithRandom function and a dispatch function. Also set up a 100 position integer array and an integer variable, called size, globally (before all methods are coded). Then the array and size do not have to be passed to any of these functions. (The alternative approach would be to define the array in the dispatch method after reading in a size. Then array name and size would be passed to all methods.) The menu should look like the following:
0. Exit
Get the size needed for todays use of the array.
Fill an array with random numbers from 1-100. Do not use srand command so all of the answers will be the same for ease of grading.
Print the array with position numbers.
Sort the array in ascending sequence
Sort the array in descending sequence use a different sort algorithm from step 4.
Sequential search of the array for a target target should be passed to method and the method returns the found location or -1.
Binary search of the array for a target target should be passed to method and the method returns the found location or -1. Remember that the array must be sorted before calling the binary search function.
Run:
The program should start with a request for a size from the user. (Type in 15.) Run the lab in this order:
Ask the user for the size of the array (1)
Fill the array with random values (2)
Print the array (3) (List the position number and the contents of the array position)
Sequentially search the array for a number in the array and then one not in the array, printing the appropriate messages of Found in position ____ and Not Found (6, 6)
Sort the array into ascending sequence (4)
Print the array (3)
Do a binary search of the array for a number in the array and then for a value not in the array, printing the appropriate messages of Found in position ____ and Not Found (7, 7)
Sort the array into descending sequence (5) (Use a different sort algorithm from code 4)
Finally, print the array again (3)
The main method will look like this: (everything else will be done in other functions)
int main ()
{
printMenu();
cout << Type in a choice << endl;
cin >> choice;
while (choice != 0)
{
#include
using namespace std;
int readArraySize(void); void printArray(int numbers[], int size); int linearSearch(int numbers[], int size, int element); void sortAscArray(int numbers[], int size); void sortDescArray(int numbers[], int size); int binarySearch(int numbers[], int size, int element); void printMenu(void); void swap(int *number1, int *number2);
int main(void) { int size; //Size of Array int numbers[100]; //Array of Numbers int choice; //Reading Choice whether to exit or enter program int index; //to read array elements int position; //position to retrieve the search element position int element; //search element //Accepting choice of user whether to exit or start program printMenu(); cout << " Enter Choice: " << endl; cin >> choice; if(choice == 0) exit(1); //Program Start performing all operations in a sequence. Otherwise offer them as parts of switch statement. else { size = readArraySize(); //Reading array elements randomly between 1 and 100. for(index = 0; index < size ; ++index) { int data; data = rand()%100; numbers[index] = data; } //Printing Array Elements as read. printArray(numbers, size); //Performing Linear Search by accepting search element from user. cout << " Performing Linear Search "< return 0; } int readArraySize(void) { int size; cout <<"Enter Array Size:"; cin >> size; return size; } void printArray(int numbers[], int size) { int index; for(index = 0; index < size;++index) cout <<"Position "< int currentIndex; int walkerIndex; int smallestIndex; for(currentIndex = 0; currentIndex < size-1; ++currentIndex){ smallestIndex = currentIndex; for(walkerIndex = currentIndex +1; walkerIndex <= size-1;++walkerIndex) if( numbers[smallestIndex] > numbers[walkerIndex]) smallestIndex = walkerIndex; swap( numbers+smallestIndex, numbers+currentIndex); } } void sortDescArray(int numbers[], int size) { int currentIndex; int walkerIndex; int smallestIndex; for(currentIndex = 0; currentIndex < size-1; ++currentIndex){ smallestIndex = currentIndex; for(walkerIndex = currentIndex +1; walkerIndex <= size-1;++walkerIndex) if( numbers[smallestIndex] < numbers[walkerIndex]) smallestIndex = walkerIndex; swap( numbers+smallestIndex, numbers+currentIndex); } } int binarySearch(int numbers[], int size, int element) { int left=0; int right=size-1; int mid; bool isLocated=false; while(!isLocated && left <= right) { mid=(left+right)/2; if(element == numbers[mid] ) isLocated=true; else if(element < numbers[mid]) right = mid - 1; else left = mid + 1; } return isLocated ? mid +1 : -1; } void swap(int* number1, int* number2){ int temp; temp = * number1; *number1 = *number2; *number2 = temp; } void printMenu(void) { cout << " MENU "< Feedback from instructor: Much of the functionality is missing from your program. Should hacve the fullrange of options 0-7. Present these in a meu and allow the user to choose each one separately and multiple times. Did not test seq and binary searches for not found items Used same sort algorithm Thanks! will rate!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
