获取最小值,最大值和输入的五个数字的AVE [英] Getting the min, max, and ave of the five numbers inputted

查看:315
本文介绍了获取最小值,最大值和输入的五个数字的AVE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用C,获取5个输入数字,然后点击这些存储阵列上的程序工作。获得了5个号码后,我必须得到分钟,最大和最低限度的的平均和最大的数字输入而不是所有的五个。因此,这里的code,我做。当我得到的最大数目,它似乎是工作的罚款。但是,当它来的给分,它仍然是相同的最大,所以我会得到不同的平均值。

 的#include<&stdio.h中GT;
#包括LT&;&CONIO.H GT;诠释的main()
{
INT NUM [5];
INT计数器,最小值,最大值= 0;
浮动平均,总;
分= NUM​​;对于(计数器= 1;计数器< = 5;反++)
{
    的printf(请输入一个数字:);
    scanf的(%d个,试验#[5]);    如果(NUM [5]≥最大值)
    {
        最大= NUM​​ [5];
    }    如果(NUM [5]<分)
    {
        分= NUM​​ [5];
    }
}总= MIN + MAX;
平均=总/ 2;的printf(最大数目为:%d个\\ N,最大值);
的printf(最小号码为:%d个\\ N,分钟);
的printf(平均是:%D,平均);
残培();
返回0;
}


解决方案

由于这是一个学习的过程,我也不会纠正你的code,但指出需要修正的内容:


  • 在C数组是从0开始索引,而不是一个,所以计数器应从0到4,包容

  • INT ,而 NUM 是一个数组,所以分配 MIN = NUM​​ 无效

  • scanf函数应该把数据放入试验#[计数] ,而不是&放; NUM [5]

  • 在您codeD你的循环,你并不需要一个数组在一路:你需要输入的最后一个号码

  • 不能被计算为分+ MAX ;你需要保持运行总和,在每次迭代更新它。

I'm trying to work on a program in C that gets 5 input numbers and then store these in an array. After getting the the 5 numbers, I must be getting the min, max and the average of the MINIMUN AND MAXIMUM numbers inputted and not all of the five. So here's the code that I made. When I get the maximum number, it seems to be working fine. But when it come's to the min, it's still same as the maximum and so I'll be getting a different average.

#include <stdio.h>
#include <conio.h>

int main()
{
int num[5];
int counter, min, max=0;
float average, total;
min=num;

for(counter=1; counter<=5; counter++)
{
    printf("Enter a number: ");
    scanf("%d", &num[5]);

    if(num[5]>max)
    {
        max = num[5];
    }

    if (num[5]<min)
    {
        min = num[5];
    }
}

total = min + max;
average = total/2;

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


getch();
return 0;
}

解决方案

Since this is a learning exercise, I wouldn't correct your code, but point out what needs to be fixed:

  • Arrays in C are indexed from zero, not from one, so the counter should go from 0 to 4, inclusive
  • min is an int, while num is an array, so the assignment min=num is invalid
  • scanf should put the data into &num[count], not &num[5]
  • In the way that you coded your loop you do not need an array at all: you need the last number entered.
  • total cannot be computed as min+max; you need to keep a running total, updating it on each iteration.

这篇关于获取最小值,最大值和输入的五个数字的AVE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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