Home > Backend Development > C++ > Recursive program to find minimum and maximum elements of array in C++

Recursive program to find minimum and maximum elements of array in C++

WBOY
Release: 2023-08-31 19:37:05
forward
1020 people have browsed it

Recursive program to find minimum and maximum elements of array in C++

We take the integer array Arr[] as input. The goal is to find the largest and smallest elements in an array using a recursive method.

Since we are using recursion, we will iterate through the entire array until we reach length = 1 and then return A[0], which forms the base case. Otherwise, the current element is compared to the current minimum or maximum value and its value is updated recursively for subsequent elements.

Let us look at various input and output scenarios of this −

Input − Arr= {12,67,99,76,32};

Output − Maximum value in the array: 99

Explanation − Among all elements, 99 is the maximum value.

Input − Arr= {1,0,-99,9,3};

Output − Minimum value in the array: - 99

Explanation − Among all elements, -99 is the smallest value.

The method used in the following program is as follows

Find the minimum value

  • Take the array Arr[] as input.

  • The function recforMin(int arr[], int len) accepts an input array and its length, and uses recursion to return the minimum value in the array.

  • Get the integer variable minimum

  • If the current index len is 1, set minimum=arr[0] and return minimum.

  • Otherwise set minimum = arr[len] or the minimum value of recforMin(arr,len-1) and return it.

  • Finally the smallest element will be returned.

  • Print the result in the main function.

Find the maximum value

  • Take the array Arr[] as input.

  • The function recforMax(int ​​arr[], int len) accepts an input array and its length and returns the maximum value in the array using recursion.

  • Get the integer variable maximum.

  • If the current index len is 1, set maximum=arr[0] and return maximum.

  • Otherwise set the maximum value of maximum = arr[len] or recforMax(arr,len-1) and return it.

  • Finally the largest element will be returned.

  • Print the result in the main function.

Find the minimum value using recursion

Example

#include <iostream>
using namespace std;
int recforMin(int arr[], int len){
   int minimum;
   if (len == 1){
      minimum=arr[0];
      return minimum;
   }
   else{
      return minimum=arr[len]<recforMin(arr,len-1)?arr[len]:recforMin(arr,len-1);
   }
}
int main(){
   int Arr[] = {-89,98,76,32,21,35,100};
   int length = sizeof(Arr)/sizeof(Arr[0]);
   cout <<"Minimum in the array :"<<recforMin(Arr, length);
   return 0;
}
Copy after login

Output

If we run the above code, the following output will be generated

Minimum in the array :-89
Copy after login

Find the minimum using recursion

Example

#include <iostream>
using namespace std;
int recforMax(int arr[], int len){
   int maximum;

   if (len == 1){
      maximum=arr[0];
      return maximum;
   }
   else{
      return maximum=arr[len]>recforMax(arr,len-1)?arr[len]:recforMax(arr,len-1);
   }
}
int main(){
   int Arr[] = {-89,98,76,32,21,35,100};
   int length = sizeof(Arr)/sizeof(Arr[0]);
   cout <<"Maximum in the array :"<<recforMax(Arr, length);
   return 0;
}
Copy after login

Output

If we run the above code, the following output will be generated

Maximum in the array :-100
Copy after login

The above is the detailed content of Recursive program to find minimum and maximum elements of array in C++. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template