Min and max in C using basics

前端 未结 4 1153
鱼传尺愫
鱼传尺愫 2021-01-24 22:04

This program is supposed to end when the user enters 0 and then show the count, sum, average, min and max. I am able to figure out the sum count and average but my min and max i

相关标签:
4条回答
  • 2021-01-24 22:23

    Set the first number that scanf reads as initial value of min and initial value of max;This will always work whatever the numbers are.

    if (count==1)
    {
        min=number;
        max=number;
    }
    
    0 讨论(0)
  • 2021-01-24 22:24

    You used:

    if (number > 0)
    {
        count = count + 1;
        sum += number;      
        min = number;
        max = number;
    }
    

    Here, don't use:

    min = number;
    max = number;
    

    Because, when number is greater than 0, min and max value will be set to the input number so the if and else if statement below it, will not work.

    0 讨论(0)
  • 2021-01-24 22:40

    First, initialize min and max with proper values.

    int min = INT_MAX;
    int max = INT_MIN;
    

    Second, update min only when the input number less than current min. Update max only when the input number is greater than the current max.

    0 讨论(0)
  • 2021-01-24 22:42
    int main()
    {
        int number = 0;
        int count = 0;
        int sum = 0;
        int average;
        int min = INT_MAX;   // don't forget to include limits.h
        int max = INT_MIN;
    
        do {        
            printf("Enter a number: ");
            scanf_s("%d", &number);
            if (number > 0)
            {
                count = count + 1;
                sum += number;
                if (number < min)
                {
                    min = number;
                }
                if (number > max)
                {
                    max = number;
                }
            }
            else if (number < 0) 
            {
                printf("Negative value entered...skipping");
            }
        } while (number != 0);
    
        printf("count: %d\n",  count);
        printf("Sum: %d\n", sum);
        average = sum / count;
        printf("average: %d\n", average);
        printf("Minimum: %d\n", min);
        printf("Maximum: %d\n", max);
    
        system("pause");
    }
    

    You need to do two things:

    1. Remove min = number; and max = number from the check number > 0. This is because, you are overriding the variables with the number value. This will lead to loss of previous min and max values, if any.
    2. Instead of int min = 0; and int max = 0, use the upper and lower limit for integer data type. That is present in limits.h. You can use INT_MAX (2147483647) and INT_MIN (–2147483648).
    3. There is another problem with your code. instead of else if (number > max), it should be if (number > max). For every number you need to check for both min and max values.
    4. Also, the condition if (number > 0) should instead be if (number != 0). This is because you want the program to end when user enters 0, so for it to accept negative numbers that condition has to be changed.
    5. Also, you need not calculate average every single time. Instead you can calculate after coming out of the loop.
    6. Also, you need to move the checks for min and max inside the check number != 0. The reason for this is that you don't want to calculate min and max when number == 0.
    0 讨论(0)
提交回复
热议问题