Pages

Saturday, June 29, 2013

RECURSIVE function to find minimum element of an array in C++

/*
QUESTION:
 write a recursive function that finds and returns the minimum element in an array, where the array and its size are given as parameters

*/

//CODE:

#include <iostream>
using namespace std;

int m_find(int,int);
//MAIN STARTS
int main(){
    int size;
 cout<<"ENTER THE SIZE OF ARRAY : ";

    cin>>size;

 cout<<"\n\nENTER THE VALUES IN THE ARRAY ";
    int * arr = new int[size];

 for(int i=0; i < size ; i++){
    cin>>arr[i];
 }
 
 cout<<"\nMinimum value is : "<<m_find(arr,size)<<endl;
 return 0;
}

//MAIN ENDS

int m_find(int *arr,int size)
{
 if (size == 0)
  return arr[0];
 if(arr[size-1] < m_find(arr , size -1))
  return arr[size -1];
 else
  return (m_find(arr , size -1));



}

No comments:

Post a Comment