problem regarding minimum an array.

[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

it seems like a homework, what did you try or where is the problem?

the basic idea with recursion, is that you only have to know how to solve one step of the problem, to solve it entirely.

the construction of the recursion algorithm will do the rest.

if you’re given an array, are you able to split the problem on smaller data ? yes.

you can compare the first value of the array, and the remaining part of the array.

this operation is called head-tail splitting (very well known of Caml developpers).

It’s the divide-and-conquer paradigm, as betlista mentionned above.

good luck

this problem solution is below…

``````#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);
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;
}

/* compute minimum element in v...v[right],
* both inclusive. */
int min_rec(int v[], int left, int right)
{
int mid, min1, min2;
if (left==right) {
// single element => minimum
return v;
}
mid = (left+right)/2;
min1 = min_rec(v, left, mid);
min2 = min_rec(v, mid+1, right);

return (min1 < min2) ? min1 : min2;
}
``````

i hope that you can understand easily this problem…

-1 I do not like these “copy the code” solutions, if questioner have to find something, just provide some hints instead of complete solution…

//