-1

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:

enter image description here

What can I do to fix this?

5
  • 3
    if (max > h) max = h; does not make sense: wouldn't you rather change max when h is greater than max, not the other way around? The next line is a pure typo, though: you meant to compare h to min, not to max, before assigning min. Oct 13, 2021 at 1:06
  • 3
    I suggest you get acquainted with the visual studio debugger. That would have helped you detect the logical error. Sergey is right here. Oct 13, 2021 at 1:07
  • 2
    Using the free, built-in Step Debugger to debug your code is easier than you think. It will also help you learn how code executes which will help you write better code. Oct 13, 2021 at 1:10
  • "if max is bigger than h, set max to h" "if h is smaller than max, set min to h". Do these sound right to you? Oct 13, 2021 at 1:13
  • Tip; if you want to find a maximum, start from an absolute minimum and work up. If you want to find a minimum start from an absolute maximum and work down. double.MinValue and double.MaxValue are 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
    – Caius Jard
    Oct 13, 2021 at 5:39

1 Answer 1

1

if (h < max) min = h; is a bug, since at the start all values =0, and since height will never be less then 0, and will never be true and min will stay 0 forever.

if (max > h) max = h; does not make much sense too for the same reason. Max is 0 at the start, so it will never be true and max will be 0 forever.

Try this

static void Main()
{
    Console.Write("Number of students in the group = ");
    int n = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine(n.ToString());
    double result = 0;
    double min = 0;
    double max = 0;
    for (var i = 0; i < n; i++)
    {
        Console.Write("The height of {0} student (in cm):  ", i + 1);
        double h = Convert.ToDouble(Console.ReadLine());
        Console.WriteLine(h.ToString());
        result += h;
        if (h > max) max = h;
        if ( min==0 || h < min) min = h;
    }
    double average = result / n;
    Console.WriteLine("Average height = " + average);
    Console.WriteLine("Maximum height = " + max);
    Console.WriteLine("Minimun heignt = " + min);
}

output

Number of students in the group = 5
The height of 1 student (in cm):  140
The height of 2 student (in cm):  150
The height of 3 student (in cm):  130
The height of 4 student (in cm):  170
The height of 5 student (in cm):  165
Average heignt = 151
Maximum heignt = 170
Minimun heignt = 130
1
  • 2
    Yes, but don't do their homework for them..
    – Caius Jard
    Oct 13, 2021 at 5:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.