int i = 0; int min = x[i]; while ( i
I've written the iterative form to find the min number of an array. But I'd like to write a function that with recursion. Please help!
int i = 0; int min = x[i]; while ( i
I've written the iterative form to find the min number of an array. But I'd like to write a function that with recursion. Please help!
Because this sounds like homework, here's a hint: The minimum value in an array is either the first element, or the minimum number in the rest of the array, whichever is smaller.
The minimum number of a single-element array is the one element in the array.
The minimum number of an array with size > 1 is the minimum of the first element and the minimum of the rest of the array.
(The minimum number of an empty array is not defined.)
This is not an optimal solution, because you may save the result of the recursive call in an int variable, but I wasn't allowed to in my exercise.
Anyway, this function searches the lowest value in an int array by using recursion. As first it checks how many elements are in the array by checking if the size equals to 1 and then returns the first value as result. If the table is bigger than 1 (and technically also when lower) it compares the last value in the array with the recursive call with the rest of the array.
The recursion stops at the 0-index and then compares that value with the index 1. Then it compares the lowest value of index 0 and 1 with the value from index 3 and so on until it reaches last value.
int minimum(int array[], int size) { if (size == 1) { return array[0]; } else { return (array[size]
Rules in my exercise:
Sounds like homework, but your best bet is something like this:
int main(void) { int size = 2; int test[] = {0,1}; int min = test[0]; findMin(&min, test, size); printf("%d", min); } void findMin(int* min, int* test, int* length); void findMin(int* min, int* test, int* length) { if (length >= 0) { if (*test
Here is a function that will return a pointer to the minimum value:
#include int *recursiveMin(int *x, int n) { if (x == NULL || n == 0) return NULL; if (n == 1) return x; int *t = recursiveMin(x + 1, n - 1); return *x
general rule of recursion is to avoid "small steps" - so "first element compared to rest of the array" is very bad idea. try to process the array in halves, like this:
min( array ) { if size of array == 1 return array[0] else if size of array == 2 return array[0]
for further optimization:
- avoid array copies - consider using quicksort-like signature (array, from, to)
- avoid recursive calls for sizes
Why do you want to do this with recursion? A general rule with recursion is don't use recursion if you can do it inside a simple linear loop.
Try:
int recursive_min(list arr) { return recursive_min(arr); }
Although this doesn't work in imperative languages.
A more serious answer would be, in pseudocode:
func recursive_min( list l ) { head = l[1] tail = l[2end] if l.length==1 return head else return min(head,recursive_min(tail)) }
Although that doesn't work if l is empty.
int findMin(int a[], int len) { // normal version // if (len==1) return a[0]; // return (a[len-1]
#include #include using namespace std; int min(int [], int); void mostrar(int [], int); int main() { int f[] = {4, 9, 0, -1, 5, 8, -3, 6, -4, 0}; int t = sizeof(f)/sizeof(int); cout nul"); } int min(int x[], int p) { if (p == 1) return x[0]; int aux = min(x+1, p-1); return x[0] 1) mostrar(v, k-1); cout
const int = 20; int getmin (int v[], int n); main () { int v[N]; int i,n,min; printf("\n\tType number of elements:\t"); scanf("%d",&n); for (i=0;i0) { if ( v[n-2] > v[n-1] ) { v[n-2]=v[n-1]; } getmin (v,n-1); } return v[n-2]; }