Question: Write a c++ program to: 1) populate an array with randomly generated integers, 2) print the array, 3) search for a specified number and output

Write a c++ program to:

1) populate an array with randomly generated integers,

2) print the array,

3) search for a specified number and output whether that number is found in the array,

4) sort the array in descending order, and

5) print the array again now that it is sorted.

Please implement your program using at least the following three functions. The required functions are prototyped below:

bool search(int arr[], int length, int key);

void selectionSort(int list[], int length);

void printArr(int arr[], int length);

Part 1:

Plan and write the main() function.

Declare an array that will store twenty integers.

Declare an integer that will be the search key. The program can prompt the user to enter a number to search. Alternatively, a chosen number may be hard-coded in the .cpp file.

Loop through the array to populate it with randomly generated integers. The code needed to generate random numbers is provided in the example slides.

Call the printArr function to output the elements of the array.

Call the search function to search for a specified search key. The search function returns a Boolean, so use that return value to output a message about whether the search key was found.

Call the selectionSort

Call the printArr function to output the elements of the array, which are now sorted.

Part 2:

Plan and write the user-defined functions.

Implement the search The array, the number of elements in the array, and the search key are passed to the search function as parameters. The search function iterates through the array elements and returns true if an array element matches the search key. If no match is found after looking at every element, false is returned.

Implement the selectionSort function to sort the array in descending The array and the number of elementsin the array are passed to the selectionSort function as parameters. The code needed for selection sort is provided in the textbook. You may use the textbook provided code, but please ensure that your version sorts the array in descending order (NOT in ascending order).

Implement the printArr The array and the number of elements in the array are passed to the printArrfunction as parameters. The printArr function iterates through the array elements and outputs each element to screen.

Testing, Commenting, and Submission:

Please test your code using several different search keys. The array elements should be randomly generated, so your program will not exactly match the sample output.

Please write a comment block above each user-defined function implementation (search, selectionSort, and printArr) with a short description of what the function does.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!