最大值和最小值相减的平均值 returns 错误结果
Average subtracted of max and min returns wrong result
我想求除 table 的最大值和最小值之外的 5 个数字的平均值。
但是下面的代码提供了错误的结果。
#include <stdio.h>
#define size 5
main() {
int i;
float table[size], max, min, mo, sum;
max = 0;
min = 0;
mo = 0;
printf("Provide a number: ");
scanf("%f", &table[1]);
max = table[1];
min = table[1];
for (i = 2; i <= size; i++) {
printf("Provide a number: ");
scanf("%f", &table[i]);
if (max <= table[i])
max = table[i];
if (min >= table[i]) {
min = table[i];
}
sum = sum + table[i];
}
mo = (sum - max - min) / (size - 2);
printf("The average numberis: %f", mo);
}
示例:
如果您输入 1,2,3,4,5,输出将是 2,666667 而不是 3。
您能告诉我为什么会这样吗?
我已经根据评论编辑了代码,并且它的词正确。非常感谢!
#include <stdio.h>
#define size 5
main() {
int i;
float table[size], max, min, mo, sum;
max = 0;
min = 0;
mo = 0;
printf("Provide a number: ");
scanf("%f", &table[0]);
max = table[0];
min = table[0];
sum = table[0];
for (i = 1; i < size; i++) {
printf("Provide a number: ");
scanf("%f", &table[i]);
if (max <= table[i])
max = table[i];
if (min >= table[i]) {
min = table[i];
}
sum = sum + table[i];
}
mo = (sum - max - min) / (size - 2);
printf("The average numberis: %f", mo);
}
以下修改后的代码工作正常。
#include <stdio.h>
#define size 5
void main()
{
int i=0, count=0;
float table[size], max, min, sum=0, avg;
//read all five numbers
for (i = 0; i < size; i++)
{
printf("Provide a number: ");
scanf("%f", &table[i]);
}
//find max and min
max = table[0];
min = table[0];
for (i = 1; i < size; i++)
{
if(max < table[i])
max = table[i];
if(min > table[i])
min = table[i];
}
//calculate sum and average leaving the max and min
for (i = 0; i < size; i++)
{
if((table[i] != max) && (table[i] != min))
{
sum = sum + table[i];
count++;
}
}
avg = sum/count;
printf("\r\nThe average number is: %f", avg);
}
`
我想求除 table 的最大值和最小值之外的 5 个数字的平均值。
但是下面的代码提供了错误的结果。
#include <stdio.h>
#define size 5
main() {
int i;
float table[size], max, min, mo, sum;
max = 0;
min = 0;
mo = 0;
printf("Provide a number: ");
scanf("%f", &table[1]);
max = table[1];
min = table[1];
for (i = 2; i <= size; i++) {
printf("Provide a number: ");
scanf("%f", &table[i]);
if (max <= table[i])
max = table[i];
if (min >= table[i]) {
min = table[i];
}
sum = sum + table[i];
}
mo = (sum - max - min) / (size - 2);
printf("The average numberis: %f", mo);
}
示例:
如果您输入 1,2,3,4,5,输出将是 2,666667 而不是 3。
您能告诉我为什么会这样吗?
我已经根据评论编辑了代码,并且它的词正确。非常感谢!
#include <stdio.h>
#define size 5
main() {
int i;
float table[size], max, min, mo, sum;
max = 0;
min = 0;
mo = 0;
printf("Provide a number: ");
scanf("%f", &table[0]);
max = table[0];
min = table[0];
sum = table[0];
for (i = 1; i < size; i++) {
printf("Provide a number: ");
scanf("%f", &table[i]);
if (max <= table[i])
max = table[i];
if (min >= table[i]) {
min = table[i];
}
sum = sum + table[i];
}
mo = (sum - max - min) / (size - 2);
printf("The average numberis: %f", mo);
}
以下修改后的代码工作正常。
#include <stdio.h>
#define size 5
void main()
{
int i=0, count=0;
float table[size], max, min, sum=0, avg;
//read all five numbers
for (i = 0; i < size; i++)
{
printf("Provide a number: ");
scanf("%f", &table[i]);
}
//find max and min
max = table[0];
min = table[0];
for (i = 1; i < size; i++)
{
if(max < table[i])
max = table[i];
if(min > table[i])
min = table[i];
}
//calculate sum and average leaving the max and min
for (i = 0; i < size; i++)
{
if((table[i] != max) && (table[i] != min))
{
sum = sum + table[i];
count++;
}
}
avg = sum/count;
printf("\r\nThe average number is: %f", avg);
}
`