In: Computer Science
#include <iostream>
using namespace std;
#define MAX_SIZE 100
// Function declarations
int Largestnumber(int array[], int index, int len);
int Smallestnumber(int array[], int index, int len);
int main()
{
//declartion of variables
int array[MAX_SIZE], size, max, min;
int i;
// Inputting size and elements of array
cout<<"Enter size of the array: ";
cin>>size;
cout<<"Enter " <<size <<" elements in array: ";
for(i=0; i<size; i++)
{
cin>>array[i];
}
//calling of functions
max = Largestnumber(array, 0, size);
min = Smallestnumber(array, 0, size);
cout<<"Smallest element in array: "<<min<<endl;
cout<<"Largest element in array: "<<max<<endl;
return 0;
}
//Recursive function to find largest element in the given array.
int Largestnumber(int array[], int index, int len)
{
int max;
if(index >= len-2)
{
if(array[index] > array[index + 1])
return array[index];
else
return array[index + 1];
}
max = Largestnumber(array, index + 1, len);
if(array[index] > max)
return array[index];
else
return max;
}
//Recursive function to find smallest element in the array
int Smallestnumber(int array[], int index, int len)
{
int min;
if(index >= len-2)
{
if(array[index] < array[index + 1])
return array[index];
else
return array[index + 1];
}
min = Smallestnumber(array, index + 1, len);
if(array[index] < min)
return array[index];
else
return min;
}
OUTPUT:
INPUT