Hi, I'm trying to determine the lowest and highest variable of a vector. In C++.
This is my code so far: I'm sorry but I don't know how to post it in a block, so hopefully it's not too hard to read.
______________________________________________
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
int Find_Lowest();
int Find_Highest();
int main ()
{
int intInput=0; // value inputed may be in decimal places
int Size,
count=1,
Lowest=0,
Highest=0;
vector<int> Vector(0);
while (intInput != -1)
{
cout << "Enter result '"<< count <<"' (or -1 if no more result): ";
cin >> intInput;
if (intInput == -1)
break;
Vector.push_back(intInput) ;
count++;
}
Size = Vector.size();
cout << endl;
if (Vector.size() == 0) //No valid input!
return 0; //will end program ( just in case user's first input is -1)
int Find_Lowest(Lowest);
int Find_Highest(Highest);
cout << endl <<"The Lowest of the results is " << Lowest << endl;
cout << "The Highest of the results is " << Highest << endl;
return 0;
}
int Find_Lowest()
{
int Lowest=0;
return Lowest;
}
int Find_Highest()
{
int Highest=0;
return Highest;
}
_______________________________________________
So basically, the program asks the user to input varius results. The results are then stored into a vector.
I was thinking of sorting the vector from lowest to highest, then telling whichever function that the lowest is the first value in the vector, and the highest is the last value. Would that work, if i sorted the vector inside the main function?
I was wondering if there was an easier way to determine the highest and lowest value in the vector?
#include <iostream>
#include <vector>
#include <iomanip>
usingnamespace std;
int Find_Lowest();
int Find_Highest();
int main ()
{
int intInput=0; // value inputed may be in decimal places
int Size,
count=1,
Lowest=0,
Highest=0;
vector<int> Vector(0);
while (intInput != -1)
{
cout << "Enter result '"<< count <<"' (or -1 if no more result): ";
cin >> intInput;
if (intInput == -1)
break;
Vector.push_back(intInput) ;
count++;
}
Size = Vector.size();
cout << endl;
if (Vector.size() == 0) //No valid input!
return 0; //will end program ( just in case user's first input is -1)
cout << "What the vector looks like:\n";
for (count = 0; count < Size; count++)
{
cout << Vector[count] << " ";
}
int Find_Lowest(Lowest);
int Find_Highest(Highest);
cout << endl <<"The Lowest of the results is " << Lowest << endl;
cout << "The Highest of the results is " << Highest << endl;
return 0;
}
int Find_Lowest()
{
int Lowest=0;
return Lowest;
}
int Find_Highest()
{
int Highest=0;
return Highest;
}
std::sort (Vector.begin(), Vector.end());
int max = Vector.at(Vector.size()-1);
int min = Vector.at(Vector.begin);
. Be sure to include #include <algorithm>
If you just want to return the max, this is easy to do in a function:
1 2 3 4 5 6 7 8 9
int Find_Highest(vector<int> &Vector)
{
std::vector<int>::iterator it;
int max=std::numeric_limits<int>::min();
for (it = Vector.begin(); it < Vector.end(); ++it)
if (*it > max) max = *it;
return max;
}
If you want to support more than int with a generic function:
1 2 3 4 5 6 7 8 9 10
template <class T>
T Find_Lowest(vector<T> &Vector)
{
vector<T>::iterator it;
int min=std::numeric_limits<T>::max();
for (it = Vector.begin(); it < Vector.end(); ++it)
if (*it < min) min = *it;
return T;
}
I chose the minimum integer allowed for the initial value of max. For this you would use #include <limits>
First you'll need to add an argument (probably a reference to the vector you want to search) to the Find_* functions otherwise you won't be able to traverse the vector.
int Find_Lowest(vector<int> &v);
call with: Lowest = Find_Lowest(Vector);
Just look at the reference here for vector to figure out how to loop through the vector values
ok, thank you.
ANd one more question, how do I get my program to accept and output values with decimal places?
For example if i input 84.66, the screen just continues on and on, not letting me put in another value.
So I've looked at the links and Stewbond's examples, but I can't seem to get my program to work. This is too confusing for me.
So far I learnt to sort arrays with the linear and bubble sorts. Is it pretty similar with vectors? I just want to sort the vector inside my main function and then tell my Find_Lowest and Find_Highest functions how to find the values.
ok, thank you.
ANd one more question, how do I get my program to accept and output values with decimal places?
For example if i input 84.66, the screen just continues on and on, not letting me put in another value.
Since you're using a vector of int values, what would you gain by being able to input a value with a fractional part?
You could add some error handling.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
while (intInput != -1)
{
cout << "Enter result '"<< Vector.size()+1 <<"' (or -1 if no more result): ";
if ( (cin >> intInput) && intInput != -1 )
{
Vector.push_back(intInput) ;
// You don't need to update count here.
// we have Vector.size(), remember?
}
else // input operation failed
{
cerr << "Invalid input!\n" ;
cin.clear() ; // reset state of cin
while ( cin.get() != '\n' ) // remove unwanted stuff from stream
;
}
}