用数组求sum,min,max

amb*_*eha 1 c

编写一个输入数值的程序,然后在循环中逐个输入这些值(双精度型),最后输出它们的和,最大值和最小值.

我为这个作业编写了代码,但是我收到了错误

#include <stdio.h>

int main(void)
{
        float a;
        float i;
        short c;
        float sum;
        float nu[];

        i=nu[];

          printf("Number of values :");
          scanf("%f",&i);

        for (c=1;i>=c;c++)
        {
          printf("values=");
          scanf("%f",&nu[c]);

                sum = sum + nu[c];
        //      printf("Sum = %f\n",sum);
        }

        printf("sum = %f \n",sum);
//      printf("Number of values :");
//      scanf("%f",&i);
}
Run Code Online (Sandbox Code Playgroud)

错误是number.c:在函数'main'中:number.c:9:错误:'nu'number.c中缺少数组大小:11:错误:']'令牌之前的预期表达式

pax*_*blo 10

在C中,您需要指定数组的大小,例如,float nu[100];如果您认为需要存储所有这些值,则会出现错误的树.最小值,最大值和总和都可以在运行中计算,无需返回并检索任何先前的数字.

您需要做的就是一次输入一个,每个输入:

  • 如果它是当前高电平的第一个或更高,则将电流设置为高电平.
  • 如果它是当前低电平的第一个或更低,则将电流设置为低电平.
  • 将它添加到一个总和.

顺便说一句,应该初始化为零开始.

就伪代码而言,从这开始:

set sum, high and low all to zero

scan "number of values" into count

for curr = one to count, inclusive:
    scan "current number" into num

    set sum to sum + num

    if curr is one, or num is less than low:
        set low to num

    if curr is one, or num is greater than high:
        set high to num

output "Minimum = " low
output "Maximum = " high
output "Sum     = " sum
Run Code Online (Sandbox Code Playgroud)

理解它的最好方法是获得一张(非常非技术性的)纸张并写出如下表格:

+-----+------+-----+-----+-------+------+
| sum | high | low | num | count | curr |
+-----+------+-----+-----+-------+------+
|     |      |     |     |       |      |
|     |      |     |     |       |      |
|     |      |     |     |       |      |
|     |      |     |     |       |      |
|     |      |     |     |       |      |
+-----+------+-----+-----+-------+------+
Run Code Online (Sandbox Code Playgroud)

然后逐步运行该程序,随时输入或更改该表中的值.你甚至可以在你使用未初始化的值来检测这样的,如果你遇到了set sum to sum + curr,如果sum栏是空的.

你会开始像计算机那样快速思考,你会感到惊讶,只希望它不会在这个过程中将所有这些社交技能从头脑中推出:-)