C中的平均,最大和最小程序

时间:2013-12-25 06:40:49

标签: c while-loop

所以我用C编码,我需要提出一些代码,该代码将从用户那里获取n个数字,并找到它们的最小值,最大值,平均值和平方和。到目前为止,我有平均值和平方和部分,但最小值和最大值是咬我的。

请记住,我处于一个非常基本的水平,我还没有到达阵列。我所知道的只是逻辑运算符,函数,循环以及stdlib.h,math.h和stdio.h库的使用。

这是我到目前为止所拥有的。 当我在编译期间尝试将float和double放在一起时,平均函数给了我很多问题,因此将它乘以1.0固定。我拥有一切,只有最小和最大。我一直把最后一个条目作为我的最大值,并将0作为我的最小值。

#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);
}

8 个答案:

答案 0 :(得分:3)

你的算法不太对劲。以下是正确的实施:

#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;
}

我添加了错误检查和恢复。如果您对逻辑有任何疑问,请询问。

答案 1 :(得分:2)

当前编写代码的方式,min必须以较高的值(不是0)开始,否则代码将无效。要选择的最佳值是int的最大可能值。

您还应该考虑是否要在每次循环时重置这些变量。

答案 2 :(得分:1)

在循环外输入第一个num并将其分配给max min

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

将while循环更改为无限循环

while(1) {...} 

现在检查您的计数器count是否等于n是否与break无限循环

的条件
if(count == n)
    break;  

修改后的完整代码:

#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);
}

答案 3 :(得分:1)

假设列表中的第一个数字为最小值和最大值。 将每个下一个字符与当前最小值和当前最大值进行比较,并相应地更新。

答案 4 :(得分:0)

你的while循环应该看起来像

   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++;
}

我同意 Robert Harvey♦ .. 您必须设置min

答案 5 :(得分:0)

您的代码中存在一些问题:

  1. 在哪里阅读num ?您应该在 minmax
  2. 之前执行此操作
  3. while循环首次执行 时,您应该 num分配给max和{{1} }。
  4. 类似的东西:

    min

答案 6 :(得分:0)

添加布尔值,移动给出值min,max 0是循环的开始

#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);
}

还应该考虑检查scanf的返回值

答案 7 :(得分:0)

npm run wbp

ERROR in ./node_modules/css-loader/dist/cjs.js?{"modules":false,"importLoaders":2,"sourceMap":true,"minimize":true}!./node_modules/react-base-table/styles.css
Module build failed: ValidationError: Invalid options object. CSS Loader has been initialised using an options object that does not match the API schema.
 - options has an unknown property 'minimize'. These properties are valid:
   object { url?, import?, modules?, sourceMap?, importLoaders?, localsConvention?, onlyLocals?, esModule? }
    at validate (/Users/ivanfigueredo/Documents/Projects/OptionsAPI/node_modules/schema-utils/dist/validate.js:85:11)
    at Object.loader (/Users/ivanfigueredo/Documents/Projects/OptionsAPI/node_modules/css-loader/dist/index.js:34:28)
 @ ./node_modules/react-base-table/styles.css 2:26-87
 @ ./wwwroot/source/optionsList.jsx
 @ ./wwwroot/source/app.js