高斯消除期间分段错误(核心转储)
在运行该程序期间,我遇到了分段错误。我正在尝试使用从 .dat 文件接收的数字来求解线性方程。我可以在高斯函数之前到达该部分,但它会在之后转储。有什么帮助吗?谢谢。
void prob2(void)
{
FILE *matrix;
matrix=fopen("matrix.dat", "r");
double a1, a2, a3, a4, **A, *a, *rhs, length;
int k, row;
A=(double **)malloc(4*sizeof(double *));
printf("\n");
for(k=1;k<=4; k++)
{
a=(double *)malloc(4*sizeof(double));
fscanf(matrix, "%lf %lf %lf %lf", &a1, &a2, &a3, &a4);
a[0]=a1;
a[1]=a2;
a[2]=a3;
a[3]=a4;
printf(" a[%d][] = %5.2f %5.2f %5.2f %5.2f\n", k, a[0], a[1], a[2], a[3]);
}
rhs=(double *)malloc(4*sizeof(double));
fscanf(matrix, "%lf %lf %lf %lf", &a1, &a2, &a3, &a4);
rhs[0]=a1;
rhs[1]=a2;
rhs[2]=a3;
rhs[3]=a4;
printf("\nb[]={ %.3f, %.3f, %.3f, %.3f }\n", rhs[0],rhs[1],rhs[2],rhs[3]);
printf("hiii");
gauss(4, A, rhs);
/* print the solution x[] stored in rhs[]:*/
printf("x[]={%7.3f, %7.3f, %7.3f, %7.3f }\n",
rhs[0], rhs[1], rhs[2], rhs[3]);
length=sqrt(pow(rhs[0],2)+pow(rhs[1],2)+pow(rhs[2],2)+pow(rhs[3],2));
printf("The length of x[] is %.6lf", length);
/* free memory */
for(row=0; row<4; row++) free(A[row]);
free(A);
free(rhs);
fclose(matrix);
}
void gauss(int n, double **A, double *rhs)
{
/* By Gauss elimination, solve a system of equations:
A[][]*x[] = rhs[] where A[][] (n x n)
and rhs[] (n x 1) are input */
/* x[] is stored in rhs[] */
printf("hiii");
double one = 1.0, zero=0.0;
double b, c, d;
int nm, row, col, krow;
nm = n - 1;
if(n == 1)
{
rhs[0] /= A[0][0]; A[0][0] = one;
return;
}
/* forward reduction */
for(row=0; row<nm; row++)
{
b = A[row][row]; A[row][row] = one;
for(col=row+1; col<n; col++) A[row][col] /= b;
rhs[row] /= b;
/* sweep rows of A[row+1][] to A[nm][] */
for(krow=row+1; krow<n; krow++)
{
c = A[krow][row]; A[krow][row] = zero;
for(col=row+1; col<n; col++) A[krow][col] -= c * A[row][col];
rhs[krow] -= c * rhs[row];
}
}
/* back substitution */
rhs[nm] /=A[nm][nm];
for(row=nm-1; row>=0; row--)
{
for(col=row+1; col<n; col++) rhs[row] -= A[row][col]*rhs[col];
}
return;
}
During the running of this program, i get a segmentation fault. Im trying to solve a linear equation, with numbers received from a .dat file. I can get to the part right before the gauss function, but it dumps afterwards. Any help? thanks.
void prob2(void)
{
FILE *matrix;
matrix=fopen("matrix.dat", "r");
double a1, a2, a3, a4, **A, *a, *rhs, length;
int k, row;
A=(double **)malloc(4*sizeof(double *));
printf("\n");
for(k=1;k<=4; k++)
{
a=(double *)malloc(4*sizeof(double));
fscanf(matrix, "%lf %lf %lf %lf", &a1, &a2, &a3, &a4);
a[0]=a1;
a[1]=a2;
a[2]=a3;
a[3]=a4;
printf(" a[%d][] = %5.2f %5.2f %5.2f %5.2f\n", k, a[0], a[1], a[2], a[3]);
}
rhs=(double *)malloc(4*sizeof(double));
fscanf(matrix, "%lf %lf %lf %lf", &a1, &a2, &a3, &a4);
rhs[0]=a1;
rhs[1]=a2;
rhs[2]=a3;
rhs[3]=a4;
printf("\nb[]={ %.3f, %.3f, %.3f, %.3f }\n", rhs[0],rhs[1],rhs[2],rhs[3]);
printf("hiii");
gauss(4, A, rhs);
/* print the solution x[] stored in rhs[]:*/
printf("x[]={%7.3f, %7.3f, %7.3f, %7.3f }\n",
rhs[0], rhs[1], rhs[2], rhs[3]);
length=sqrt(pow(rhs[0],2)+pow(rhs[1],2)+pow(rhs[2],2)+pow(rhs[3],2));
printf("The length of x[] is %.6lf", length);
/* free memory */
for(row=0; row<4; row++) free(A[row]);
free(A);
free(rhs);
fclose(matrix);
}
void gauss(int n, double **A, double *rhs)
{
/* By Gauss elimination, solve a system of equations:
A[][]*x[] = rhs[] where A[][] (n x n)
and rhs[] (n x 1) are input */
/* x[] is stored in rhs[] */
printf("hiii");
double one = 1.0, zero=0.0;
double b, c, d;
int nm, row, col, krow;
nm = n - 1;
if(n == 1)
{
rhs[0] /= A[0][0]; A[0][0] = one;
return;
}
/* forward reduction */
for(row=0; row<nm; row++)
{
b = A[row][row]; A[row][row] = one;
for(col=row+1; col<n; col++) A[row][col] /= b;
rhs[row] /= b;
/* sweep rows of A[row+1][] to A[nm][] */
for(krow=row+1; krow<n; krow++)
{
c = A[krow][row]; A[krow][row] = zero;
for(col=row+1; col<n; col++) A[krow][col] -= c * A[row][col];
rhs[krow] -= c * rhs[row];
}
}
/* back substitution */
rhs[nm] /=A[nm][nm];
for(row=nm-1; row>=0; row--)
{
for(col=row+1; col<n; col++) rhs[row] -= A[row][col]*rhs[col];
}
return;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来矩阵
A
未分配。我认为分配a
后你需要这个:It looks like the matrix
A
is not assigned. I think you need this after allocatinga
:当算法在对角线上遇到 0 时,通常会发生这种情况。您需要使用旋转来避免这种情况。有关详细信息,请参阅有关高斯消除的维基百科文章。
This usually happens when the algorithm encounters a 0 on the diagonal. You need to use pivoting to avoid this. See the Wikipedia article on Gaussian elimination for details.
你永远不会在 A 中存储任何内容。你在 k = 1 ... 4 循环中进行 malloc,但你永远不会保存你 malloc 的内容。
You never store anything in A. You malloc in your k = 1 ... 4 loop but you never save what you malloc.