forked from BRUNDA55/ADA-Lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linearsearch.c
43 lines (37 loc) · 895 Bytes
/
linearsearch.c
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
38
39
40
41
42
43
#include <stdio.h>
#include<time.h>
#include<stdlib.h>
FILE *fp3, *fp1, *fp2;
int count=0;
int linear(int *a, int n, int key)
{
int i=0;
while(i<n && a[i] != key)
{ i++; count++;}
if(i<n)
printf("%d found at position %d\n",key,i );
return count;
}
int main()
{
int n, key, i, r;
int size, *a;
printf("Enter the number of times to run the program:\n");
scanf("%d",&n);
fp1 = fopen("best.txt","a");
fp2 = fopen("worst.txt","a");
fp3 = fopen("average.txt","a");
while(n--)
{
printf("Enter the input size:\n");
scanf("%d",&size);
a = (int*)malloc(size*sizeof(int));
srand(time(0));
for(i=0;i<size;i++)
*(a+i) = rand()%10005;
fprintf(fp1, "%d %d\n",size,linear(a,size,*a) );
fprintf(fp2, "%d %d\n",size,linear(a,size,*(a+size-1)) );
fprintf(fp3, "%d %d\n",size,linear(a,size,*(a+rand()%size)) );
}
return 0;
}