Finding the Minimum of a Vector Write a function to return the index of the minimum value in a vector, or -1 if the vector doesn't have any elements. In this problem you will implement the IndexOfMinimumElement function in minimum.cc and then call that function from main.cc. You do not need to edit minimum.h. Index of minimum value with std::vector Complete IndexOfMinimumElement in minimum.cc. This function takes a std::vector containing doubles as input and should return the index of the smallest value in the vector, or -1 if there are no elements. Complete main.cc Your main function asks the user how many elements they want, construct a vector, then prompts the user for each element and fills in the vector with values. Your only task in main is to call the IndexOfMinimumElement function declared in minimum.h, and print out the minimum element's index. Here's an example of how this might look: How many elements? 3 Element 0: 7 Element 1: -4 Element 2: 2 The minimum value in your vector is at index 1   main.cc file   #include   #include       int main() {   // First, we ask the user what size vector they want to create.   int size = 0;   std::cout << "How many elements? ";   std::cin >> size;       // Construct a vector of doubles with the given size.   // This initializes all the elements in the vector to the   // default value of double: 0.   std::vector numbers(size);   for (int i = 0; i < size; i++) {   // In every iteration of this for loop, we prompt the user   // to input the element at index i, then we put value   // into the vector at index i.   double value = 0;   std::cout << "Element " << i << ": ";   std::cin >> value;   numbers.at(i) = value;   }       // ==================== YOUR CODE HERE ====================   // Call the IndexOfMinimumElement function and pass in   // the `numbers` vector as input, and print out the index.   // Don't forget to #include "minimum.h" so you can   // make a call to the IndexOfMinimumElement declared there.   // ========================================================   std::cout << "The minimum value in your vector is at index ";   }   minimum.cc file #include "minimum.h"       int IndexOfMinimumElement(std::vector &input) {   // ==================== YOUR CODE HERE ====================   // Find the index of the smallest element in the input   // vector, and return it. If the input vector is empty,   // return -1.   // ========================================================   return -1;   }   minimum.h file #include       // Returns the index of the smallest element in the vector,   // or returns -1 if the vector is empty.   int IndexOfMinimumElement(std::vector &input);

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

Finding the Minimum of a Vector

Write a function to return the index of the minimum value in a vector, or -1 if the vector doesn't have any elements. In this problem you will implement the IndexOfMinimumElement function in minimum.cc and then call that function from main.cc.

You do not need to edit minimum.h.

Index of minimum value with std::vector

Complete IndexOfMinimumElement in minimum.cc. This function takes a std::vector containing doubles as input and should return the index of the smallest value in the vector, or -1 if there are no elements.

Complete main.cc

Your main function asks the user how many elements they want, construct a vector, then prompts the user for each element and fills in the vector with values.

Your only task in main is to call the IndexOfMinimumElement function declared in minimum.h, and print out the minimum element's index. Here's an example of how this might look:

How many elements? 3 Element 0: 7 Element 1: -4 Element 2: 2 The minimum value in your vector is at index 1
 
main.cc file
 
#include <iostream>
  #include <vector>
   
  int main() {
  // First, we ask the user what size vector they want to create.
  int size = 0;
  std::cout << "How many elements? ";
  std::cin >> size;
   
  // Construct a vector of doubles with the given size.
  // This initializes all the elements in the vector to the
  // default value of double: 0.
  std::vector<double> numbers(size);
  for (int i = 0; i < size; i++) {
  // In every iteration of this for loop, we prompt the user
  // to input the element at index i, then we put value
  // into the vector at index i.
  double value = 0;
  std::cout << "Element " << i << ": ";
  std::cin >> value;
  numbers.at(i) = value;
  }
   
  // ==================== YOUR CODE HERE ====================
  // Call the IndexOfMinimumElement function and pass in
  // the `numbers` vector as input, and print out the index.
  // Don't forget to #include "minimum.h" so you can
  // make a call to the IndexOfMinimumElement declared there.
  // ========================================================
  std::cout << "The minimum value in your vector is at index ";
  }

 

minimum.cc file

#include "minimum.h"
   
  int IndexOfMinimumElement(std::vector<double> &input) {
  // ==================== YOUR CODE HERE ====================
  // Find the index of the smallest element in the input
  // vector, and return it. If the input vector is empty,
  // return -1.
  // ========================================================
  return -1;
  }

 

minimum.h file

#include <vector>
   
  // Returns the index of the smallest element in the vector,
  // or returns -1 if the vector is empty.
  int IndexOfMinimumElement(std::vector<double> &input);
Expert Solution
Step 1

Here's the updated code for minimum.cc:

----------------------------------------------------------------------------------------------------------------------------------

#include "minimum.h"

int IndexOfMinimumElement(std::vector<double> &input) {
    if (input.empty()) {
        return -1; // return -1 if the input vector is empty
    }

    int minIndex = 0;
    double minValue = input[0];

    for (int i = 1; i < input.size(); i++) {
        if (input[i] < minValue) {
            minValue = input[i];
            minIndex = i;
        }
    }

    return minIndex;
}

-----------------------------------------------------------------------------------------------------------------------------------

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education