Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <stdio.h>
void main(void)
{
// linear search
// given a value to find
// search each element in the array
// if found return the value/position
int array[] = {1,2,3,4,5,6,7,8,9,10};
int len = sizeof array / sizeof array[0];
int i, target, position;
target = 10;
position = -1;
for(i = 0; i < len; i++)
{
if(array[i] == target)
position = i;
}
printf("%d is located at position %d\n", target, position);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment