Boolean Expressions Unnecessarily Long

I need to write a program the inputs 15 numbers and displays only the highest and lowest number. The partial code I included below is the only way I know how to do this. And this would work if I only had to input 3 numbers, but in this case I would have to do this 15 times. I know this is not the most efficient way to write this code, so if anyone knows how to do this better I would appreciate the help. Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include <iostream>
using namespace std;
int main()
{
    int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o;
    cout << "Enter 15 numbers.";
    cin >> a >> b >> c >> d >> e >> f >> g >> h >> i >> j >> k >> l >> m >> n >> o;
    if ((a > b) && (a > c) && (a > d) && (a > e) && (a > f) && (a > g) && (a > h) && (a > i) && (a > j) && (a > k) && (a > l) && (a > m) && (a > n) && (a > o)) || ((a < b) && (a < c) && (a < d) && (a < e) && (a < f) && (a < g) && (a < h) && (a < i) && (a < j) && (a < k) && (a < l) && (a < m) && (a < n) && (a < o))
    {
        cout << a;
    }
    else if ((b > a) && (b > c) && (b > d) && (b > e) && (b > f) && (b > g) && (b > h) && (b > i) && (b > j) && (b > k) && (b > l) && (b > m) && (b > n) && (b > o)) || ((b < a) && (b < c) && (b < d) && (b < e) && (b < f) && (b < g) && (b < h) && (b < i) && (b < j) && (b < k) && (b < l) && (b < m) && (b < n) && (b < o))
    {
        cout << b;
    }
    else if ((c > a) && (c > b) && (c > d) && (c > e) && (c > f) && (c > g) && (c > h) && (c > i) && (c > j) && (c > k) && (c > l) && (c > m) && (c > n) && (c > o)) || ((c < a) && (c < b) && (c < d) && (c < e) && (c < f) && (c < g) && (c < h) && (c < i) && (c < j) && (c < k) && (c < l) && (c < m) && (c < n) && (c < o))
    {
        cout << c;
    }
    return 0;
}
Have you learned arrays yet?

If so you can input like this

1
2
3
4
5
6
7
8
const int SIZE = 15;
int array[SIZE];

std::cout << "Please enter 15 numbers: " << std::endl;
for( int i = 0; i < SIZE; ++i )
{
    std::cin >> array[i];
}


Then to output only the min and max numbers you would do something like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int min = 9;
int max = 0;

for( int i = 0; i < SIZE; ++i )
{
    if( array[i] < min )
    {
        min = array[i];
    }
    if( array[i] > max )
    {
        max = array[i];
    }
}

std::cout << "Min: " << min << std::endl;
std::cout << "Max: " << max << std::endl;
Or like this. It uses just 3 variables, and one more as a loop counter.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    int a;
    int min;
    int max;
    
    cout << "Enter 15 numbers.";
    cin >> a;
    min = max = a;
    for (int i = 1; i<15; i++)
    {
        cin >> a;
        if (a > max)
            max = a;
        else if (a < min)
            min = a;
    }
    cout << endl;
    cout << "max = " << max << endl;
    cout << "min = " << min << endl;
Topic archived. No new replies allowed.