In: Computer Science
C++
Please read the question carefully and make sure that the function prototypes given are used correctly for both parts. This is one whole programming assignment so please make sure that it;s answered entirely not just one part.
The output example is provided at the end of the question.
First , write a program to create an array and fill it up with 20 randomly generated integers between 0 to 10 and output the array.
Part 1:
Write a function to implement following function prototype:
void search( int array [], int length, int
key);
Precondition: Function accepts an integer array, the size of the
array, and a key to search.
Post condition: 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.
Part 2:
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!!
void selectionSort( int list[], int
length);
Output:
1 7 0 5 0 6 3 8 8 9
8 0 9 8 0 1 8 9 8 7
0 was found! the number of occurence 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
#include<iostream>
#include <time.h>
#include <stdlib.h>
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,loc,temp;
for(int i=0;i<length;i++)
{
min=list[i];
loc=i;
for(int j=i+1;j<length;j++)
{
if(min<list[j])
{
min=list[j];
loc=j;
}
}
temp=list[i];
list[i]=list[loc];
list[loc]=temp;
}
cout <<"\nSorted array\n";
for(int i=0;i<length;i++) cout
<<list[i]<<" ";
}
/* Main function */
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]<<" ";
cout << endl;
search(array,20,0);
selectionSort(array,20);
}