Finding the max value in a vector

I'm currently teaching myself C++ using Stroustrup's Programming book. Everything has been going well, until now. I'm on the chapter 4 drill, part 6 and have hit a wall.

I need to find the largest value in a vector. I've seen you can use max_element, but would prefer to do it the more basic way first. This is what I have so far, but it doesn't work. Any suggestions?

#include "std_lib_fac.h"
int main()

{ vector<double>vec;
double dig;
while (cin >> dig)
vec.push_back(dig);

double max = 0;

for (double x = 0; x<dig; ++x);
{
if (vec[dig]>max)
max = vec[dig];
}
cout << "The biggest number is: " << max << endl;

return 0;
}
if (vec[dig]>max)
You can't use a double as index - use size_t instead.
Normal procedure is to assume the first element of the vector as max and start the loop with the second element.
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
#include <iostream>
#include <vector>
#include <limits>
#include <algorithm>

int main()
{
    std::vector<double> vec ;

    double dig ;
    while( std::cin >> dig ) vec.push_back(dig) ;

    std::cout << "vec: [ " ;
    // http://www.stroustrup.com/C++11FAQ.html#for
    for( double d : vec ) std::cout << d << ' ' ;
    std::cout << "]\n" ;
    

    if( !vec.empty() )
    {
        double max_value = vec.front() ;
        for( std::size_t i = 1 ; i < vec.size() ; ++i ) if( vec[i] > max_value ) max_value = vec[i] ;
        std::cout << "The biggest number is: " << max_value << '\n' ;
    }

    if( !vec.empty() )
    {
        // http://en.cppreference.com/w/cpp/types/numeric_limits/lowest
        double max_value = std::numeric_limits<double>::lowest() ;
        for( double d : vec ) if( d > max_value ) max_value = d ;
        std::cout << "The biggest number is: " << max_value << '\n' ;
    }

    if( !vec.empty() )
    {
        std::cout << "The biggest number is: " << *std::max_element( vec.begin(), vec.end() ) << '\n' ;
    }

    if( !vec.empty() )
    {
        // http://en.cppreference.com/w/cpp/algorithm/nth_element
        // http://www.stroustrup.com/C++11FAQ.html#lambda
        std::nth_element( vec.begin(), vec.begin(), vec.end(), [] ( int a, int b ) { return a > b ; } );
        std::cout << "The biggest number is: " << vec.front() << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/4018f45c88462f66
... and then something entirely, but not quite, different:
1
2
3
4
5
6
7
8
9
10
    if( !vec.empty() )
    {
        std::size_t winner = 0;
        for ( std::size_t pos = 1 ; pos < vec.size() ; ++pos ) {
            if ( vec[winner] < vec[pos] ) {
                winner = pos;
            }
        }
        std::cout << "The biggest number is: " << vec[winner] << '\n' ;
    }
Thanks for the suggestions. I couldn't get the vectors to work, not sure why. So abandoned this approach and went with simple if clauses:

#include "std_lib_fac.h"
int main()
{
double value = 0;
double max = 0;
double min = 0;

cout << "Enter a value: \n";
while (cin >> value )
{
if (min == 0 && max == 0)
{ min = value;
max = value;

cout << min << " is the smallest.\n"
<< max << " is the largest.\n";
}
else
{
if (value < min)
{
min = value;
cout << value << " is the smallest.\n"
<< max << " is the largest.\n";
}
else
{
if (value > max)
{
max = value;
cout << min << " is the smallest.\n"
<< max << " is the largest.\n";
}
else
{
cout << min << " is the lowest.\n"
<< max << " is the highest.\n";
}
}
}
cout << "Enter another value: ";
}
return 0;
}
Topic archived. No new replies allowed.