Write a C++ program to input integer numbers into a vector named fmax and determine the maximum value entered in the vector and the index number for the maximum. The program should loop until -999 is entered. The maximum value and element number should be determined as vector element values are being input. Display the contents of the vector and the following two messages with the values found.
The vector contains { n, n, n, n, … } where n is the numbers in the vector
The maximum value is: _____
This is element number _____ in the list of numbers
here is what i have so far
#include <iostream>
#include <vector> //Needed to implement vectors
#include <algorithm> //needed to run sort() (line 23)
using namespace std;
int main()
{
vector <int> fmax;
int numEntered;
cout << "Enter 10 Digits: " << endl;
for(int i = 0; i < 10; i++) {
cin >> numEntered;
fmax.push_back(numEntered);
}
sort(fmax.begin(), fmax.end());
cout << "The Maximum value is: " << fmax.at(9) << endl;
cout<<"the vector contains"<< fmax.size()<<" elements.\n";
}
i know i should use a sentinel to end the loop but i do not know how to add that in there everything i have tried so far gave me a error if someone could show me how to do that i would appreciate it.
trying to sort the array is not in the spirit (or letter) of the question, nor is limiting the number of entries.
Below, I've borrowed ne555 code, the loop is terminated by -999 or bad input. A running check is done and the index of the maximum value seen so far is stored, to be used at the end of the input.
#include <iostream>
#include <vector>
usingnamespace std;
int main()
{
vector<int> fmax;
int n = 0;
int i = 0;
int max_index = 0;
while (cin >> n && n != -999)
{
fmax.push_back(n);
if (n > fmax[max_index])
{
max_index = i;
}
++i;
}
cout << "The vector contains { ";
for (auto vm : fmax)
{
cout << vm << ", ";
}
cout << "}\nThe maximum value is " <<fmax[max_index];
cout << "\nThis is element number " << max_index << " in the list of numbers" << endl;
}