find maximum and Minimum elements in the array using recursion in c++/cpp/cplusplus program
input and Output
The smallest Element in array is -40
The Biggest Element in Array is 50
program
#include <iostream>
using namespace std;
int smallElet(int *arr, int n) {
if (n == 0)
return arr[0];
int ans = min(arr[n - 1], smallElet(arr, n - 1));
return ans;
}
int bigElet(int *arr, int n) {
if (n == 0)
return arr[0];
int ans = max(arr[n - 1], bigElet(arr, n - 1));
return ans;
}
int main() {
int arr[] = {1, 2, 3, -40, 50, 10, -2};
int n = sizeof(arr) / sizeof(int);
cout <<"The smallest Element in array is "<< smallElet(arr, n) << endl;
cout <<"The Biggest Element in Array is " <<bigElet(arr, n) << endl;
}
0 Comments