Q:

Write C++ program to find maximum and minimum elements in array using recursion

0

Write C++ program to find maximum and minimum elements in array using recursion

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.

#include <iostream>
using namespace std;
#define MAX_SIZE 100
 
// Function declarations
int Findmaxnumber(int array[], int index, int len);
int Findminnumber(int array[], int index, int len);
 
 
int main()
{
    int array[MAX_SIZE], Num, max, min;
    int i;
 
    // Inputting size and elements of array
    cout<<"Enter size of the array: ";
    cin>>Num;
    cout<<"Enter " <<Num <<" elements in array: ";
    for(i=0; i<Num; i++)
    {
       cin>>array[i];
    }
 
    max = Findmaxnumber(array, 0, Num);
    min = Findminnumber(array, 0, Num);
 
    cout<<"Minimum element in array: "<<min<<endl;
    cout<<"Maximum element in array: "<<max<<endl;
 
    return 0;
}
 
 
 //Recursive function to find maximum element in the given array.
int Findmaxnumber(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 = Findmaxnumber(array, index + 1, len);
 
    if(array[index] > max)
        return array[index];
    else
        return max;
}
 
//Recursive function to find minimum element in the array
int Findminnumber(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 = Findminnumber(array, index + 1, len);
 
    if(array[index] < min)
        return array[index];
    else
        return min;
}

Result:

Enter size of the array: 5

Enter 5 elements in array: 10

20

30

40

50

Minimum element in array: 10

Maximum element in array: 50

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
<< Write C++ program to check palindrome number using...