fork download
  1. #include <stdio.h>
  2. #include <omp.h>
  3.  
  4. #define N 10
  5.  
  6. int main() {
  7. int i, sum = 0;
  8. int array[N];
  9.  
  10. // Initialize the array
  11. for (i = 0; i < N; i++) {
  12. array[i] = i + 1; // Array contains values 1 to N
  13. }
  14.  
  15. // Parallelize the for loop
  16. #pragma omp parallel for reduction(+:sum)
  17. for (i = 0; i < N; i++) {
  18. sum += array[i];
  19. printf("Thread %d processing index %d\n", i);
  20. }
  21.  
  22. printf("Sum of array elements: %d\n", sum);
  23. return 0;
  24. }
  25.  
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
Thread 0 processing index 814404080
Thread 1 processing index 0
Thread 2 processing index 0
Thread 3 processing index 0
Thread 4 processing index 0
Thread 5 processing index 0
Thread 6 processing index 0
Thread 7 processing index 0
Thread 8 processing index 0
Thread 9 processing index 0
Sum of array elements: 55