Task 2:
Write a program that randomly generates two sets of integers that is, 1000 integers. Determine whether there exists an integer in the first set in such a way that its value is identical with an integer in the second set. Record how much time it will be executed. Analyze your algorithm complexity using big-o notation.
#include <cstdlib>
#include <ctime> // clock() and CLOCKS_PER_SEC
#include <iostream>
usingnamespace std;
int main()
{
clock_t start = clock();
int y;
int index;
int random_integer[1000]; // increased array size to 1000, was 10
int count = 0; // make your counter variables set to 0 to count above
// number was way too high when only "int count;"
// got this # when count was not equal to 0. "4196874"
// 4 million way more than 1000 and not all randoms would = y
cout<<"random_integer is an array of 1000 numbers\n";
cout<<"y is the inserted number\n\n";
cout<<"Please insert a number 1 to 10: ";
cin >> y;
cout << "\n";
srand((unsigned)time(0));
for(index=0; index<=999; index++) { // reset for 1000 elements
random_integer[index] = rand() % 10+1;
if (random_integer[index] == y)
count++;
}
cout << "number of times random_integer = y is " << count << "\n";
clock_t stop = clock();
cout << "execution time is " << (stop-start)/double(CLOCKS_PER_SEC)*1000 << " seconds" << endl;
return 0;
}