0

I'm trying to write a program that creates and fills a vector with int values, then searches through it and returns the minimum value, recursively. I have the code written out and building, but it returns a weirdly large value for minimum every time- I have a feeling it's not properly assigning the smallest value to int minimum, but I'm not sure. Any thoughts?

#include <iostream>
#include <conio.h>
#include <vector>

using namespace std;

int vectorSize;
int minimum;
int result = -1;
int start;
int ending;
int answer;
int test;

int recursiveMinimum(vector<int>, int, int);

void main() {
    cout << "How many values do you want your vector to be? ";
    cin >> vectorSize;
    cout << endl;

    vector<int> searchVector(vectorSize);

    start = 0;
    ending = searchVector.size() - 1;

    for (int i = 0; i < vectorSize; i++) {
        cout << "Enter value for position " << i << " " << endl;
        cin >> searchVector[i];
    }

    for (int x = 0; x < vectorSize; x++) {
        cout << searchVector[x] << " ";
    }

    int answer = recursiveMinimum(searchVector, start, ending);
    cout << "The smallest value in the vector is: " << answer;

    _getch();
}

int recursiveMinimum(vector<int> searchVector, int start, int end) {
    if (start < end) {
        if (searchVector[start] < minimum) {
            minimum = searchVector[start]; //this part seems to not work
        }

        start++;
        recursiveMinimum(searchVector, start, end);
    }
    else {
        return minimum;
    }
}
`
nerdsley
  • 223
  • 2
  • 11
  • recursion isn't required to find the minimum value in a vector. You can iterate through (an unsorted vector) and find it in O(N), and if it's sorted you can do it in O(1). – Nicolas Holthaus Feb 02 '17 at 20:17
  • 3
    It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: **[How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver Feb 02 '17 at 20:17
  • Also you really should be passing the vector by reference to avoid copies. – NathanOliver Feb 02 '17 at 20:20
  • Possible duplicate of [Find the minimum number in an array with recursion?](http://stackoverflow.com/questions/1735550/find-the-minimum-number-in-an-array-with-recursion) – Nicolas Holthaus Feb 02 '17 at 20:22

2 Answers2

2

Your minimum variable is not initialised, which leads to undefined behaviour. It should be set to the first value in the vector:

minimum = searchVector[0];
int answer = recursiveMinimum(searchVector, start, ending);

Additionally, ending is off by one, which makes it pick 6 as the smallest value out of [6, 9, 8, 4].

So, ultimately, your code should look like this:

minimum = searchVector[0];
int answer = recursiveMinimum(searchVector, start, ending + 1); // note the + 1

While irrelevant to the question, I advise you to use a tail call in recursiveMinimum, as explained here:

start++;
return recursiveMinimum(searchVector, start, end);
Community
  • 1
  • 1
Emily
  • 1,030
  • 1
  • 12
  • 20
0

The main issue is that you do not initialise minimum. Hence, comparison searchVector[start] < minimum might never become true, and minimum remains uninitialized.

As a quick fix, write int minimum = MAX_INT; instead of int minimum;. MAX_INT is the maximum positive integer value (defined in limits.h). So the values in your array will never be greater that this value, and your minimum search loop will work (unless there are other issues; but for that, please consult the debugger :-) )

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58