[P3. MINIMUM IN AN ARRAY, RECURSIVE]
Use RECURSION to find the minimum element in an array of
integers. You CAN NOT USE any form of looping (for, do-while,
while, goto).
The function should have the following signature:
int min_rec(int v[], int left, int right);
Use the following driver program to test your function:
/* DRIVER PROGRAM STARTS */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* compute minimum element in v...v[right],
* both inclusive. */
int min_rec(int v[], int left, int right);
#define MAX 100
int main()
{
int arr[MAX], min;
unsigned int num, i;
/* To generate some random numbers.
* Simple "srand()" seed: just use "time()" */
unsigned int iseed = (unsigned int)time(NULL);
srand (iseed);
printf("Number of Elements? (<= %d) ", MAX);
scanf("%u", &num);
if (num > MAX) {
printf("Error - too many elements\n");
return -1;
}
/* fill random numbers */
for (i = 0; i < num; i++) {
arr[i] = rand();
}
min = min_rec(arr, 0, num-1);
/* Print the result in a readable way */
printf("The minimum of\n");
for (i = 0; i < num; i++) {
printf(" %14d", arr[i]);
if ((i+1)%5 == 0)
printf("\n");
}
printf("\n is %d\n", min);
return 0;
}
/* DRIVER PROGRAM ENDS */
A sample run:
$ ./a.out
Number of Elements? (<= 100) 23
The minimum of
282885870 1628974172 1036121684 763240898 1003630177
2083408542 280132862 278795535 2003340273 1633465384
905799767 684319243 1675961439 1583794681 1644508794
1850912549 1256333464 866289918 1317913195 1156015339
1346464449 858894413 1198070159
is 278795535