Hey and hi,
Task 1:
Write a program that randomly generates 1000 integers, then, generate an integer Y. Determine whether there is a match between Y and any of the 1000 integers. If a match has been found, write an appropriate output. Record how much time your program can be executed. Analyze your algorithm completely using big-0 notation.
This is my code, (It is just sample) #include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int main()
{
int y;
cout<<"Please insert any number";
cin>>y;
{srand((unsigned)time(0));
int random_integer[10];
for(int index=0; index<100; index++){
random_integer[index] = (rand()%10)+1;
cout << random_integer[index] << endl;
{if (y==random_integer[index])
cout<<"Congrats!";}
int x;
cin>>x;
}}
}
p/s: I cannot generate it automatically. I have to press the number one by one. What can I do? Can you give me sample?
#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;
}
this works now
redone for 1000 integers instead of 10
execution time is active