Array of random integers in C

I'm working on a problem here.It says to write a program that creates an array of 100 random integers in the range of 1 to 200 and then using the sequential search,searches the array 100 times using randomly generated targets in d same range and then to display the number of searches completed, the number of successful searches, the percentage of successful searches, the average number of tests per search.
I have started working on this but i am stuck with the sequential search part.I have written the code that shows the 100 random integers in the range of 1 to 200 but using the sequential search to search the array 100times is giving me problems.Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
//Local Declarations
int range;
srand(time(NULL));
range = (200 - 1) + 1; 
printf("%d",   rand() % range + 1);
printf(" %d",   rand() % range + 1);
printf(" %d\n", rand() % range + 1); 
  
  
  
  system("PAUSE");	
  return 0;
}
Create an array of 100 random integers:
1
2
3
4
5
6
int randoms[100];

for(int i = 0; i < 100; ++i)
{
  randoms[i] = rand() % range + 1;
}


Sequential search with random #'s:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int successes = 0;
int rnd_number;
for(int i = 0; i < 100; ++i)
{
  rnd_number = rand() % range + 1;

  for(int j = 0; j < 100; ++j)
  {

     if(randoms[i] == rnd_number)
    {
      ++successes;
    }

  }

}


printf("Successes = %d\n", successes);



Edit: I tested it a few times and got between 40 and 60 successes. ( You may get more or less )

Last edited on
I'm not sure what you are saying the problem is, but to me it sounded like a success is finding at least one copy of the number in the array, but yours would count each copy as a success. If that's not what you want (It may be, I'm not sure), you can add a break the after ++successes to stop it from looking further in the array.
thanks for all the replies and post
Topic archived. No new replies allowed.