Recursively Find the Minimum Value in an Array - C++ Function

C++ examples for Function:Recursive Function

Description

Recursively Find the Minimum Value in an Array

Demo Code

#include <ctime>
#include <iostream>

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

static const int LIMIT = 10;

int main(int argc, const char *argv[]) {
    int n[LIMIT];

    srand(time(0));//from  w w  w  . j ava 2  s  . com

    for (int i = 0; i < LIMIT; ++i) {
        n[i] = rand() % LIMIT + 1;

        std::cout << n[i] << std::endl;
    }
    std::cout << "\nMinimum : " << recursiveMinimum(n, 0, LIMIT - 1) << std::endl;
    return 0;
}
// Returns the minimum value from an array recursively
int recursiveMinimum(const int n[], int start, int end) {
    static int minimum = n[start];

    if (start == end)
        return minimum;

    if (minimum < n[start + 1]) {
        return recursiveMinimum(n, ++start, end);
    }

    minimum = n[start + 1];
    return recursiveMinimum(n, ++start, end);
}

Result


Related Tutorials