Question about vectors

Hello. I had this assignment in which the user has to input some numbers which will be stored in a vector and the program has to tell you when you input the smallest / largest number so far.

I came up with this code, which works fine for what I need:

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
int main () {
    double max = 0;
    double min = 0;
    double num = 0;
    int indicatormax = 0;
    int indicatormin = 0;

    vector <double> numbers;
    while (cin >> num) {
        indicatormax = 0;
        indicatormin = 0;
        numbers.push_back(num);

        for (int i = 0; i < numbers.size(); ++i) {
            if (num < numbers[i])
                ++indicatormax;
            if (num > numbers[i])
                ++indicatormin;
        }
        if (indicatormax == 0) {
            max = num;
            cout << "Largest so far: " << max << '\n';
        }
        else if (indicatormin == 0) {
            min = num;
            cout << "Smallest so far: " << min << '\n';
        }
        else {}   
    }
    return 0;
}


Anyway, I think there has to be a simpler way to do it. I mean, is there any way to compare a number with every number stored so far in a vector and if the condition is true for all values, output say, a bool, "true"?

I had the idea of using what I called "indicators" which will remain 0 if the condition is always false, but I'm not sure if that is the optimal way for doing it.

Thanks.
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
#include <iostream>
#include <vector>

int main() {

    std::vector<double> numbers ;
    double max_so_far = -999999999999999999999999999999999999999999999999999999.9 ;
    double min_so_far = +999999999999999999999999999999999999999999999999999999.9 ;

    double num ;
    while( std::cout << "next number? " && std::cin >> num ) {

        numbers.push_back(num) ;

        if( num > max_so_far ) {

            max_so_far = num ;
            std::cout << "Largest so far: " << max_so_far << '\n' ;
        }

        if( num < min_so_far ) {

            min_so_far = num ;
            std::cout << "Smallest so far: " << min_so_far << '\n';
        }
    }
}
Last edited on
Didn't think of this solution, but again, is there any way to compare a value with all the values inside a vector and output say a 'true' boolean if the condition fits for every 'x' inside the vector?
The algorithms library has std::all_of, std::any_of and std::none_of which can do this.
http://en.cppreference.com/w/cpp/algorithm/all_any_none_of

This may be a little beyond you at the moment; the implementation of these algorithms iterate through the sequence, testing the condition against each element ( like you had done in your code).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
     const std::vector<int> seq = { 2, 16, 8, 56, 64, 66, 20, 12, 18 } ;
     std::cout << std::boolalpha ;

     const auto is_even = [] ( int i ) { return i%2 == 0 ; };
     std::cout << "all even numbers? " << std::all_of( seq.begin(), seq.end(), is_even ) << '\n' ;

     const auto is_divisible_by_7 = [] ( int i ) { return i%7 == 0 ; };
     std::cout << "at least one divisible by 7? " << std::any_of( seq.begin(), seq.end(), is_divisible_by_7 ) << '\n' ;

     const auto greater_than_100 = [] ( int i ) { return i > 100 ; };
     std::cout << "none greater than 100? " << std::none_of( seq.begin(), seq.end(), greater_than_100 ) << '\n' ;
}

http://coliru.stacked-crooked.com/a/87f89b55fea7f593
Topic archived. No new replies allowed.