[英]Sum and average for n numbers with -1 as exit integer
Hello I have a small problem with my code and I want to understand it.您好,我的代码有一个小问题,我想了解一下。 My task is to write a program that take the sum and average for n numbers with while loops or nested while loops with if conditions, when you want to exit the code you should enter -1.
我的任务是编写一个程序,用 while 循环或嵌套的 while 循环,用 if 条件对 n 个数字求和和求平均值,当你想退出代码时,你应该输入 -1。 The code is down below.
代码在下面。 The problem I have is that I can't get it to exclude the -1 from the calculation .
我遇到的问题是我无法从计算中排除 -1 。 What am I doing wrong.
我究竟做错了什么。 And it is suppose to be a simple code.
它假设是一个简单的代码。
int main(void) {
int count;
float sum, average, number;
sum = 0;
count = 0;
printf("Calculate sum and average (-1 to quit)\n");
while (number!=-1) {
scanf("%f", &number);
sum = sum + number;
count = count + 1;
}
average = sum / count;
printf("Sum=%f", sum);
printf(" Average=%f", average);
}
in the while block, you read the number and then add it to your average calculation, and only then after the iteration repeats you're checking if it's different than -1 to stop the iteration:在 while 块中,您读取数字,然后将其添加到您的平均计算中,只有在重复迭代之后,您才检查它是否不同于 -1 以停止迭代:
there are obviously different ways to solve it:显然有不同的解决方法:
one way can be to use while(true) and in the while block after you read the number add an if statement to compare it with -1 and break out of the loop一种方法是使用 while(true) 并在读取数字后的 while 块中添加一个 if 语句以将其与 -1 进行比较并跳出循环
while (true) {
scanf("%f", &number);
if (number == -1) break;
sum = sum + number;
count = count + 1;
}
Reordering the different steps could solve this:重新排序不同的步骤可以解决这个问题:
int main(void) {
int count;
float sum, average, number=0.0f;
sum = 0;
count = -1;
printf("Calculate sum and average (-1 to quit)\n");
while (number!=-1) {
sum = sum + number; // first time no effect with 0
count = count + 1; // first time "no effect" counting to 0
scanf("%f", &number); // will not be counted or summed if -1
}
average = sum / count;
printf("Sum=%f", sum);
printf(" Average=%f", average);
}
Think it through with immediatly entered -1:立即输入 -1 仔细考虑:
Think it through with one number:用一个数字来想一想:
By the way, comparing a float for identity is risky.顺便说一下,比较浮动的身份是有风险的。 You would be safer if you could compare more "generously", eg
while (number>-0.5)
.如果你可以更“慷慨地”比较,你会更安全,例如
while (number>-0.5)
。
I think you should take the input at the last of the loop as it will get check in the next iteration.我认为你应该在循环的最后一个输入,因为它会在下一次迭代中得到检查。 And when -1 comes up it will not be added.
当 -1 出现时,它不会被添加。
#include <stdio.h>
int main(){
int count=0;
float sum=0, average=0, number=0;
printf("Calculate sum and average (-1 to quit)\n");
while(number!=-1){
sum = sum + number;
count = count + 1;
scanf("%f", &number);
}
average = sum / count;
printf("Sum=%f", sum);
printf(" Average=%f", average);
}
Code needs to test number
as -1 before including it in the summation.代码需要将
number
测试为 -1,然后才能将其包含在总和中。 OP's code test almost does this right. OP 的代码测试几乎可以做到这一点。 OP's code test for
-1
before number
is even assigned leading to undefined behavior (UB).甚至在分配
number
之前 OP 的代码测试-1
会导致未定义的行为(UB)。
float number;
while (number!=-1) { // UB!
Also good to test the return code of scanf()
for success.测试
scanf()
的返回码是否成功也很好。
while (scanf("%f", &number) == 1 && number != -1) {
sum = sum + number;
count = count + 1;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.