My task is: Data on the height of N students in a class are entered in turn. Determine the average, minimum and maximum height of students in the group.
I wrote such a solution, but the program incorrectly outputs the maximum and minimum values.
Console.Write("Number of students in the group = ");
int n = Convert.ToInt32(Console.ReadLine());
double result = 0;
double min = 0;
double max = 0;
for (double i = 0.0; i < n; i++)
{
Console.Write("The height of {0} student (in cm): ", i + 1);
double h = Convert.ToDouble(Console.ReadLine());
result += h;
if (max > h) max = h;
if (h < max) min = h;
}
double average = 0;
average = result / n;
Console.WriteLine("Average value = " + average);
Console.WriteLine("Maximum value = " + max);
Console.WriteLine("Minimun value = " + min);
Output:
What can I do to fix this?

if (max > h) max = h;does not make sense: wouldn't you rather changemaxwhenhis greater thanmax, not the other way around? The next line is a pure typo, though: you meant to comparehtomin, not tomax, before assigningmin.double.MinValueanddouble.MaxValueare constants programmed into c# that are helpful for such purposes. All the numeric types have these constants. 0 is in the middle of all possible range of signed values so not always a great choice