fork download
  1. #include <stdio.h>
  2. #include <omp.h>
  3.  
  4. #define N 1000000 // Size of the array
  5.  
  6. int main() {
  7. int arr[N];
  8. long long sum = 0; // The sum of the array elements
  9.  
  10. // Initialize the array
  11. for (int i = 0; i < N; i++) {
  12. arr[i] = 1; // Set all elements to 1
  13. }
  14.  
  15. // Parallel region with reduction to compute the sum
  16. #pragma omp parallel for reduction(+:sum)
  17. for (int i = 0; i < N; i++) {
  18. sum += arr[i];
  19. }
  20.  
  21. printf("The sum of the array is: %lld\n", sum);
  22.  
  23. return 0;
  24. }
  25.  
Success #stdin #stdout 0.01s 5412KB
stdin
Standard input is empty
stdout
The sum of the array is: 1000000