Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Visual Studio, use C++: First, write a program to create an array and fill it

ID: 3880230 • Letter: I

Question

In Visual Studio, use C++:

First, write a program to create an array and fill it with 20 randomly generated integers (range 0- 10) and output the array. (Hint: Check the provided slides for the random number generator code.) Write a function to implement following function prototype: void search(int array[], int length, int key); The function takes as input an integer array, the size of the array, and a key to search. The function outputs the key value and a message stating whether the key exists in the array. If the key exists, the function outputs the number of occurrence of that key. 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 and use the selection sort algorithm provided in the text book. void selectionSort(int list[], int length); Submit a single.cpp file for both questions. You should have a similar output as below.

Explanation / Answer

#include <iostream>
#include <random>
using namespace std;

// function that takes 3 parameters
void search(int array[], int length, int key)
{
// to keep track of occurrences of key in array
int count = 0;
  
// looping through each number to check if it is matching key
for(int i=0; i<length; i++)
{
if (array[i] == key)
count++;
}
  
// if key occurred, then count will be greater than 0
if(count > 0)
{
cout << "Key is in the arary and it occurred for " << count << " times" << endl;
}
  
// else comes here
else
{
cout << "Key is not found in the array" << endl;
}
}

int main() {
int a[20];
  
// GENERATING AND PRINTING array
cout << "Array is ";
for(int i=0; i<20; i++)
{
int x = rand() % (11);
a[i] = x;
cout << a[i] << " ";
}
cout << endl;
  
// calling function twice
search(a, 20, 3);
search(a, 20, 5);
}

/* SAMPLE OUTPUT
Array is 6 10 6 2 1 4 0 6 3 1 8 7 5 3 7 4 9 10 2 0
Key is in the arary and it occurred for 2 times
Key is in the arary and it occurred for 1 times
*/

/*NOTE: One question at a time please -- Chegg Policy*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote