Write a recursive function recursiveMinimum that takes an integer array and the array size as arguments and returns the smallest element of the array. The function should stop processing and return when it receives an array of one element.

Respuesta :

Answer:

#include <iostream>

#include <iomanip>

#include <cstdlib>

#include <ctime>

using namespace std;

const int RANGE = 1000;

int recMin( const int [], int, int );

int main()

{

const int SIZES = 10;

int arr [ SIZES ];

int small;

srand( time( 0 ) );

for ( int i = 0; i < SIZES; i++ )

arr[ i ] = 1 + rand() % RANGE;

cout << "Array members are:\n";

for ( int j = 0; j < SIZES; j++ )

cout << setw( 5 ) << arr[ j ];

cout << '\n';

small = recMin( arr, 0, SIZES - 1 );

cout << "\nSmallest element is: " << small << endl;

}

int recMin( const int arr[], int lo, int hi )

{

static int small = RANGE;

if ( arr[ lo] < small )

small = arr[ lo ];

return lo == hi ?small : recMin( arr, lo + 1, hi );

}

RELAXING NOICE
Relax