[英]Count function always returns at least 264 C
I am currently writing a program in c that is to do a few equations via functions. 我目前正在用c编写一个程序,该程序通过函数来做一些方程。 The possible data that is going to be read is up to 1 million two precision floating numbers excluding 0. I am getting an error when I try to count the number of numbers read into the array, but for some reason I get a default value of 264 every single time I run the program. 可能要读取的数据最多为100万个两个精度浮点数(不包括0)。当我尝试计算读取到数组中的数字数时出现错误,但是由于某种原因,我得到了默认值每次我运行该程序264。 So for instance if dont input any values I get a count of 264, 1 value 265, 2 values 266 and so on. 因此,例如,如果不输入任何值,我将得到264的计数,1个值265、2个值266,依此类推。 I guess I could subtract 264 from count to get the accurate total but I want to know why this is happening and where the 264 comes from. 我想我可以从计数中减去264以得出准确的总数,但是我想知道为什么会这样以及264的来源。 I have attached the code I have so far below. 我已经附上了到目前为止的代码。 Thank you. 谢谢。
#include <stdio.h>
#include <stdlib.h>
#define N 1000000
int count_num(double numbers[]);
double sum(double numbers[]);
double max(double numbers[]);
double min(double numbers[]);
double ar_mean(double numbers[]);
double har_mean(double numbers[]);
double variance(double numbers[]);
int main(void)
{
double numbers[N];
int i =0;
while(scanf("%lf.2",&numbers[i])!=EOF&&i<N)
{
i++;
}
int count=count_num(numbers);
printf("Count: %d\n", count);
}
int count_num(double numbers[])
{
int count=0;
for(int i=0;i<N;i++)
{
if((numbers[i]!=0)&&(numbers[i]!=0.0))
{
count++;
}
}
return count;
}
Both commenters, information_interchange and AnT, are correct, the uninitialized array is the cause of the problem-it contains unknown values. 这两个注释者(information_interchange和AnT)都是正确的,未初始化的数组是问题的原因-它包含未知值。 You need to initialize the array, either by looping and setting all elements to 0.0
or by using the construct double numbers[N] = {0.0};
您需要通过循环并将所有元素设置为0.0
或使用构造double numbers[N] = {0.0};
来初始化数组double numbers[N] = {0.0};
Another one: your main
function does not return int
but has to according to ISO/IEC 9899:2011 5.1.2.2.1. 另一个:您的main
函数不会返回int
但必须根据ISO / IEC 9899:2011 5.1.2.2.1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.