generated from GauravWalia19/mernboilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathLinearSearch.c
55 lines (45 loc) · 910 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
44
45
46
47
48
49
50
51
52
53
54
55
/**
* PROBLEM: Linear Search in C language
* AUTHOR: GauravWalia19
**/
#include <stdio.h>
#include <stdbool.h>
void linearsearch(int*, int, int);
int main(){
int size;
int findElement;
printf("Enter the size of the array\n");
scanf("%d",&size);
int array[size];
register int i;
for(i=0; i<size; i++){
scanf("%d", &array[i]);
}
printf("Enter the element to search in the array\n");
scanf("%d", &findElement);
linearSearch(array, size, findElement);
}
/**
* this function contains the core logic for linear search algorithm
*
* @param array
* @param size
* @param findElement
*
* @return void
**/
void linearSearch(int *array, int size, int findElement){
register int i;
bool flag = false;
for(i=0; i<size; i++){
if(findElement == array[i]){
flag = true;
break;
}
}
if(flag){
printf("Element found\n");
}else{
printf("No element found\n");
}
}