gpt4 book ai didi

c - 错误计算均值和标准差

转载 作者:太空宇宙 更新时间:2023-11-04 04:45:23 24 4
gpt4 key购买 nike

我正在尝试使用以下方法计算均值和标准差代码段。我没有收到任何编译错误。当我使用 .txt 文件运行它时,输出出现以下方式:

"
"is not a valid integer

求和、均值和标准差错误,而且无法输入 double 值

int main ( int argc, char *argv[] )
{

if(buffer == NULL)
{

}

double result = fread(buffer,1,lSize,file);
if(result != lSize){

}
else{

char *str = strtok(buffer," ,");

int count = 0;
while(str != NULL){
double val = (int) strtol(str,&str,10);

if(*str != ' ' && *str != '\0')
{


}
else{

if(count == 0)
{

root = malloc(sizeof(struct node));
root->next = NULL;
root->value = val;
current = root;
count++;

}
else{

double x = createNode(current,val);
}
}

str = strtok(NULL," ,");
}

}
current = root;
printf("Sum is %lf and link count is %lf\n",
getSummation(current), getNodeCount(current));

double mean = getMean(getSummation(current),getNodeCount(current));
double stdev = getStandardDeviation(root, mean,getNodeCount(current));



fclose( file );
free(buffer);
}
}
return 0;
}


int createNode(struct node *n,double val)
{
if(n != NULL){
while(n->next != NULL)
{
n = n->next;
}
n->next = malloc(sizeof(struct node));
n = n -> next;
n -> value = val;
n -> next = NULL;
}
return 1;
}

int getSummation(struct node *element){
double sum = 0.0f;
if(element != NULL)
{
while(element != NULL)
{
sum = sum + element->value;
element= element->next;
}
}
else
{
printf("Null Point Exception thrown");
}
return sum;
}

int getNodeCount(struct node *link)
{
int count = 0;

while(link != NULL)
{

}

return count;
}

double getMean(double summation,int numberOfNodes)
{

}

double getStandardDeviation(struct node *link, double mean,int linkCount)
{


while(link != NULL)
{

}

stdev = sqrt((diffrenceSummation)/(linkCount - 1));
return stdev;
}

最佳答案

1) 在main()

之前添加原型(prototype)
//double getSummation(struct node*element);
int getSummation(struct node *element);
int createNode(struct node *n,double val);
int getNodeCount(struct node *link);
#include <math.h>

2) 使用一致的格式说明符

  //printf("Summation is %lf and link count is %lf\n", 
// getSummation(current), getNodeCount(current));
printf("Summation is %d and link count is %d\n",
getSummation(current), getNodeCount(current));

3)各种铸件有问题

// mean = (double)summation/(double)numberOfNodes;
mean = summation/numberOfNodes;
// double linkValue = (double) link->value;
double linkValue = link->value;
// buffer = (char*) malloc(sizeof(char)*lSize);
buffer = malloc(lSize);
// double val = (int) strtol(str,&str,10);
double val = strtol(str,&str,10);

关于c - 错误计算均值和标准差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21634649/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com