Given an integer array a of size n, write a tail-recursive function with prototype
int f(int a[], int n);
that finds the minimum element of the 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 do not like the use of a static variable.