程序尝试写出结果时出现分段错误
您好,
有人可以发现,当我接受所有输入时,为什么会写出这些程序分段错误? 我找不到问题出在哪里,或者应该在哪里修改我的代码
,我想得到结果吗?
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void inMatrix(int n, double **matrix)
{
int j, i;
for (i = 0; i < n; i++)
{
for (j= 0; j < n; j++)
{
scanf("%lf", &matrix[i][j]);
}
}
}
void inVector(double *vektor, int n)
{
int k;
for (k = 0; k < n; k++)
{
scanf("%lf", &vektor[k]);
}
}
void outVector(double *vektor, int n)
{
int k;
for (k = 0; k < n; k++)
{
printf("%.8lf ", vektor[k]);
}
}
void lup(int n, double **A, double **b, int v)
{
int *Permutation = (int*)malloc(sizeof(int)*n);
int i,j;
int k;
double *max = (double*) malloc (sizeof(double)*n);
int m=0, p=0;
int tmp=0, tmp2=0;
int t=0, isSingular=0;
double largestElement=0.0;
double *helpVector = (double*) malloc (sizeof(double)*n);
double *helpVectorA = (double*) malloc (sizeof(double)*n);
double *helpVectorB = (double*) malloc (sizeof(double)*n);
// for(i=0;i<n;i++)
// {
// for(j=0;j<n;j++)
// {
// A[i][j]=D[i][j];
// }
// }
for(i=0; i<n; i++)
Permutation[i]=i;
for(m=0; m<n-1; m++)
{
for(i=m; i<n; i++)
{
max[i]=fabs(A[i][m]);
}
for(i=m; i<n; i++)
{
if(max[i]>largestElement)
{
largestElement=max[i];
p=i;
}
}
for(i=0; i<n; i++)
{
helpVectorA[i]=A[m][i];
helpVectorB[i]=A[p][i];
}
for(i=0; i<n; i++)
{
A[m][i]=helpVectorB[i];
A[p][i]=helpVectorA[i];
}
tmp=Permutation[m];
tmp2=Permutation[p];
Permutation[m]=tmp2;
Permutation[p]=tmp;
if(fabs(A[m][m])>0.00000000000000001)
{
for(i=m+1; i<n; i++)
{
A[i][m]=A[i][m]/A[m][m];
for(j=m+1; j<n; j++)
{
A[i][j]=A[i][j]-A[i][m]*A[m][j];
}
}
}
if(fabs(A[m][m])<0.00000000001)
{
printf("szingularis\n");
isSingular=1;
break;
}
for(i=0; i<n; i++) max[i]=-1;
largestElement=0.0;
p=m+1;
}
if(isSingular==0)
{
if(fabs(A[n-1][n-1])<0.00000000001)
{
printf("szingularis\n");
isSingular=1;
}
}
if(isSingular==0)
{
for(k=0; k<v;k++)
{
for(i=0; i<n; i++)
{
t=Permutation[i];
helpVector[i]=b[k][t];
}
for(i=0; i<n; i++)
{
b[i][k]=helpVector[i];
}
for(i=1; i<n; i++)
{
for(j=0; j<i; j++)
{
b[k][i]-=A[i][j]*b[k][j];
}
}
for(i=n-1; i>=0; i--)
{
for(j=i+1; j<n; j++)
{
b[k][i]-=A[i][j]*b[k][j];
}
b[k][i]=b[k][i]/A[i][i];
}
}
for(i=0; i<n; i++)
{
printf("%.8lf ", b[k][i]);
}
printf("\n");
}
}
int main()
{
int k, v,n;
int j;
double **A;
double **C;
// read dimension of matrix and value
scanf("%d", &n);
//matrix
A = (double **) calloc(n, sizeof ( double*));
// matrix to store the vectors
C = (double **) calloc(n, sizeof(double *));
while(n!=0)
{
for (k = 0; k < n; k++)
{
A[k] = (double *) calloc(n, sizeof ( double));
}
inMatrix(n, A);
scanf("%d", &v);
for(k=0;k<v;k++)
{
C[k] = (double *) calloc(n, sizeof ( double));
}
for(k=0; k<v;k++)
{
for(j=0;j<n;j++)
{
scanf("%lf", &C[k][j]);
}
}
//print out result
for(k=0;k<v;k++)
{
for(j=0;j<v;j++)
{
lup(n,A,C,v);
}
}
}
return 0;
}
Greeting,
Somebody can find, why write out these program segmenation fault, when I take in the all inputs?
I don't find where is the problem, or where should be modify my code
And I would like to get the results?
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void inMatrix(int n, double **matrix)
{
int j, i;
for (i = 0; i < n; i++)
{
for (j= 0; j < n; j++)
{
scanf("%lf", &matrix[i][j]);
}
}
}
void inVector(double *vektor, int n)
{
int k;
for (k = 0; k < n; k++)
{
scanf("%lf", &vektor[k]);
}
}
void outVector(double *vektor, int n)
{
int k;
for (k = 0; k < n; k++)
{
printf("%.8lf ", vektor[k]);
}
}
void lup(int n, double **A, double **b, int v)
{
int *Permutation = (int*)malloc(sizeof(int)*n);
int i,j;
int k;
double *max = (double*) malloc (sizeof(double)*n);
int m=0, p=0;
int tmp=0, tmp2=0;
int t=0, isSingular=0;
double largestElement=0.0;
double *helpVector = (double*) malloc (sizeof(double)*n);
double *helpVectorA = (double*) malloc (sizeof(double)*n);
double *helpVectorB = (double*) malloc (sizeof(double)*n);
// for(i=0;i<n;i++)
// {
// for(j=0;j<n;j++)
// {
// A[i][j]=D[i][j];
// }
// }
for(i=0; i<n; i++)
Permutation[i]=i;
for(m=0; m<n-1; m++)
{
for(i=m; i<n; i++)
{
max[i]=fabs(A[i][m]);
}
for(i=m; i<n; i++)
{
if(max[i]>largestElement)
{
largestElement=max[i];
p=i;
}
}
for(i=0; i<n; i++)
{
helpVectorA[i]=A[m][i];
helpVectorB[i]=A[p][i];
}
for(i=0; i<n; i++)
{
A[m][i]=helpVectorB[i];
A[p][i]=helpVectorA[i];
}
tmp=Permutation[m];
tmp2=Permutation[p];
Permutation[m]=tmp2;
Permutation[p]=tmp;
if(fabs(A[m][m])>0.00000000000000001)
{
for(i=m+1; i<n; i++)
{
A[i][m]=A[i][m]/A[m][m];
for(j=m+1; j<n; j++)
{
A[i][j]=A[i][j]-A[i][m]*A[m][j];
}
}
}
if(fabs(A[m][m])<0.00000000001)
{
printf("szingularis\n");
isSingular=1;
break;
}
for(i=0; i<n; i++) max[i]=-1;
largestElement=0.0;
p=m+1;
}
if(isSingular==0)
{
if(fabs(A[n-1][n-1])<0.00000000001)
{
printf("szingularis\n");
isSingular=1;
}
}
if(isSingular==0)
{
for(k=0; k<v;k++)
{
for(i=0; i<n; i++)
{
t=Permutation[i];
helpVector[i]=b[k][t];
}
for(i=0; i<n; i++)
{
b[i][k]=helpVector[i];
}
for(i=1; i<n; i++)
{
for(j=0; j<i; j++)
{
b[k][i]-=A[i][j]*b[k][j];
}
}
for(i=n-1; i>=0; i--)
{
for(j=i+1; j<n; j++)
{
b[k][i]-=A[i][j]*b[k][j];
}
b[k][i]=b[k][i]/A[i][i];
}
}
for(i=0; i<n; i++)
{
printf("%.8lf ", b[k][i]);
}
printf("\n");
}
}
int main()
{
int k, v,n;
int j;
double **A;
double **C;
// read dimension of matrix and value
scanf("%d", &n);
//matrix
A = (double **) calloc(n, sizeof ( double*));
// matrix to store the vectors
C = (double **) calloc(n, sizeof(double *));
while(n!=0)
{
for (k = 0; k < n; k++)
{
A[k] = (double *) calloc(n, sizeof ( double));
}
inMatrix(n, A);
scanf("%d", &v);
for(k=0;k<v;k++)
{
C[k] = (double *) calloc(n, sizeof ( double));
}
for(k=0; k<v;k++)
{
for(j=0;j<n;j++)
{
scanf("%lf", &C[k][j]);
}
}
//print out result
for(k=0;k<v;k++)
{
for(j=0;j<v;j++)
{
lup(n,A,C,v);
}
}
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在
main
中,您有n
个元素v
?它从哪里来?您的意思可能是n
请记住,即使在设置
v
之前,C
就已为n
元素分配了空间。您的缩进和空白的使用也可以有所改善。
In
main
, you haven
elementsv
? Where did it come from? Do you perhaps meann
Remember
C
was allocated space forn
elements, even beforev
was set.Your indentation and use of whitespace could improve a bit too.
在函数 lup 的第 157 行(在第二个“if(isSingular==0)”块中)您编写的
这一行是在 i 上迭代的 for 循环中编写的,而 k 具有上一个循环的剩余值。该循环的中断条件是 k = v(C 的大小,又称为上面代码行中的“b”)。
所以,基本上,你写道:
这可能就是你正在寻找的臭东西。
两个注释。首先,我已经成为 .Net 人太久了,所以我可能会在这里遗漏一些完全微不足道的东西,在这种情况下,抱歉。
其次,当您希望人们阅读您的代码时(例如发布代码寻求帮助时),必须使用更有意义的名称,或者使用一些可以提高代码可读性的名称,但如果您不打算这样做,至少要避免混淆的内容就像将名为“C”的变量传递到名为“b”的参数(如果可以避免)一样。
In function lup, at line 157 (in the second "if(isSingular==0)" block) you wrote
this line is written in a for-loop iterating on i, while k has a leftover value from the last loop. The break condition of that loop was that k = v (the size of C, A.K.A "b" from the line of code above).
So, basically, you wrote:
And that's probably the stinker you're looking for.
Two notes. First, I've been .Net man for too long, so I might be missing something completely trivial here, In that case, sorry.
Second, when you want people to read your code (like when posting it for help), it's imperative to use more meaningful names, or something to improve code readability, but if you're not going to do this, at least avoid confusing stuff like passing variable named "C" into parameter named "b" if it's avoidable.
查看此教程。它教您如何使用 GDB 查找段错误。
Checkout this tutorial. It teaches you how to find segfaults using GDB.