Find max and min of loop

Hey everyone,

I've been assigned to make a program that takes the average of student test scores and tell you how many passed, failed, the number of students who took it, as well as the min and max. My problem is finding the min and max of the loop. When using a do while loop you're inputting infinite variables till you declare -1 to end the loop. My problem is, when I try to find the max/min of the inputs, it uses the most recent inputs and compares that to the first input opposed to using all the inputs and comparing it to the first.

I've attached the code, developed in Visual Studio 2015. Feel free to run it. I'm just stuck right now.

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
28
29
30
31
32
33
34
35
36
37
38
39
  #include <iostream>
#include <limits>
#include <iomanip>

using namespace std;

int main()
{
	double firstgrade, nextgrade, stotal = 0.0, total = 0.0, average = 0.0, min = 0.0, max = 0.0;
	int gradecount = 0, passed = 1, failed = -1;

	cout << "Enter grades at one at a time. When finished, enter a negative value to end.\n";
	cout << "Enter first grade: ";
	cin >> firstgrade;
	if (firstgrade < 0) { cout << "There are no statistics to report.\n"; system("pause"); return 0; }
	else do
	{
		cout << "Enter next grade or -1 to end: ";
		cin >> nextgrade;
		stotal += nextgrade;
		gradecount++;
		if (nextgrade >= 60) { passed++; }
		if (nextgrade < 60) { failed++; }
		if (nextgrade > firstgrade) { max = nextgrade; }
		if (nextgrade > firstgrade) { min = firstgrade; }
	} while (nextgrade >= 0);
	
	cout << "There are " << gradecount << " grades.\n";
	total = ((stotal + 1) + firstgrade);
	average = (total / gradecount);
	cout << "The grades range from " << min << " to " << max << endl;
	cout << "The average is " << fixed << setprecision(1) << average << ".\n";
	
	cout << passed << " students passed.\n";
	cout << failed << " students failed.\n";

	system("pause");
	return 0;
}
Last edited on
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
#include <iostream>
using namespace std;

int minimum(int, int);
void minmax(int &, int &, int, int);

int main()
{
    int num1, num2;
    int min, max;

    cout << "please provide two numbers" << endl;
    cin >> num1 >> num2;

    //call mininum function
    min = minimum(num1, num2);

    cout << "the minimum is " << min << endl;

    //call minmax function
    minmax(min, max, num1, num2);

    cout << "The minimum is " << min << endl;
    cout << "The maximum is " << max << endl;

    return 0;

}

/*******************************************
    Function: minimum
    Input: two numbers, num1 and num2 (integers)
    Output: The minimum of the two numbers (min)
********************************************/
int minimum(int num1, int num2)
{
    if (num1 > num2)
    {
        return num2;
    }
    else
    {
        return num1;
    }
}

/********************************************************
    Function: minmax()
    Input: min (int &)
           max (int &)
           num1 (int)
           num2 (int)
    Output: no formal return but a modifcation of min and max
*******************************************************/
void minmax(int &min, int &max, int num1, int num2)
{
    if (num1>num2)
    {
        max = num1;
        min = num2;
    }
    else
    {
        max = num2;
        min = num1;
    }
}
Thank you but that example is for two input numbers. My statement is a loop so it can range from 2 numbers to how ever many.
you will have to figure out a way to make it work with if statements. at least thats what i would do. this is just an example. yours can be constructed in a similair way. use your smarts to figure out a way to make it work. you can do the same thing for the max
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
#include <iostream>
#include <cstdlib>

using namespace std;

int rand_int(int a, int b);
int findMin (int x, int y, int z);

int main()
{
    int bottom, top;
    int x, y, z, result;
   
    cout << "Please enter a range.... 2 numbers" << endl;
    cin >> bottom;
    cin >> top;

    srand(time(NULL));

    x = rand_int(bottom, top);
    y = rand_int(bottom, top);
    z = rand_int(bottom, top);
    cout << "x = " << x << "y = " << y << "z = " << z << endl;
    result = findMin(x,y,x);
    cout << "res = " << result << endl;
}


int rand_int(int a, int b) {
return rand()%(b-a+1) + a;
}

int findMin (int x, int y, int z)
{
if((x<=y)&&(x<=z))
    return(x);
if((y<=x)&&(y<=z))
    return(y);
if((z<=x)&&(z<=y))
    return(z);
}
Last edited on
This seems to work...

I changed the min & max variables to mingrade and maxgrade... min & max are keywords and may cause problems in some cases, just my preference. I changed mingrades initilization value. It's hard to enter a number less than 0 and not quit the program.





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
#include <iostream>
#include <limits>
#include <iomanip>

using namespace std;

int main()
{
	double firstgrade, nextgrade, stotal = 0.0, total = 0.0, average = 0.0, mingrade = 100.0, maxgrade = 0.0;           
	int gradecount = 0, passed = 1, failed = -1;

	cout << "Enter grades at one at a time. When finished, enter a negative value to end.\n";
	cout << "Enter first grade: ";
	cin >> firstgrade;
	if (firstgrade < 0) { cout << "There are no statistics to report.\n";  return 0; }
	else do
	{
		cout << "Enter next grade or -1 to end: ";
		cin >> nextgrade;

		if (nextgrade>-1){
		if (nextgrade<mingrade){mingrade=nextgrade;}
		if (nextgrade>maxgrade){maxgrade=nextgrade;}
		}
		stotal += nextgrade;
		gradecount++;
		if (nextgrade >= 60) { passed++; }
		if (nextgrade < 60) { failed++; }
		//if (nextgrade > firstgrade) { maxgrade = nextgrade; }
		//if (nextgrade > firstgrade) { mingrade = firstgrade; }
	} while (nextgrade >= 0);

	cout << "There are " << gradecount << " grades.\n";
	total = ((stotal + 1) + firstgrade);
	average = (total / gradecount);
	cout << "The grades range from " << mingrade << " to " << maxgrade << endl;
	cout << "The average is " << fixed << setprecision(1) << average << ".\n";

	cout << passed << " students passed.\n";
	cout << failed << " students failed.\n";

		return 0;
}
Last edited on
Thank you so much! I would've never gotten that. It's interesting to see what the mind of others can do.

Question though, why did you declare mingrade as 100.0 and maxgrade as 0.0? Shouldn't it be the other way around?
Last edited on
Topic archived. No new replies allowed.