对于循环运行额外的时间超过规定是什么? [英] For loop runs an extra time than what is specified?
本文介绍了对于循环运行额外的时间超过规定是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
那么,我的code的作用是,它需要在数量和输出所有的数字连续滚动平均值的列表。我scanf的函数运行,所以我只是把零一个额外的时间。我该如何解决这个问题?以下是我有:
So what my code does is that it takes in a list of numbers and outputs consecutive rolling average of all the numbers. My scanf function runs an extra time so I just put in a zero. How can I fix this? Here is what I have:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
int i,n;
double numbers[100]; double previous[100]; double x[100];
double mean[100]; double old_average[100]; double new_average[100];
printf("Total amount of numbers: ");
scanf("%d\n", &n);
for (i=1; i<n+1; i++){
scanf("%lf\n", &numbers[i]);
}
old_average[1] = numbers[1] / 1;
for (i=1; i<n+1; i++){
new_average[i] = (((old_average[i] * (i)) + numbers[i+1]) / (i+1));
old_average[i+1]=new_average[i];
}
printf("%lf\n", old_average[1]);
for (i=1; i<n+1; i++){
printf("%lf\n", new_average[i]);
}
//new_average[i] = (((old_average[i] * i-1) + numbers[i]) / i);
//mean[j] = numbers[i] + mean
//printf("%f\n", old_average[1]);
//for (i=2; i<n+2; i++){
// printf("%f\n", new_average[i]);
//}
return 0;
}
下面是输入和输出的样子
Here is what the input and output look like
Input:
Total amount of numbers: 10
10
8
9
15
12
2
3
8
7
11
0(this is the extra loop)
Output:
10.0
9.0
9.0
10.5
10.8
9.3
8.4
8.4
8.2
8.5
-1.#QNAN0 (this is the extra loop)
我怎样才能解决这个问题?
How can I fix this issue?
推荐答案
删除多余的 \\ n
,意思是直到读或明年非白色 - 文件结束 - 空格字符,传递到 scanf的每个格式说明()
。
Remove extra \n
, which means "read until end-of-file or next non white-space character", in each format specifier passed to scanf()
.
这篇关于对于循环运行额外的时间超过规定是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文