-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear-search.c
More file actions
45 lines (37 loc) · 745 Bytes
/
Copy pathlinear-search.c
File metadata and controls
45 lines (37 loc) · 745 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
#include <stdio.h>
int linear_search(int array[], int size, int key)
{
for (int i = 0; i < size; i++)
{
if (array[i] == key)
{
return i;
}
}
return -1;
}
int main()
{
int size;
printf("Enter size of the array: ");
scanf("%d", &size);
int array[size];
printf("Enter elements of the array: ");
for (int i = 0; i < size; i++)
{
scanf("%d", &array[i]);
}
int key;
printf("Enter the element to be searched: ");
scanf("%d", &key);
int index = linear_search(array, size, key);
if (index >= 0)
{
printf("%d found at index %d\n", key, index);
}
else
{
printf("Element not found");
}
return 0;
}