Sequentially searching arrays in C

Hello,I posted a topic on this earlier.Got some tips that helped but need some more assistance.The problem is to create an array of 100 random integers in the range 1 to 200 and then using the sequential search,searches the array 100 times using randomly generated targets in the same range.At the end of the program the number of searches completed, the number of successful searches, the percentage of successful searches and the average number of tests per search is to be displayed.

I already have the array created with 100 random integers and the code is below.I'm having problems with the rest part of the program, ie the successful searches, etc.
Any ideas and hints will be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ARY_SIZE 200

main ()
{
int randoms[100];
int i;
int j;
int range;

for(i = 0; i < 100; i++)
{
  randoms[i] = rand() % 200;
  printf(" %d", randoms[i]);
}
Your random number generation is a hair off.

I slapped the code together.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ARY_SIZE 200

main ()
{
int randoms[100];

srand( (unsigned) time(0) );

for(int i = 0; i < 100; i++)
{
  randoms[i] = rand() % 200 + 1;
  printf(" %d", randoms[i]);
}

int successes = 0;
int rnd_number;
for(int i = 0; i < 100; ++i)
{
  rnd_number = rand() % 200 + 1;

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

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

  }

}

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


Last edited on
Topic archived. No new replies allowed.