Table of Contents
Let us look at various input and output scenarios of this −
The method used in the following program is as follows
Find the minimum value
Find the maximum value
Find the minimum value using recursion
Example
Output
Find the minimum using recursion
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++

Aug 31, 2023 pm 07:37 PM
array recursion c

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP array key value flipping: Comparative performance analysis of different methods PHP array key value flipping: Comparative performance analysis of different methods May 03, 2024 pm 09:03 PM

The performance comparison of PHP array key value flipping methods shows that the array_flip() function performs better than the for loop in large arrays (more than 1 million elements) and takes less time. The for loop method of manually flipping key values ​​takes a relatively long time.

A beginner's guide to C++ recursion: Building foundations and developing intuition A beginner's guide to C++ recursion: Building foundations and developing intuition May 01, 2024 pm 05:36 PM

Recursion is a powerful technique that allows a function to call itself to solve a problem. In C++, a recursive function consists of two key elements: the base case (which determines when the recursion stops) and the recursive call (which breaks the problem into smaller sub-problems ). By understanding the basics and practicing practical examples such as factorial calculations, Fibonacci sequences, and binary tree traversals, you can build your recursive intuition and use it in your code with confidence.

C++ function recursion explained: alternatives to recursion C++ function recursion explained: alternatives to recursion May 01, 2024 pm 04:54 PM

Recursion is a technique where a function calls itself, but has the disadvantages of stack overflow and inefficiency. Alternatives include: tail-recursion optimization, where the compiler optimizes recursive calls into loops; iteration, which uses loops instead of recursion; and coroutines, which allow execution to be paused and resumed, simulating recursive behavior.

Detailed explanation of C++ function recursion: tail recursion optimization Detailed explanation of C++ function recursion: tail recursion optimization May 03, 2024 pm 04:42 PM

Recursive definition and optimization: Recursive: A function calls itself internally to solve difficult problems that can be decomposed into smaller sub-problems. Tail recursion: The function performs all calculations before making a recursive call, which can be optimized into a loop. Tail recursion optimization condition: recursive call is the last operation. The recursive call parameters are the same as the original call parameters. Practical example: Calculate factorial: The auxiliary function factorial_helper implements tail recursion optimization, eliminates the call stack, and improves efficiency. Calculate Fibonacci numbers: The tail recursive function fibonacci_helper uses optimization to efficiently calculate Fibonacci numbers.

Application of PHP array grouping function in data sorting Application of PHP array grouping function in data sorting May 04, 2024 pm 01:03 PM

PHP's array_group_by function can group elements in an array based on keys or closure functions, returning an associative array where the key is the group name and the value is an array of elements belonging to the group.

The role of PHP array grouping function in finding duplicate elements The role of PHP array grouping function in finding duplicate elements May 05, 2024 am 09:21 AM

PHP's array_group() function can be used to group an array by a specified key to find duplicate elements. This function works through the following steps: Use key_callback to specify the grouping key. Optionally use value_callback to determine grouping values. Count grouped elements and identify duplicates. Therefore, the array_group() function is very useful for finding and processing duplicate elements.

What are the debugging techniques for recursive calls in Java functions? What are the debugging techniques for recursive calls in Java functions? May 05, 2024 am 10:48 AM

The following techniques are available for debugging recursive functions: Check the stack traceSet debug pointsCheck if the base case is implemented correctlyCount the number of recursive callsVisualize the recursive stack

Can arrays be used as function parameters? Can arrays be used as function parameters? Jun 04, 2024 pm 04:30 PM

Yes, in many programming languages, arrays can be used as function parameters, and the function will perform operations on the data stored in it. For example, the printArray function in C++ can print the elements in an array, while the printArray function in Python can traverse the array and print its elements. Modifications made to the array by these functions are also reflected in the original array in the calling function.

See all articles