Average, max, and min program in C

我与影子孤独终老i 提交于 2019-12-20 02:11:53

问题


So I'm coding in C, and I need to come up with code that will take n numbers from the user, and find their minimum, maximum, average, and sum of squares for for their values. So far I have the average and sum of squares portion, but the minimum and maximum is biting me.

Keep in mind I'm at a very rudimentary level, and I have not reached arrays yet. All I know are logical operators, functions, loops, and the use of the stdlib.h, math.h, and stdio.h libraries.

This is what I have so far. The average function gave me a lot of problems when I tried to put float and double during compiling, so multiply it by a 1.0 fixed that. I have everything, just the minimum and maximum. I keep getting the last entry as my maximum, and a 0 for my minimum.

#include<stdio.h>
int main()
{
    float average;
    int i, n, count=0, sum=0, squaresum=0, num, min, max;


    printf("Please enter the number of numbers you wish to evaluate\n");
    scanf_s("%d",&n);

    printf("Please enter %d numbers\n",n);

    while(count<n)
    {
        min=0;
        max=0;

            if(num>max)
               max=num;
            if(num<min)
               min=num;

            scanf_s("%d",&num);

        sum = sum+num;
        squaresum = squaresum + (num*num);

        count++;
    }
        average = 1.0*sum/n;

    printf("Your average is %.2f\n",average);
    printf("The sum of your squares is %d\n",squaresum);    



    printf("Your maximum number is %d\n",max);
    printf("Your minimum number is %d\n",min);

return(0);
}

回答1:


Your algorithm is not quite right. Below is the correct implementation:

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main(void)
{
    float average;
    int n, num, count = 0, sum = 0, squaresum = 0;
    int min = INT_MAX, max = INT_MIN;
    bool gotAnswer = false;

    /* Don't Let User Enter Wrong Input */
    while(!gotAnswer)
    {
        printf("Please enter the number of numbers you wish to evaluate: ");
        if(scanf_s("%d", &n) != 1)
        {
            /* User Entered Wrong Input; Clean Up stdin Stream*/
            while(getchar() != '\n')
            {
                 continue;
            }
        }
        else
        {
            /* User Input Was Good */
            gotAnswer = true;
        }
    }

    /* Clear stdin Stream Just In Case */
    while(getchar() != '\n')
        continue;

    while(count < n)
    {
        /* Don't Let User Enter Wrong Input */
        gotAnswer = false;
        printf("Enter number %d: ", count + 1);
        if(scanf_s("%d", &num) != 1)
        {
            /* User Entered Wrong Input; Clean Up stdin Stream */
            while(getchar() != '\n')
                continue;

            /* Let User Try Again */
            continue;
        }
        else
        {
            /* User Input Was Correct */
            gotAnswer = true;

            /* Clear stdin Stream Just In Case */
            while(getchar() != '\n')
                continue;
        }

        if(num > max)
            max = num;
        if(num < min)
            min = num;

        sum += num;
        squaresum += num * num;
        count++;
    }

    average = 1.0 * sum / n;

    printf("Your average is %.2f\n", average);
    printf("The sum of your squares is %d\n", squaresum);    
    printf("Your maximum number is %d\n", max);
    printf("Your minimum number is %d\n", min);

    system("pause");
    return 0;
}

I've added error checking and recovery. Please ask if you have any questions about the logic.




回答2:


The way your code is currently written, min has to start out at a high value (not 0), or the code won't work. The best value to choose is the maximum possible value for an int.

You should also consider whether or not you want to reset these variable each time through the loop.




回答3:


Enter the first num outside the loop and assign that to max min

scanf("%d",&num);
max = min = num;  

Change your while loop to infinite loop

while(1) {...} 

and now check for the condition that whether your counter count is equal to n is or not to break out from the infinite loop

if(count == n)
    break;  

Full code after modification:

#include<stdio.h>
int main()
{
    float average;
    int i, n, count=0, sum=0, squaresum=0, num, min, max;

    printf("Please enter the number of numbers you wish to evaluate\n");
    scanf_s("%d",&n);

   printf("Please enter %d numbers\n",n);

   scanf_s("%d",&num);
   max = min = num;

   while(1)
   {
        if(num>max)
           max=num;
        if(num<min)
           min=num;

     sum = sum+num;
     squaresum = squaresum + (num*num);

     count++;
     if(count == n)
        break;
     scanf_s("%d",&num);

   }
    average = 1.0*sum/n;

printf("Your average is %.2f\n",average);
printf("The sum of your squares is %d\n",squaresum);



printf("Your maximum number is %d\n",max);
printf("Your minimum number is %d\n",min);

return(0);
}



回答4:


Assume your first number in the list as the minimum and maximum. Compare every next character with the current minimum and the current maximum and update accordingly.




回答5:


your while loop should look like

   min=3;
   max=0;
while(count<n)
    { 

     scanf("%d",&num);
    if(num>max)
    max=num;
    if(num<min)
    min=num;

    sum = sum+num;
    squaresum = squaresum + (num*num);

    count++;
}

And I agree with Robert Harvey♦.. You must set min




回答6:


Add a boolean, moved giving the values min, max 0 are the start of loop

#include<stdio.h>
int main()
{
    float average;
    int i, n, count=0, sum=0, squaresum=0, num, min, max;

    bool first = true;
    printf("Please enter the number of numbers you wish to evaluate\n");
    scanf_s("%d",&n);

    printf("Please enter %d numbers\n",n);
        min=0;
        max=0;

    while(count<n)
    {
            scanf_s("%d",&num);

        if (first) {
           first = false;
           min = max = num;
        }
            if(num>max)
               max=num;
            if(num<min)
               min=num;


        sum = sum+num;
        squaresum = squaresum + (num*num);

        count++;
    }
        average = 1.0*sum/n;

    printf("Your average is %.2f\n",average);
    printf("The sum of your squares is %d\n",squaresum);    



    printf("Your maximum number is %d\n",max);
    printf("Your minimum number is %d\n",min);

return(0);
}

Should also consider to check the return value of scanf




回答7:


There're some issues in your code:

  1. Where num is read? You should do it before min and max
  2. When while loop executes first time you should just assign num to max and min.

Something like that:

  int min = 0;
  int max = 0;

  // If your compiler supports C99 standard you can put
  // bool first_time = true;
  int first_time = 1;

  while (count < n) {
    printf("Please, enter the next number\n");
    scanf_s("%d", &num); 

    // If your compiler supports C99 you can put it easier:
    // if (first_time) { 
    if (first_time == 1) {
      first_time = 0;
      max = num;
      min = num;
    }
    else {
      if(num > max)
        max = num;

      if(num < min)
        min = num;
    }
  ...


来源:https://stackoverflow.com/questions/20769834/average-max-and-min-program-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!