Recursively Find the Minimum Value in a vector - C++ STL

C++ examples for STL:vector

Description

Recursively Find the Minimum Value in a vector

Demo Code

#include <ctime>
#include <iostream>
#include <vector>

int recursiveMinimum(const std::vector<int>&, int, int);

static const int LIMIT = 10;

int main(int argc, const char* argv[]) {
    std::vector<int> n(LIMIT);

    srand(time(0));/*from w  ww.j  a  v  a 2s  .  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 std::vector<int>& n, int start, int end) {
    static int minimum = n[start];

    std::cout << minimum << std::endl;

    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