#include #include #include #define MASTER 0 int main(int argc, char** argv) { MPI_Init(&argc, &argv); int world_rank, world_size; MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); MPI_Comm_size(MPI_COMM_WORLD, &world_size); // Assuming the size of the array is world_size - 1 int n = world_size - 1; int *array; // Master process if (world_rank == MASTER) { array = (int*) malloc(n * sizeof(int)); // Initialize the array with random numbers for (int i = 0; i < n; i++) { array[i] = rand() % 100; // Random numbers between 0 and 99 } // Send each element to the corresponding worker for (int i = 1; i <= n; i++) { MPI_Send(&array[i-1], 1, MPI_INT, i, 0, MPI_COMM_WORLD); } free(array); } // Worker processes else { int value = -1; // Initial placeholder value int received; for (int i = 0; i < n - world_rank + 1; i++) { MPI_Recv(&received, 1, MPI_INT, world_rank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); if (received < value || value == -1) { int temp = value; value = received; received = temp; } if (world_rank < world_size - 1) { MPI_Send(&received, 1, MPI_INT, world_rank + 1, 0, MPI_COMM_WORLD); } } // Final value for this worker printf("Worker %d final value: %d\n", world_rank, value); } MPI_Finalize(); return 0; }