fork download
  1. #include <stdio.h>
  2. #include <omp.h>
  3. #define ARRAY_SIZE 1000
  4. #define SEARCH_VALUE 42
  5. int linearSearch(int *arr, int size, int target) {
  6. int result = -1;
  7. #pragma omp parallel for
  8. for (int i = 0; i <size; ++i) {
  9. if (arr[i] == target) {
  10. #pragma omp critical
  11. {
  12. result = i;
  13. }
  14. }
  15. }
  16. return result;
  17. }
  18. int main() {
  19. int array[ARRAY_SIZE];
  20.  
  21. for (int i = 0; i < ARRAY_SIZE; ++i) {
  22. array[i] = i;
  23. }
  24. int position = linearSearch(array, ARRAY_SIZE, SEARCH_VALUE);
  25. if (position != -1) {
  26. printf("Value %d found at position %d.\n", SEARCH_VALUE, position);
  27. } else {
  28. printf("Value %d not found in the array.\n", SEARCH_VALUE);
  29. }
  30. return 0;
  31. }
  32.  
Success #stdin #stdout 0.01s 5276KB
stdin
Standard input is empty
stdout
Value 42 found at position 42.