矩阵c++的行列式
determinant of a matrix c++
我叫 Daniele,我是 C++
初学者。在此期间,我尝试改进指针、递归函数和动态内存分配。
出于这个原因,我编写了一个代码来查找方阵的行列式。
这段代码似乎完成了它的工作,但给我带来了一些问题。当我完成代码时,使用 5x5 矩阵进行的测试程序崩溃了;然后我将函数的 return 值和 DET 变量从 int 更改为 double。事情似乎有所好转,但当我用 5x5 身份进行测试时,程序再次崩溃,我不知道为什么。
另一个问题,我不得不更正最后一个if-else控制语句首先调用递归函数是
if (dyn_dim > 3)
{
DET = DET + coeff*funz_det (temp, (dyn_dim-1));
}
else
{
return (temp[0][0]*temp[1][1]-temp[0][1]*temp[1][0]);
}
我在网上看到过这个,但我的代码不起作用。
为什么如果我将 DET 变量作为外部变量,代码也不起作用?
最后但并非最不重要的。我以您可以看到的方式递归调用函数,但是最好在 if-else contro 语句中编写递归函数的主体还是相同?
我不想为行列式本身编写此算法,而是为涉及的技能编写此算法。
非常感谢大家
(我提前为我的英语道歉)
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
double funz_det (int **a, int dyn_dim) // dyn_dim: the dynamic dimension created for sub-matrix
{
double DET = 0;
/*Create dynamically the new sub-matrix*/
int **temp;
temp = new int*[dyn_dim-1];
for(int i=0; i<dyn_dim; i++)
{
temp[i] = new int[dyn_dim-1];
}
//////////////////////////////////////
int coeff = 0;
int BR = 0;
int BC = 0;
int row_index = 0;
int column_index = 0;
for (BC = 0; BC != dyn_dim; BC++)
{
for (int i=0; i<dyn_dim; i++)
{
for (int j=0; j<dyn_dim; j++)
{
if (i==BR && j==BC)
{
coeff = a[i][j];
int esp = i+j;
coeff = pow(-1,esp)*coeff;
row_index--;
}
if (i!=BR && j!=BC)
{
temp[row_index][column_index] = a[i][j];
column_index++;
}
}
column_index = 0;
row_index++;
}
row_index = 0;
if (dyn_dim > 3)
{
DET = DET + coeff*funz_det (temp, (dyn_dim-1));
}
else
{
DET = DET + coeff*(temp[0][0]*temp[1][1]-temp[0][1]*temp[1][0]);
}
}
return DET;
}
int main()
{
int dimension = 0;
cout << "Please insert the matrix dimension: ";
cin >> dimension;
cout << endl << endl;
int **A;
A = new int*[dimension];
for(int d=0; d<dimension; d++)
{
A[d] = new int[dimension];
}
for (int i=0; i<dimension; i++)
{
for(int j=0; j<dimension; j++)
{
cout << "Please insert the element A[" << i << "][" << j << "] = ";
cin >> A[i][j];
}
}
cout << endl << endl;
for (int display_row=0; display_row<dimension; display_row++)
{
for(int display_column=0; display_column<dimension; display_column++)
{
cout << A[display_row][display_column] << " ";
}
cout << endl << endl;
}
cout << endl << endl;
cout << "Determinant Value = " << funz_det(A,dimension) << endl;
system ("PAUSE");
return EXIT_SUCCESS;
}
您的代码崩溃是 C++ 中的一个常见问题,并且在您刚开始时经常发生。问题是您没有删除动态指针。看到你在函数开始时创建 sub-matrix 了吗?好吧,你正在用你的指针使用 'new',这当然是正确的,但是你总是应该销毁你用 'new' 创建的任何指针,使用 'delete',然后指针消失范围。如果不这样做,就会出现内存泄漏,并且 at-run-end 崩溃错误是遗忘指针的正常症状...
我叫 Daniele,我是 C++
初学者。在此期间,我尝试改进指针、递归函数和动态内存分配。
出于这个原因,我编写了一个代码来查找方阵的行列式。
这段代码似乎完成了它的工作,但给我带来了一些问题。当我完成代码时,使用 5x5 矩阵进行的测试程序崩溃了;然后我将函数的 return 值和 DET 变量从 int 更改为 double。事情似乎有所好转,但当我用 5x5 身份进行测试时,程序再次崩溃,我不知道为什么。
另一个问题,我不得不更正最后一个if-else控制语句首先调用递归函数是
if (dyn_dim > 3)
{
DET = DET + coeff*funz_det (temp, (dyn_dim-1));
}
else
{
return (temp[0][0]*temp[1][1]-temp[0][1]*temp[1][0]);
}
我在网上看到过这个,但我的代码不起作用。 为什么如果我将 DET 变量作为外部变量,代码也不起作用? 最后但并非最不重要的。我以您可以看到的方式递归调用函数,但是最好在 if-else contro 语句中编写递归函数的主体还是相同? 我不想为行列式本身编写此算法,而是为涉及的技能编写此算法。 非常感谢大家 (我提前为我的英语道歉)
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
double funz_det (int **a, int dyn_dim) // dyn_dim: the dynamic dimension created for sub-matrix
{
double DET = 0;
/*Create dynamically the new sub-matrix*/
int **temp;
temp = new int*[dyn_dim-1];
for(int i=0; i<dyn_dim; i++)
{
temp[i] = new int[dyn_dim-1];
}
//////////////////////////////////////
int coeff = 0;
int BR = 0;
int BC = 0;
int row_index = 0;
int column_index = 0;
for (BC = 0; BC != dyn_dim; BC++)
{
for (int i=0; i<dyn_dim; i++)
{
for (int j=0; j<dyn_dim; j++)
{
if (i==BR && j==BC)
{
coeff = a[i][j];
int esp = i+j;
coeff = pow(-1,esp)*coeff;
row_index--;
}
if (i!=BR && j!=BC)
{
temp[row_index][column_index] = a[i][j];
column_index++;
}
}
column_index = 0;
row_index++;
}
row_index = 0;
if (dyn_dim > 3)
{
DET = DET + coeff*funz_det (temp, (dyn_dim-1));
}
else
{
DET = DET + coeff*(temp[0][0]*temp[1][1]-temp[0][1]*temp[1][0]);
}
}
return DET;
}
int main()
{
int dimension = 0;
cout << "Please insert the matrix dimension: ";
cin >> dimension;
cout << endl << endl;
int **A;
A = new int*[dimension];
for(int d=0; d<dimension; d++)
{
A[d] = new int[dimension];
}
for (int i=0; i<dimension; i++)
{
for(int j=0; j<dimension; j++)
{
cout << "Please insert the element A[" << i << "][" << j << "] = ";
cin >> A[i][j];
}
}
cout << endl << endl;
for (int display_row=0; display_row<dimension; display_row++)
{
for(int display_column=0; display_column<dimension; display_column++)
{
cout << A[display_row][display_column] << " ";
}
cout << endl << endl;
}
cout << endl << endl;
cout << "Determinant Value = " << funz_det(A,dimension) << endl;
system ("PAUSE");
return EXIT_SUCCESS;
}
您的代码崩溃是 C++ 中的一个常见问题,并且在您刚开始时经常发生。问题是您没有删除动态指针。看到你在函数开始时创建 sub-matrix 了吗?好吧,你正在用你的指针使用 'new',这当然是正确的,但是你总是应该销毁你用 'new' 创建的任何指针,使用 'delete',然后指针消失范围。如果不这样做,就会出现内存泄漏,并且 at-run-end 崩溃错误是遗忘指针的正常症状...