mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
Added linear search algorithm
This commit is contained in:
parent
eff67c4e5f
commit
d9f536d818
27
searching/LinearSearch.c
Normal file
27
searching/LinearSearch.c
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
include <stdio.h>
|
||||||
|
|
||||||
|
int linearsearch(int *arr, int size, int val){
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < size; i++){
|
||||||
|
if (arr[i] == val)
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void main(){
|
||||||
|
int s,i,v;
|
||||||
|
printf("Enter the size of the array:\n");
|
||||||
|
scanf("%d",&s);
|
||||||
|
|
||||||
|
int a[s];
|
||||||
|
printf("Enter the contents for an array of size %d:\n", s);
|
||||||
|
for (i = 0; i < s; i++) scanf("%d", &a[i]);
|
||||||
|
|
||||||
|
printf("Enter the value to be searched:\n");
|
||||||
|
scanf("%d", &v);
|
||||||
|
if (linearsearch(a, s, v))
|
||||||
|
printf("Value %d is in the array.\n", s);
|
||||||
|
else
|
||||||
|
printf("Value %d is not in the array.\n", s);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user