#include <iostream> // Include the I/O stream library for input and output operations
#include <omp.h> // Include the OpenMP library for parallel programming
#include <climits> // Include the library for limit constants such as INT_MAX and INT_MIN
using namespace std; // Use the standard namespace to avoid prefixing std::
// Function to find the minimum element in the array using OpenMP for parallel processing
int min_reduction(int arr[], int n) {
int min_value = INT_MAX; // Start with the largest possible int
#pragma omp parallel for reduction(min: min_value)
for (int i = 0; i < n; i++) {
if (arr[i] < min_value) {
min_value = arr[i];
}
}
return min_value; // Return the minimum value found
}
// Function to find the maximum element in the array using OpenMP
int max_reduction(int arr[], int n) {
int max_value = INT_MIN; // Start with the smallest possible int
#pragma omp parallel for reduction(max: max_value)
for (int i = 0; i < n; i++) {
if (arr[i] > max_value) {
max_value = arr[i];
}
}
return max_value; // Return the maximum value found
}
// Function to compute the sum of all elements in the array using OpenMP
int sum_reduction(int arr[], int n) {
int sum = 0;
#pragma omp parallel for reduction(+: sum)
for (int i = 0; i < n; i++) {
sum += arr[i];
}
return sum; // Return the total sum of elements
}
// Function to compute the average of the elements in the array using OpenMP
double average_reduction(int arr[], int n) {
int sum = 0;
#pragma omp parallel for reduction(+: sum)
for (int i = 0; i < n; i++) {
sum += arr[i];
}
return static_cast<double>(sum) / n; // Calculate and return the average
}
int main() {
int *arr, n;
cout << "\n enter total no of elements=>";
cin >> n;
arr = new int[n];
cout << "\n enter elements=>";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int min_value = min_reduction(arr, n);
int max_value = max_reduction(arr, n);
int total_sum = sum_reduction(arr, n);
double average = average_reduction(arr, n);
cout << "Minimum value: " << min_value << endl;
cout << "Maximum value: " << max_value << endl;
cout << "Sum: " << total_sum << endl;
cout << "Average: " << average << endl;
delete[] arr; // Free the allocated memory
}