Skip to content

Instantly share code, notes, and snippets.

@sojohnnysaid
Last active January 19, 2021 22:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sojohnnysaid/e676c8271a8585c6b4ed953057f5aae1 to your computer and use it in GitHub Desktop.
Save sojohnnysaid/e676c8271a8585c6b4ed953057f5aae1 to your computer and use it in GitHub Desktop.
#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