I have to write a C code that finds the smallest value in an array using recursion. I've already done it using the for loop, but recursion is trickier . Can someone help me??
Asked
Active
Viewed 1.5k times
-2
-
8what you tried so far? – Jayesh Bhoi Aug 20 '14 at 12:23
-
3Have you tried anything yet ? Go as always with recursion : solve the degenerate case (one-element array) then work up the general case. – Quentin Aug 20 '14 at 12:23
-
1For large arrays, you could (conceptually) split them in half and consider each half. Here is the recursion! – Basile Starynkevitch Aug 20 '14 at 12:24
-
the user has asked the first question here, and get a down vote!! @angie please post what you've tried and welcom on SO – Engine Aug 20 '14 at 12:25
-
possible duplicate of [Finding the maximum element of an array recursively](http://stackoverflow.com/questions/12285978/finding-the-maximum-element-of-an-array-recursively) – Kabulan0lak Aug 20 '14 at 12:26
-
@Rup i have to do it that way. I have a test in C after 2 weeks, this exercise was in my book and i just can't figure it out. – angie Aug 20 '14 at 12:27
-
2@angie I suggest you that don't see the answers. Please find your solution by a simple example like an array with 1 element, then with 2 elements, then 3 elements, and so on. You may find your answer after three steps. – Nematollah Zarmehi Aug 20 '14 at 12:36
-
You might want to look in here : http://stackoverflow.com/questions/1735550/find-the-minimum-number-in-an-array-with-recursion – Florian Schöffl Aug 20 '14 at 12:38
-
@NematollahZarmehi - too late. Answer already provided and copy/pasted into homework. – Martin James Aug 20 '14 at 13:19
-
@Martin James it's not like that, i need to undertsand that answer, not just copy-paste it – angie Aug 20 '14 at 13:23
-
@angie Great! The bright minds,The bright roads! – Nematollah Zarmehi Aug 20 '14 at 13:27
5 Answers
4
The minimum of a single item array is that single item (base case or the termination condition).
The min of an array is the minimum of [the first item, the minimum from the rest (excluding the first item)]

perreal
- 94,503
- 21
- 155
- 181
2
Here is simple code for finding minimum value using recursion,
int rec(int a[],int n)
{
int min;
if(n==1)
return a[0];
else {
min=rec(a,n-1);
if(min<a[n-1])
return min;
else
return a[n-1];
}
}
void main()
{
int i,j,n,a[20];
printf("enter n :");
scanf("%d",&n);
printf("enter values : ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n%d",rec(a,n));
getch();
}

Nematollah Zarmehi
- 694
- 6
- 14

PU.
- 148
- 9
-
2Congrats - you put more effort into providing a read-made copy-paste straight-to-homework answer than the OP did in writing the question and searching for solutions. – Martin James Aug 20 '14 at 13:24
-
it's not like that, i need to undertsand that answer, not just copy-paste it – angie Aug 20 '14 at 13:32
-
-
I don't understand this: min=rec(a,n-1); I know that a[] is the array, but i still don't get it. – angie Aug 20 '14 at 15:49
-
For this you need to understand execution of recursive function via program stack .. this link may help http://mooreccac.com/kcppdoc/Recursion.htm – PU. Aug 22 '14 at 06:18
1
#include <stdio.h>
int minimum(int *a_s, int *a_e, int candidate){
if(a_s == a_e)
return candidate;
return minimum(a_s+1, a_e, *a_s < candidate ? *a_s : candidate);
}
int main(void){
int array[] = { 1,3,-2,0,-1};
printf("%d ", minimum(array+1, &array[sizeof(array)/sizeof(*array)], *array));
return 0;
}

BLUEPIXY
- 39,699
- 7
- 33
- 70
-
3It's probably not a good idea to do OP's homework. Plus, your code would probably be cryptic to a beginner. – Quentin Aug 20 '14 at 12:34
-
2@Quentin We do not _know_ this is homework nor from a beginner (wink, wink, ;-)). Still, providing an answer that needs work to understand on OP's part looks like a good idea to me in response to such a post. – chux - Reinstate Monica Aug 20 '14 at 21:20
1
After accept answer.
Below is a recursive solution that does not chew up the stack. Estimate max stack depth at O(ln2(n)). Other solutions look like the maximum stack depth is O(n).
int ArrayMin(const int a[], size_t n) {
if (n <= 1) {
if (n < 0) return 0; // Handle degenerate ArrayMin( ,0)
return a[0];
}
size_t nhalf = n / 2;
int left = ArrayMin(a, nhalf);
int right = ArrayMin(&a[nhalf], n - nhalf);
return left < right ? left : right;
}
Answered after 9 hours, we can assume homework due date is past.

chux - Reinstate Monica
- 143,097
- 13
- 135
- 256
0
- Consider first element of the array is minimum.
- Call the function by passing base address of the array and number of elements.
- Check for any other number is less then the minimum value, if yes assign that value to minimum.
- For every iteration increment the array address and decrement the no of elements!
Try this code-
#include<stdio.h>
int min;
int arrfun(int *arr,int n)
{
if(n){
if(*arr < min) // check any no is less than minimum.
min = *arr; // if yes assign it
}
else
return min; // when n becomes 0 it returns the minimum element
arrfun(++arr,--n); // recursive call
}
int main()
{
int arr[]={7,3,9,2,1,6};
min = arr[0]; // Taking first element is minimum
printf("minimum is: %d\n",arrfun(arr,6)); // call the function by passing address and no of elements in array
return 0;
}

Sathish
- 3,740
- 1
- 17
- 28
-
2you forgot a `return` at the end of `arrfun`. Also I have never seen a recursion using a global variable. – mch Aug 20 '14 at 13:12