Find lowest number w/ Function

I am having trouble getting the lowest number to display. Everything else works fine and the entire program works fine when not in a function. I'm assuming there is something wrong with my for loop and that is why it gives me the max but not the min?

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  int getGrades(int[], int);
int calcStats(int[], int, int&, int&, int&);

int main()
{
	const int SIZE = 20;
	int grades[SIZE];
	int max, min, average;
	
	
	cout << "Enter up to 20 grades, then enter -1 when finished." << endl;
	cout << endl;

	getGrades(grades, SIZE);

	calcStats(grades, SIZE, max, min, average);

	cout << endl;
	cout << "The highest grade is: " << max << endl;
	cout << "The lowest grade is: " << min << endl;
	cout << "The average grade is: " << average << endl;

	system("pause");
	return 0;
}

int getGrades(int grades[], int SIZE)
{
	int count = 0;
	int total = 0;
	const int min_grade = 0;
	const int max_grade = 100;

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

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

		if (grades[count] >= min_grade && grades[count] <= max_grade)
		{
			count++;
		}
	}
	 
	return grades[count];
}

int calcStats(int grades[], int SIZE, int &max, int &min, int &average)
{
	int count = 0;
	int total = 0;
	int min_grade = 0;
	int max_grade = 100;
	
	while (count < SIZE)
	{
		if (grades[count] == -1)
			break;

		if (grades[count] >= min_grade && grades[count] <= max_grade)
		{
			total += grades[count];
			count++;
		}
	}

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

	if (count > 0)
	{
		average = total / count;
	}

	return max, min, average;
}
Two things.

1. Could it be possible that a grade is both more than max and less than min simultaneously? You else if does not handle such case.

2. What is fundamentally wrong in this program:
1
2
3
4
5
int main() {
  int foo;
  if ( foo > 42 ) std::cout "Hello";
  return 0;
}

(Your program has the same issue.)

3. The grades in input are {7, 666, 42, -1}
* You count 2 valid grades.
* You thus consider 7 and 666 for min and max?
I got to work. I put max = 0 and min = 100 in int main () and it worked.
Last edited on
Topic archived. No new replies allowed.