-2

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??

angie
  • 31
  • 1
  • 1
  • 5

5 Answers5

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();
}
PU.
  • 148
  • 9
  • 2
    Congrats - 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
  • `void main` is a pain. – David Ranieri Aug 20 '14 at 13:53
  • 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
  • 3
    It'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
  1. Consider first element of the array is minimum.
  2. Call the function by passing base address of the array and number of elements.
  3. Check for any other number is less then the minimum value, if yes assign that value to minimum.
  4. 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
  • 2
    you 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