
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:
#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); |

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 nowThis is a popular solution!
Step by stepSolved in 3 steps

- struct remove_from_front_of_vector { // Function takes no parameters, removes the book at the front of a vector, // and returns nothing. void operator()(const Book& unused) { (/// TO-DO (12) ||||| // // // Write the lines of code to remove the book at the front of "my_vector". // // Remember, attempting to remove an element from an empty data structure is // a logic error. Include code to avoid that. //// END-TO-DO (12) /||, } std::vector& my_vector; };arrow_forwardWrite a function named mean that accepts as a parameter a reference to a vector of real numbers, and returns the arithmetic mean (average) of the integers in the vector as a real number. For example, if the vector passed contains {2.0, 4.5, 6.5, 1.0}, your function should return 3.5. If the vector is empty, return 0.0. Do not modify the vector that is passed in.arrow_forwardPlease help me in this fast. I have to write a C++ function to which a vector is passed as argument and it returns the maximal difference between any two adjacent elements of the vector. Function prototype is given below. int maximalDiff(vector<int>&);arrow_forward
- Write the lines of code to remove the book at the back of "my_vector". Remember, attempting to remove an element from an empty data structure is a logic error. Include code to avoid that.arrow_forwardt. Given a string as a parameter to the function, return a square grid filled with all the characters of the string from left to right. Fill empty spaces in the grid with '_'. Sample Input: stringtogrid Output: {{s, t, r, i}, {n, g, t, o}, {g, r, i, d}, {_, _, _, _}} Complete the function: vector<vector<int> formGrid(string s){}arrow_forwardIn C++,define a vector object and initialize it with 3 values and then define an iterator that points to elements of this object, then ask the user to enter 3 more values and then add them to the previous vector and then print all elements in the vector.arrow_forward
- Write a function named cumulative that accepts as a parameter a reference to a vectorof integers, and modifies it so that each element contains the cumulative sum of the elements up through that index. For example, if the vector passed contains {1, 1, 2, 3, 5}, your function should modify it to store {1, 2, 4, 7, 12}.arrow_forwardSelect all code lines that creates a vector with content {10, 10, 10, 10). You are also given the C-type array, an_array with content (10, 10, 10, 10). std::vector a_vector (an_array, an_array + sizeof(an_array)/sizeof(int)); std::vector a_vector [10, 10, 10, 10); std::vector a_vector (10, 4); std::vector a_vector = {10, 10, 10, 10); Ostd::vector a_vector (4, 10);arrow_forwardIn c++ i have this information Type of Ticket &. Cost Monday. Adult= £25 and Child = £15 Thursday. Adult = £40 and child = £20 I have made a vector to store this information Vectorticket ={“Monday”, “Thursday”}; Vectoradult = {25, 40}; Vectorchildren ={15, 20}; Thr format of the adult and children vectors is in the order such that the first elements in those vectors correspond with the first element in the ticket vector Now what am struggling with, is i want the program to calculate the total cost of the tickets in correspondant with The user’s inputs which will be the quantity of how many tickets the user bought respectively (adult and children) So something like Monday 1 2 So 1 will be quantity of adult ticket and 2 will quantity of children ticket and the total costs of both added togetherarrow_forward
- please help me with codingarrow_forwardComplete the following function that counts the even numbers in a 2D vector of integers. int count_evens(const std::vector<std::vector<int>>& v) { // Add your code... } Please add output screenshot!arrow_forwardComplete the C++ code below#include "std_lib_facilities.h"/*1. Define a function which calculates the outer product of two vectors. The function return is a matrix. */vector<vector<int>> outerProduct(vector<int>& A, vector<int>& B){//implement here}/*2. Define a function which transposes a matrix. */vector<vector<int>> transpose(vector<vector<int>>& A){//implementation}/*3. Define a function which calculates the multiplication of a matrix and it's transpose. */vector<vector<int>> product(vector<vector<int>>& A){// implementation}/*4. Define a print out function that will print out a matrix in the following format:Example: a 3 by 4 matrix should have the print out:1 2 3 42 3 4 53 4 5 6*/void printMatrix(vector<vector<int>>& A){// implementation}int main(){// Define both vector A and vector Bvector<int> A = {1 ,2, 3, 4, 5};vector<int> B = {1, 2, 3};/*Test outerProduct…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





