【发布时间】:2021-12-25 07:44:44
【问题描述】:
我试图找到 4x4 矩阵的行列式,它被编译,获取矩阵的值,然后突然终止,显示一些未知错误导致它停止工作。
#include <stdio.h>
int detcalfnl(int a,int b,int c,int d)
{
int anss;
anss = ((a*d)-(b*c));
return anss;
}
/*********************************************************************************************************************************************************/
void detcalthree(int mat[3][3],int *ansss)
{
*ansss=0;
int x;
for(x=0;x<3;x++)
{
if(x==0)
{
*ansss+=((mat[0][x])*(detcalfnl(mat[x+1][x+1],mat[x+1][x+2],mat[x+2][x+1],mat[x+2][x+2])));
}
else if(x==1)
{
*ansss+=((-1)*((mat[0][x])*(detcalfnl(mat[x][x-1],mat[x][x+1],mat[x+1][x-1],mat[x+1][x+1]))));
}
else if(x==2)
{
*ansss+=((mat[0][x])*(detcalfnl(mat[x-1][x-2],mat[x-1][x-1],mat[x][x-2],mat[x][x-1])));
}
}
}
/*********************************************************************************************************************************************************/
void detcalfour(int mat[4][4],int *ansss)
{
*ansss=0;
int a1[3][3],a2[3][3],a3[3][3],a4[3][3];
int x,row,clm,a5,a6,a7,a8;
int *p5=&a5;
int *p6=&a6;
int *p7=&a7;
int *p8=&a8;
for(x=0;x<4;x++)
{
int a=0;
int b=0;
for(row=1;row<4;row++)
{
for(clm=0;clm<4;clm++)
{
if(clm==x){continue;}
else
{
if(x==0)
{
a1[b][a]=mat[row][clm];
}
else if(x==1)
{
a2[b][a]=mat[row][clm];
}
else if(x==2)
{
a3[b][a]=mat[row][clm];
}
else if(x==3)
{
a4[b][a]=mat[row][clm];
}
}
a++;
}
b++;
}
}
detcalthree(a1,p5);
detcalthree(a2,p6);
detcalthree(a3,p7);
detcalthree(a4,p8);
*ansss=((mat[0][0])*(*p5))-((mat[0][1])*(*p6))+((mat[0][2])*(*p7))-((mat[0][3])*(*p8));
}
/*********************************************************************************************************************************************************/
int main()
{
int dim,row,clm,fnlans;
printf("Please Enter The Dimension Of Matrix :");
scanf("%d",&dim);
printf("\n\n\n");
int oprnd[dim][dim];
for(row=0;row<dim;row++)
{
for(clm=0;clm<dim;clm++)
{
printf("\nPlease Provide Element For Row %d and Column %d : ",row+1,clm+1);
scanf("%d",&oprnd[row][clm]);
}
}
int *pfs=&fnlans;
detcalfour(oprnd,pfs);
printf("\n\n\nValue Of Determinant Is %d \n\n",*pfs);
return 0;
}
【问题讨论】:
-
你应该写一个清晰的标题,重点放在问题上。并且您应该学习如何调试,因为问题发生在程序执行期间。同样在
for(row=1;row<4;row++)中,您使用的是基于 1 的值,而 mat[] 是基于 0 的;更改 0..3 的循环。
标签: arrays c function pointers