guys..I would like to ask for your help..I was given a programming assignment about searching a random number in a array of also random numbers. here's the problem:
write a program that creates an array of 100 random integers in the range from 1 to 200 and, then use a sequential search to search the array 100 times using randomly generated targets in the same range. at the end of the program, display the statistics of the number of searches it has completed, the number of successful searches, the percentage of successful searches and finally l the average number of test per search.
here is my code and I just would like you guys to look through it and give me some comments if there is something wrong with my code. thanks a lot.
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main(){
int array[100];
int key;
int search=0;
int success=0;
double percent;
int average;
srand(time(0));
// Step 1: I first made the array of 100 random integers from 0 to 99//
for(int i=0; i<100; i++){
array[i]= 0 + rand() % 100;
}
// Step 2: generate a random search key from 0 to 199//
for( int loop= 0; loop<100; loop++){
key= 0 + rand() % 200;
//Step 3; find the key using the sequential search algortihm//
for(int j=0; j<100; j++){
if( array[j]==key){
search=search + 1;
success= success + 1;
}
else if(array[j] !=key){
search=search + 1;
success=success;
}
}
}
percent= (success / search) * 10000;
cout<<"The number of searches completed is:"<<search<<endl;
cout<<"The number of successful searches is:"<<success<<endl;
cout<<"The percentage of succesful searches is:"<<percent<<endl;
system("pause");
return 0;
}