fork(1) download
  1. #include <stdio.h>
  2. int binarysearch(int a, int mass[], int n);
  3. int main()
  4. {
  5. int N,mass,S;
  6. printf("N:");
  7. scanf("%d", &N);
  8. printf("Elements\n");
  9. for (int i = 0; i < N; i++)
  10. scanf("%d", &mass[i]);
  11. printf("S: ");
  12. scanf("%d", &S);
  13. int k;
  14. k = binarysearch(S, mass, N);
  15. if (k != -1)
  16. {
  17. printf("The index of the element is %d\n", k);
  18. }
  19. else
  20. printf("notfound\n");
  21. return 0;
  22. }
  23.  
  24. int binarysearch(int S, int mass[], int n)
  25. {
  26. int min, max, mid;
  27. min = 0;
  28. max = n-1;
  29. while (min <= max)
  30. {
  31. mid = (min + max) / 2;
  32. if (S < mass[mid])
  33. max = mid - 1;
  34. else if (S > mass[mid])
  35. min = mid + 1;
  36. else
  37. return mid;
  38. }
  39. return -1;
  40. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.m: In function ‘main’:
prog.m:9:2: error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode
  for (int i = 0; i < N; i++)
  ^~~
prog.m:9:2: note: use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 to compile your code
prog.m:10:19: error: subscripted value is neither array nor pointer nor vector
  scanf("%d", &mass[i]);
                   ^
prog.m:14:22: warning: passing argument 2 of ‘binarysearch’ makes pointer from integer without a cast [-Wint-conversion]
  k = binarysearch(S, mass, N);
                      ^~~~
prog.m:2:5: note: expected ‘int *’ but argument is of type ‘int’
 int binarysearch(int a, int mass[], int n);
     ^~~~~~~~~~~~
stdout
Standard output is empty