Home:ALL Converter>Find minimum value in vector

Find minimum value in vector

Ask Time:2022-10-07T07:46:36         Author:Isaiah Vogt

Json Formatter

Below I have attached code for a project that is intended to find the lowest value in a user-inputed vector, return -1 if the vector is empty, and 0 if the vector only has one index. I have run into an issue with the condition in which a vector is empty as the unit test continues to fail the returns_negative_one_for_empty_vector test.

main.cc

#include <iostream>
#include <vector>
#include "minimum.h"

int main() {
  int size;
  std::cout << "How many elements? ";
  std::cin >> size;
  std::vector<double> numbers(size);

  for (int i = 0; i < size; i++) {
    double value;
    std::cout << "Element " << i << ": ";
    std::cin >> value;
    numbers.at(i) = value;
  }
 
  double index;
  index = IndexOfMinimumElement(numbers);

  std::cout << "The minimum value in your vector is at index" << index << std::endl;
}

minimum.cc

#include "minimum.h"
#include <vector>

int IndexOfMinimumElement(std::vector<double> input) {
  int i, min_index;

  double min_ = input.at(0);

  for (int i = 0; i < input.size(); i++) {
    if (input.at(i) < min_) {
      min_index = i;

      return min_index;
    }
    else if (input.size() == 0) {
      return -1;
    }
    else if(input.size() == 1) {
      return 0;
    }
  }
};

minimum.h

#include <vector>

int IndexOfMinimumElement(std::vector<double> input);

Author:Isaiah Vogt,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/73981007/find-minimum-value-in-vector
PaulMcKenzie :

\nfind the lowest value in a user-inputed vector, return -1 if the\nvector is empty, and 0 if the vector only has one index.\n\nInstead of writing raw for loops, this can be accomplished much more easily by using the STL algorithm functions.\nThere are other issues, one being that the vector should be passed by const reference, not by value. Passing the vector by-value incurs an unnecessary copy.\n#include <algorithm>\n#include <vector>\n#include <iostream>\n\nint IndexOfMinimumElement(const std::vector<double>& input) \n{\n if (input.empty()) \n return -1;\n auto ptrMinElement = std::min_element(input.begin(), input.end());\n return std::distance(input.begin(), ptrMinElement);\n}\n\nint main() \n{\n std::cout << IndexOfMinimumElement({ 1.2, 3.4, 0.8, 7.8 }) << std::endl;\n std::cout << IndexOfMinimumElement({}) << std::endl; // empty\n std::cout << IndexOfMinimumElement({3}) << std::endl; // only 1 element\n return 0;\n}\n\nOutput:\n2\n-1\n0\n\nThe relevant functions are std::min_element and std::distance. The std::min_element returns an iterator (similar to a pointer) to the minimum element in the range.\nThe code is written with a clear understanding of what each function does -- it is practically self-documenting. To get the minimum element, you call std::min_element. To get the distance from the first to the found minimum element, you call std::distance with an iterator to the starting position and an iterator to the ending position.\nThe bottom line is this: the STL algorithm functions rarely, if ever, fail when given the proper input parameters. Writing raw for loops will always have a much greater chance of failure, as you have witnessed. Thus the goal is to minimize having to write such for loops.",
2022-10-07T13:47:02
yy