help with a function

I am trying to write a function to find the maximum, minimum and average of group of numbers entered by user.

I did max and average I have problem with finding min number

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  void readE(double & max, double & min, double & average)
{
	double number, count = 0, sum = 0;
	//max = 0; min = 0; average = 0;
	cout << "Gimme #'s to do sum Math: ";
	for (number; cin >> number; count++)
	{
		sum += number;
		
		if (number > max)
			max = number;
		if (max>min)
			min;
		
	}
	average = (sum / count);
	recover();
	if (count == 0) die("Input Failure");
	cout << "The largest # seen is: " << max << endl; 
	cout << "The smallest # seen is: " << min << endl;
	cout<<"The sum of the #'s seen is: " << average << endl;
}
Finding the minimum is the same as finding the maximum, just with the > flipped to a <.
it returns 0 !
It'll work if you enter a negative number. :)

To fix it, you'll need to either initialize min to a big number (say, std::numeric_limits<double>::max()) before the loop or put a if (count == 0) min = number; in your loop.
this also doesn't work

I have tried most of these workarounds
Okay, so what do you have so far?
it doesn't work if you have something like

9 8 7 6

max =9
min = 9
average=7.5
Can you post your code for that function?
it's in my first post
Works for me:
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
void readE(double & max, double & min, double & average)
{
    double number, count = 0, sum = 0;
    //max = -1e100; min = 1e100; average = 0;
    cout << "Gimme #'s to do sum Math: ";
    for (; cin >> number; count++)
    {
        if (count == 0)
        {
            min = number;
            max = number;
        }
        sum += number;
        
        if (number > max)
            max = number;
        if (number < min)
            min = number;
    }
    average = (sum / count);
    recover();
    if (count == 0) die("Input Failure");
    cout << "The largest # seen is: " << max << endl; 
    cout << "The smallest # seen is: " << min << endl;
    cout<<"The sum of the #'s seen is: " << average << endl;
}
Yes it does work with me

can you please tell comment on the steps so I know what going on
I only added these lines:
1
2
3
4
5
6
7
8
if (count == 0)
{
    min = number;
    max = number;
}
// ...
if (number < min)
    min = number;

Those last two lines are essentially my first post:
I wrote:
Finding the minimum is the same as finding the maximum, just with the > flipped to a <.

The first few lines set the min and max to the number the user entered, but only on the first iteration of the loop (when count is 0).
That fixes the problem with min becoming 0 using my second suggestion:
I wrote:
To fix it, you'll need to either [...] or put a if (count == 0) min = number; in your loop.
Topic archived. No new replies allowed.