找到总和,最小值,最大值与阵列 [英] finding sum,min,max with array

查看:130
本文介绍了找到总和,最小值,最大值与阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

收件其输入则值的数量输入这些值(双型)逐一在一个循环中,最终输出的总和,最大值和最小值的程序。

Write a program that inputs number of values then inputs these values (double type) one by one in a loop and finally outputs their sum, the maximum value and the minimum value.

我写的code这个任务,但我得到的错误

I write the code for this assignment but i got error

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

错误number.c:在函数'主':
number.c:9:错误:数组大小NU缺失
number.c:11:错误:预期出厂前pression']'标记

error is number.c: In function ‘main’: number.c:9: error: array size missing in ‘nu’ number.c:11: error: expected expression before ‘]’ token

推荐答案

在C中,你需要指定数组的大小,如用浮动NU [100]; 但你如果你认为你需要存储所有这些值找错了树。最小,最大和金额都可以实时计算而不需要回去和检索任何previous号码。

In C, you need to specify the sizes of your arrays such as with float nu[100]; but you're barking up the wrong tree if you think you need to store all those values. The minimum, maximum and sum can all be calculated on the fly without any need to go back and retrieve any previous numbers.

所有你需要做的就是进入他们一次一个,并且对于每个:

All you need to do is enter them one at a time and, for each:


  • 如果它是第一个或更大的电流高,设置当前高吧。

  • 如果它是第一个以内目前的低,将目前的低吧。

  • 将其添加到总和。

的方式的总和,应初始化为零下手。

The sum by the way, should be initialised to zero to start with.

在伪code而言,入手这样的:

In terms of pseudo-code, start with this:

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

和理解它是获得一个(非常非技术)一张纸,写了一个表像这样的最佳方式:

And the best way to understand it is to get a (very non-tech) piece of paper and write up a table like this:

+-----+------+-----+-----+-------+------+
| sum | high | low | num | count | curr |
+-----+------+-----+-----+-------+------+
|     |      |     |     |       |      |
|     |      |     |     |       |      |
|     |      |     |     |       |      |
|     |      |     |     |       |      |
|     |      |     |     |       |      |
+-----+------+-----+-----+-------+------+

然后运行该程序一步一步的通过你的头,进入或在该表中更改值,当您去。您甚至可以当你使用未初始化值检测等,如果你碰到设置总和总结+ CURR 如果栏是空的。

您会惊奇地发现你开始如何迅速认为像一台电脑,只希望它不会推那些社交技巧从你的脑子在这个过程中: - )

You'll be surprised how quickly you begin to think like a computer, just hope it doesn't push all those social skills out of your head in the process :-)

这篇关于找到总和,最小值,最大值与阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆