【发布时间】:2016-12-29 16:58:08
【问题描述】:
我正在尝试编写一个程序来为我计算行列式,这就是我迄今为止所做的。但它不起作用,它只是为我扔给它的每个矩阵打印 6356918 。我什至将我的代码与互联网上的其他代码进行了比较,但没有奏效。
而且我对指针一无所知,所以我不能使用它们。我尝试了调试,但我对此也不太了解,但是第二个函数中的第一个“if”和计算行列式的代码的最后一部分似乎有问题。我在 code::blocks 中编码。
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
main()
{
int A[100][100];
int i,j,k,n,res;
printf("Enter the order of the matrix: \n");
scanf("%d",&n);
printf("\nEnter the elements of the matrix one by one: \n");
for(i = 0 ; i < n ; i++)
{
for(j = 0 ; j < n ; j++)
{
scanf("%d",&A[i][j]);
}
}
for(i = 0 ; i < n ; i++)
{
for(j = 0 ; j < n ; j++)
{
printf("%5d",A[i][j]);
}
printf("\n");
}
res = det(A,n);
printf("%d",res);
}
int det(int A[100][100], int n)
{
int Minor[100][100];
int i,j,k,c1,c2;
int determinant;
int c[100];
int O=1;
if(n == 2)
{
determinant = 0;
determinant = A[0][0]*A[1][1]-A[0][1]*A[1][0];
return determinant;
}
else
{
for(i = 0 ; i < n ; i++)
{
c1 = 0, c2 = 0;
for(j = 0 ; j < n ; j++)
{
for(k = 0 ; k < n ; k++)
{
if(j != 0 && k != i)
{
Minor[c1][c2] = A[j][k];
c2++;
if(c2>n-2)
{
c1++;
c2=0;
}
}
}
}
determinant = determinant + O*(A[0][i]*det(Minor,n-1));
O=-1*O;
}
}
return determinant;
}
【问题讨论】:
-
And BTW I don't know anything about pointers so I literally cannot use them...是什么阻止你学习它们?它们很有用。 :) -
我感觉您的输入有问题,而不是计算有问题...
-
@Sourav Ghosh 我知道,但是对于这个特殊的代码,我不允许使用它们,教授说我们只能使用我们已经知道的东西:(。
-
@Eugene 输入?怎么样?
-
你需要编译所有警告和调试信息(例如
gcc -Wall -Wextra -g,如果使用GCC...),改进代码直到你没有收到警告,你应该使用调试器 (gdb)