I need help with the following question, I don't want the whole code but I really need help with this section. (DISPLAY THE NUMBER OF KEY COMPARISON FOR EACH SEARCH). Here is the question.
Method 1A
1A-1. Create one array arr1 of size 1000. Populate it with random numbers in the range from 0 to 2000.
1A-2. Randomly generate 5 numbers and try to search each number in the array.
1A-3. Display the number of key comparison for each search.
1A-4. Display a table like the following:
Target Number # of key comparison
356 (Not Found) 1000
#include<iostream>
#include<ctime>
usingnamespace std;
int main () {
srand(time(NULL));
int arr1[1000];
// fill with random numbers
for (int i = 0; i < 1000; i++) {
arr1[i] = rand() % 2001;
}
int arr2[5];
// 5 more random numbers
for (int i = 0; i < 5; i++) {
arr2[i] = rand() % 2001;
}
int a = 0;
//find matches and output the results
for (int j = 0; j < 5; j++) {
a = 0;
for (int i = 0; i < 1000; i++) {
if (arr2[j] == arr1[i]) {
a++;
}
}
cout << arr2[j] << " Number of matches: " << a << endl;
}
cin.get();
return 0;
}
#include<iostream>
#include<ctime>
usingnamespace std;
int main () {
srand(time(NULL));
int arr1[1000];
// fill with random numbers
for (int i = 0; i < 1000; i++) {
arr1[i] = rand() % 2001;
}
int arr2[5];
// 5 more random numbers
for (int i = 0; i < 5; i++) {
arr2[i] = rand() % 2001;
}
int a = 0;
//find matches and output the results
for (int j = 0; j < 5; j++) {
a = 0;
for (int i = 0; i < 1000; i++) {
a++;
if (arr2[j] == arr1[i]) {
i = 1000;
}
}
if (a < 1000) {
cout << arr2[j] << " Number of comparisons until match found: " << a << endl;
}
else {
cout << arr2[j] << " No match found: " << a << endl;
}
}
cin.get();
return 0;
}