sinat_26873755 2015-04-01 11:07 采纳率: 0%
浏览 1526

始终出现 segmentation fault, 不知道问题在哪? 求大神指教。

int main()
{
FILE *fp;
double d;
double mean1;
double mean2;
double std1, std2;

if((fp = fopen("Table_1.txt", "rb")) == NULL)
{
    return -1;
}
while (fscanf(fp, "%lf", &d)==1)
{
    Add(d);
}
fclose(fp);

----------读取文件结束后,开始计算2个数值,mean和std----------------//
mean1 = count_mean(0);
mean2 = count_mean(1);//这里获取的数值不知道为什么不正确,测试时候发现,计算不了文件第二行的第一个数字'15.0'

//---------------这里是我读取的表---------------------------------
160 591 114 229 230 270 128 1657 624 1503
15.0 69.9 6.5 22.4 28.4 65.9 19.4 198.7 38.8 138.2
//--------------------------------------------------------------
std1 = count_std(mean1, 1);//segmentation就是报错这里,始终不知道问题是哪,并且计算出的结果也不对。
std2 = count_std(mean2, 2);

printf("Mean                         Std\n");
printf("%lf                   %lf\n", mean1, mean2);
printf("%lf                   %lf\n", mean2, std2);

return 0;

}
//以下是我的计算std的方法
double count_std(double mean, int column)
{
double std;
int count = 0;
double temp;
if(column = 1)
{
Reverse();
while(count < 10)
{
temp = temp + pow(mean - head -> data,2);
//printf("%lf\n", temp);
head = head -> next;
count++;
}
std = sqrt(temp/9);

}
else
{
while(count < 10)
{
temp = temp + pow(mean - head -> data,2);
head = head -> next;
count++;
}
std = sqrt(temp/9);
}
return std;
}
//以下是我计算mean的方法
double count_mean(int k)
{
double temp;
int count;
double mean;
int len = 10;
//
if(k == 0)
{
Reverse();
count = 0;
while(count < 10)
{
temp = temp + head -> data;
head = head -> next;
count++;
}
mean = temp/len;
}
else if(k == 1)
{
count = 0;
while(count < 10)
{
temp = temp + head -> data;
head = head -> next;
count++;
}
mean = temp/len;
}
return mean;

}

//---------这是一个链表结构的(double data, *next),添加方法---
void Add(double x)
{
struct Node *temp = malloc(sizeof(struct Node));
temp -> data = x;
temp -> next = head;
head = temp;
}

  • 写回答

3条回答 默认 最新

  • shuizhilei3334 2015-04-01 12:00
    关注

    你是否保留了链表的头指针啊
    如果head是指向链表头的指针,那么计算两个mean之后head指向了链表尾部。
    如果再reverse()的话,当然会出现segmentation fault.
    并且,你的temp需要初始化为0.

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?