-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_linear_search.c
More file actions
47 lines (37 loc) · 1014 Bytes
/
03_linear_search.c
File metadata and controls
47 lines (37 loc) · 1014 Bytes
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
44
45
46
47
#include<stdio.h>
#include<stdlib.h>
int linearSearch(int arr[], int n, int e)
{
int index;
for(index = 0 ; index < n ; index++)
if(arr[index] == e)
return index + 1;
return 32767;
}
int main()
{
int arr[50];
int num_of_elmnt, index;
int srch_elmnt;
int status;
printf("Enter Number of Elements : ");
scanf("%d", &num_of_elmnt);
if((num_of_elmnt < 0) || (num_of_elmnt > 50))
{
printf("Invalid Input");
return 1;
}
for(index = 0 ; index < num_of_elmnt ; index++)
arr[index] = rand() % 10;
printf("Enter Number you want to Search : ");
scanf("%d", &srch_elmnt);
status = linearSearch(arr, num_of_elmnt, srch_elmnt);
if(status != 32767)
printf("\nElement Found at Location %d\n", status);
else
printf("\nNot Found\n");
printf("\nArray\n");
for(index = 0 ; index < num_of_elmnt ; index++)
printf("Element %d : %d\n", index + 1, arr[index]);
return 0;
}