How can I find the highest/lowest variable in a vector?

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)


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;
}
_______________________________________________

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?

Thank you
Last edited on
To put your code in tags, just place this text around it: [co de] [/code]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#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)


	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;
}


Now regarding your question:
To sort your vector you can use an STL algorithm:
http://cplusplus.com/reference/algorithm/sort/
1
2
3
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>
Last edited on
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

http://www.cplusplus.com/reference/stl/vector/
http://www.cplusplus.com/reference/stl/vector/operator[]/
Last edited on
Ah ha, I never noticed those ones. That would work better.
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.
You can sort a vector in exactly the same way you sort an array.

If the vector is sorted, Find_Lowest and Find_Highest are trivial. They're the two elements on either end.

Pseudo code for finding the smallest element (in an unsorted vector):

1
2
3
4
5
6
7
lowest_value = vector[0]

for i = 1 to vector.size()-1
     if vector[i] < lowest_value
          lowest_value = vector[i]

return lowest_value


Finding the highest element is quite similar.
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
                           ;                
                }
	}

Last edited on
Topic archived. No new replies allowed.