This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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