【问题标题】:What is the error in this computation of matrix determinant?矩阵行列式的计算中的错误是什么?
【发布时间】: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&lt;4;row++) 中,您使用的是基于 1 的值,而 mat[] 是基于 0 的;更改 0..3 的循环。

标签: arrays c function pointers


【解决方案1】:

detcal4 中,在x 上的循环内,ab 设置为零,而a 在内部循环内(clm)使用a++ 递增。即使外部循环迭代,这种递增也会重复; a 永远不会重置为零。因此它会增加超出数组维度。这会导致访问数组a1a2a3a4 的代码越界,从而破坏内存。

每次启动clm 上的循环时,将a 重置为零。将int a=0;row 的循环外移到循环内。

作为一个良好的常规做法,在需要的地方声明变量。由于arow 的循环之外不需要,因此不应在该循环之外声明它,并且在内部声明它会自动避免此错误。

【讨论】:

  • 它开始运行但没有计算出正确的答案,我正在努力。 @eric
猜你喜欢
  • 2012-05-25
  • 1970-01-01
  • 2013-05-12
  • 2013-05-21
  • 1970-01-01
  • 2019-01-20
  • 2015-01-16
  • 1970-01-01
相关资源
最近更新 更多