fork download
  1. #include <stdio.h>
  2. #include <omp.h>
  3.  
  4. #define ARRAY_SIZE 1000
  5.  
  6. int main() {
  7. int array[ARRAY_SIZE];
  8. long long sum = 0;
  9.  
  10. // Initialize the array with some values
  11. for (int i = 0; i < ARRAY_SIZE; i++) {
  12. array[i] = i + 1; // Filling the array with numbers 1 to ARRAY_SIZE
  13. }
  14.  
  15. // Parallel region to calculate the sum
  16. #pragma omp parallel
  17. {
  18. long long local_sum = 0;
  19.  
  20. // Parallel for loop with reduction to avoid race conditions
  21. #pragma omp for reduction(+:sum)
  22. for (int i = 0; i < ARRAY_SIZE; i++) {
  23. local_sum += array[i];
  24. }
  25.  
  26. // Update the total sum
  27. sum += local_sum;
  28. }
  29.  
  30. printf("The sum of the array is: %lld\n", sum);
  31. return 0;
  32. }
  33.  
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
The sum of the array is: 500500