Question
First, w rite a C++ program to create an array and fill it up with 20 randomly generated integers (range 010) and output the array.
First, write a C++ program to create an array and fill it up with 20 randomly generated integers (range 010) and output the array.
Hint: Check the provided PowerPoint file regarding the random number generator code.
Question 1:
Write a function to implement following function prototype:
void search(int array[], int length, int key);
The function takes input an integer array, the size of the array, and a key to search. The function outputs the key value and a message whether or not the key exists in the array. If the key exists, function outputs the number of occurrence of the key in the array.
Question2:
Write a function to sort the array in Descending order (from largest to smallest) and output the result. You should implement the following function prototype and use the selection sort provided in the book page 545.
void selectionSort(int list[], int length);
Submit a single cpp file for both questions. You should get the similar output as below.
1 7 0 5 0 6 3 8 8 9
8 0 9 8 0 1 8 9 8 7
0 has found! the number of occurrence is 4
50 not found!
9 9 9 8 8 8 8 8 8 7
7 6 5 3 1 1 0 0 0 0
press any key to continue.....
below is my code for this program. i am not getting the correct result for this output.
#include
#include
#include
#include
#include
using namespace std;
void search(int array[], int length, int key);
void selectionSort(int list[], int length);
int main()
{
int array[10];
srand(time(NULL));
// generate random nos
for (int i = 0; i < 20; i++) {
array[i] = rand() % 10;
}
for (int i = 0; i < 20; i++) {
cout << array[i] << "\t";
}
cout << endl;
search(array, 20, 0);
selectionSort(array, 20);
return 0;
}
void search(int array[], int length, int key)
{
int occ = 0;
for (int i = 0; i < length; i++)
if (array[i] == key)
occ++;
if (occ > 0)
cout << key << " was found!the number of occurence is " << occ;
}
void selectionSort(int list[], int length)
{
int min, location, temp;
for (int i = 0; i < length; i++)
{
min = list[i];
location = i;
for (int j = i + 1; j < length; j++)
{
if (min < list[j])
{
min = list[j];
location = j;
}
}
temp = list[i];
list[i] = list[location];
list[location] = temp;
}
cout << " ";
cout << endl;
for (int i = 0; i < length; i++)
cout << list[i] << " ";
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started