Find lowest number

I'm having trouble getting my program to display the proper minimum number. It is outputting (-858993460) It is outputting the highest number and average number properly, just not the lowest. I've tried everything I've found on here but I'm missing something. Any help would be appreciated.

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
const int SIZE = 20;
	int grades[SIZE];
	int count = 0, total = 0;
		
	cout << "Enter up to 20 grades, then enter -1 when finished." << endl;
	cout << endl;

	while (count < SIZE)
	{
		cout << "Enter value #" << count + 1 << ": ";
		cin >> grades[count];

		if (grades[count] != -1)
		{
			total += grades[count];
			count++;
		}
		else
			break;
	}


	int max = grades[0];
	int min = grades[0];

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

	int average = total / count;

	cout << endl;
	cout << "The highest grade is: " << max << endl;
	cout << "The lowest grade is: " << min << endl;
	cout << "The average grade is: " << average << endl;
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
59
#include <iostream>

int main()
{
    const int SIZE = 20;
    int grades[SIZE];
    int count = 0, total = 0;

    std::cout << "Enter up to 20 grades, then enter -1 when finished.\n\n" ; // << endl;
    // cout << endl;

    const int min_grade = 0 ; // *** added
    const int max_grade = 100 ; // *** added

    while (count < SIZE)
    {
        std::cout << "Enter value #" << count + 1 << ": ";
        std::cin >> grades[count];

        // if (grades[count] != -1)
        if( grades[count] == -1 ) break ;

        if( grades[count] >= min_grade && grades[count] <= max_grade ) // *** added
        {
            total += grades[count];
            count++;
        }
        else std::cout << "invalid grade. please enter a value in [" << min_grade << ',' << max_grade << "]\n" ;
            // break;
    }


    int max = grades[0];
    int min = grades[0];

    for (int i = 0; i < /*SIZE*/ count; ++i)
    {
        if (max < grades[i])
        {
            max = grades[i];
        }
        else if (min > grades[i])
        {
            min = grades[i];
        }
    }

    if( count > 0 ) // *** added
    {
        // int average = total / count;
        const double average = double(total) / count ; // avoid integer division

        // cout << endl;
        std::cout << "\nThe highest grade is: " << max // << endl;
        /*cout*/  << "\nThe lowest grade is: " << min // << endl;
        /*cout*/  << "\nThe average grade is: " << average << '\n' ; // << endl;
    }

}
Thanks a lot! I understand it a lot better now.
Topic archived. No new replies allowed.