Please implement the following problem in basic C++ code and include detailed co
ID: 3890493 • Letter: P
Question
Please implement the following problem in basic C++ code and include detailed comments so that I am able to understand the processes for the solution. Thanks in advance.
So far, I have the following code. I am able to search and see if a given number (in this case, 5 and 50) is in the generated array or not. I do not know how to find the number of occurrences and I also do not know how to output the array in descending order using selection sort. Please help.
#include <iostream>
#include "stdlib.h"
#include "time.h"
using namespace std;
#define ASIZE 20
void search(int array[],int length,int key)
{
int loc;
int occurence;
bool found = false;
loc = 0;
occurence = 0;
while (loc < length && !found)
if (array[loc] == key)
found = true;
else
loc++;
if (found)
cout << key << " found!" << endl;
else
cout << key << " not found!" << endl;
}
int main()
{
srand((int)time(NULL));
int array[ASIZE];
for (int i = 0; i < ASIZE; i++)
{
array[i] = rand()%10; // stores the random value (0-10)
}
cout << "Values: ";
for (int i = 0; i < ASIZE; i++)
{
cout << array[i] << " ";
}
cout << endl;
search(array, 20, 5);
search(array, 20, 50);
return 0;
}
First, write a program to create an array and fill it up with 20 randomly generated integers (range 0-10) and output the array. Question: 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. 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 1ist[], int length); You should get the similar output as below Output: CAWINDOWS system32cmd.exe 8 has found the number of occurrence is 4 50 not found 8 8 2 6 0 Press any key to continue . . .Explanation / Answer
#include <iostream>
#include "stdlib.h"
#include "time.h"
using namespace std;
#define ASIZE 20
void selectionSort(int list[],int length) {
for (int i = 0; i < length - 1; i++)
{
int index = i;
for (int j = i + 1; j < length; j++)
if (list[j] > list[index])
index = j;
int smallerNumber = list[index];
list[index] = list[i];
list[i] = smallerNumber;
}
}
void search(int array[],int length,int key)
{
int loc;
int occurence;
bool found = false;
loc = 0;
occurence = 0;
while (loc < length) {
if (array[loc] == key) {
found = true;
occurence++;
}
loc++;
}
if (found)
cout << key << " found! and the number of occurances: "<<occurence << endl;
else
cout << key << " not found!" << endl;
}
int main()
{
srand((int)time(NULL));
int array[ASIZE];
for (int i = 0; i < ASIZE; i++)
{
array[i] = rand()%10; // stores the random value (0-10)
}
cout << "Values: ";
for (int i = 0; i < ASIZE; i++)
{
cout << array[i] << " ";
}
cout << endl;
search(array, 20, 5);
search(array, 20, 50);
selectionSort(array, 20);
cout<<"Array elements are after sorting: "<<endl;
for (int i = 0; i < ASIZE; i++)
{
cout << array[i] << " ";
}
return 0;
}
Output: