Implement a linear search

This commit is contained in:
baz 2024-02-13 02:09:49 +00:00
parent 484280e160
commit 4b10a016ce
1 changed files with 22 additions and 0 deletions

22
linearsearch/main.c Normal file
View File

@ -0,0 +1,22 @@
#include <stdio.h>
int main(int argc, char *argv[])
{
int arr[] = {1,3,8,6,7};
int searchterm = 8;
int length = sizeof(arr) / sizeof(arr[0]);
int result = -1;
for (int i = 0; i < length; i++)
{
if (arr[i] == searchterm)
{
result = i;
break;
}
}
printf("%d", result);
return -1;
}