Guest User

Untitled

a guest
Mar 22nd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.45 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. void main(void)
  4. {
  5. // linear search
  6. // given a value to find
  7. // search each element in the array
  8. // if found return the value/position
  9.  
  10. int array[] = {1,2,3,4,5,6,7,8,9,10};
  11. int len = sizeof array / sizeof array[0];
  12.  
  13. int i, target, position;
  14.  
  15. target = 10;
  16. position = -1;
  17.  
  18. for(i = 0; i < len; i++)
  19. {
  20. if(array[i] == target)
  21. position = i;
  22. }
  23.  
  24. printf("%d is located at position %d\n", target, position);
  25. }
Add Comment
Please, Sign In to add comment