Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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);
- }
Add Comment
Please, Sign In to add comment