Previous: Hello World Up: Examples
This program finds the minimum element of a statically defined integer array. The value of this minimum element is displayed.
#include <iostream.h>const int N = 8; int A[N] = {3, 4, 5, 1, 2, 5, 9, 6};
int find_min (int i, int j) { if (i==j) return A[i]; else { int small1, small2; par { small1 = find_min (i, (j+i)/2); small2 = find_min ((j+i)/2+1, j); } if (small1<small2) return small1; else return small2; } }
int main() { int min = find_min(0,N-1); cout << "Minimum element is " << min << endl; return 0; }
As explained in Section ,
the minimum is found recursively as the smaller of the minimum
of the first half of the array and the minimum of the second
half of the array. Because the recursive calls operate on
different parts of the array, they are completely independent
and can be composed in parallel.