For an integer array a size n , write a tail-recursive function with a prototype
int f(int a[], int n);
which finds the minimum element of an array.
This is the best I managed to come up with:
int f(int a[], int n) { static int *min; if (min == 0) min = new int(a[n - 1]); else if (*min > a[n - 1]) *min = a[n - 1]; if (n == 1) return *min; else return f(a, n - 1); }
Can it get better? I don't like using a static variable.
source share