国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

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;
}

Output

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

Minimum in the array :-89

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;
}

Output

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

Maximum in the array :-100

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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

PHP Tutorial
1502
276
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.

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.

MySQL can't be installed after downloading MySQL can't be installed after downloading Apr 08, 2025 am 11:24 AM

The main reasons for MySQL installation failure are: 1. Permission issues, you need to run as an administrator or use the sudo command; 2. Dependencies are missing, and you need to install relevant development packages; 3. Port conflicts, you need to close the program that occupies port 3306 or modify the configuration file; 4. The installation package is corrupt, you need to download and verify the integrity; 5. The environment variable is incorrectly configured, and the environment variables must be correctly configured according to the operating system. Solve these problems and carefully check each step to successfully install MySQL.

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

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.

Detailed explanation of C++ function recursion: formulation of recursion termination conditions Detailed explanation of C++ function recursion: formulation of recursion termination conditions May 05, 2024 am 08:33 AM

In C++ function recursion, the recursion termination condition is essential to prevent infinite recursion. The key to developing recursive termination conditions is to: identify stopping points, such as stopping when a specific number is reached; verify small-scale cases, such as factorial stopping when the input is 0; prevent infinite loops and ensure that the condition is independent of the input value.

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.

What are the cross-platform development environments in C? What are the cross-platform development environments in C? Apr 28, 2025 pm 09:24 PM

Cross-platform development in C is recommended to use VisualStudioCode, CLion and QtCreator. 1. VisualStudioCode is lightweight and flexible, suitable for multi-platform development, but the initial configuration is more complicated. 2. CLion integrates CMake, suitable for cross-platform projects, but licenses are expensive. 3.QtCreator supports cross-platform development, with built-in Qt library, but the learning curve is steep.

See all articles