((C ++)函数来计算动态矩阵的行列式]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了((C ++)函数来计算动态矩阵的行列式]相关的知识,希望对你有一定的参考价值。
我目前正在开发一个程序,该程序应该向用户询问一些数据并根据这些数据创建2个矩阵。稍后在程序上也应该可以选择为这些矩阵计算行列式。我想在函数(int行列式)中完成此操作,但是当我尝试将其用于矩阵3x3或更大尺寸时,程序崩溃并显示错误(进程返回-1073741819(0xC0000005)执行时间:23.503 s)
可能是什么原因?
#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <time.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
using namespace std;
void inputdata (int&,int&,int&,int&,int&,int&);
void printmatrix (int**,int&,int&);
int determinant(int**, int);
bool ifmatsquare (int, int);
int main()
{
int rows1=0,rows2=0,colm1=0,colm2=0,matmin=0,matmax=0;
char menuoption;
while (true){
inputdata(rows1,rows2,colm1,colm2,matmin,matmax);
system("cls");
int **matrix1(0);
int **matrix2(0);
matrix1 = new int *[rows1];
matrix2 = new int *[rows2];
srand(time(NULL));
for(int i = 0; i <rows1; i++){
matrix1[i] = new int[colm1];
for(int j = 0; j <colm1; j++){
matrix1[i][j] = rand()%(matmax-matmin+1) + matmin;;
}
}
for(int i = 0; i <rows2; i++){
matrix2[i] = new int[colm2];
for(int j = 0; j <colm2; j++){
matrix2[i][j] = rand()%(matmax-matmin+1) + matmin;;
}
}
for (bool menu=true; menu==true;){
printmatrix(matrix1, rows1, colm1);
cout<< endl;
printmatrix(matrix2, rows2, colm2);
cout<< endl;
cout<< endl;
cout<< "MENU
" <<endl;
cout<< "1: XXX" <<endl;
cout<< "2: XXX" <<endl;
cout<< "3: Calculate Determinant" <<endl;
cout<< "4: XXX" <<endl;
cout<< "5: Reset program" <<endl;
cout<< "6: Exit program" <<endl;
cout<< endl;
menuoption = getch();
switch(menuoption){
/*case '1':
//jakis kod
break;
*/
case '3':
char mat1or2;
cout<< "Matrix 1 or 2: ";
do {
mat1or2 = getch();
} while ((mat1or2!='1') && (mat1or2!='2'));
if ((mat1or2=='1')&&(ifmatsquare(rows1,colm1)==true))
cout<<"
Determinant for matrix 1 is "<<determinant(matrix1, rows1)<<endl;
else if ((mat1or2=='2')&&(ifmatsquare(rows2,colm2)==true)){
cout<<"
Determinant for matrix 2 is "<<determinant(matrix2, rows2)<<endl;
}
cout<< "Press any key to return to menu";
getch();
break;
case '5':
menu=false;
break;
case '6':
delete []matrix1;
delete []matrix2;
exit(0);
break;
default:
cout<< "No such option, press any key to reurn to menu!"<<endl;
getch();
break;
}
system("cls");
}
delete []matrix1;
delete []matrix2;
}
return 0;
}
void inputdata (int &r1,int &r2,int &c1,int &c2,int &mmin, int &mmax){
cout << "Give number of rows for matrix 1" << endl;
cin >> r1;
cout << "Give number of columns for matrix 1" << endl;
cin >> c1;
do{
cout << "Give number of rows for matrix 2 (should be the same as columns for matrix 1)" << endl;
cin >> r2;
} while(r2!=c1);
cout << "Give number of columns for matrix 2" << endl;
cin >> c2;
cout << "Give MIN value for matrix" << endl;
cin >> mmin;
do{
cout << "Give MAX value for matrix" << endl;
cin >> mmax;
} while(mmax<mmin);
}
void printmatrix(int **a, int &rows, int &colm){
for (int x = 0; x < rows; x++)
{
for (int y = 0; y < colm; y++)
{
printf("%4d", a[x][y]);
}
printf("
");
}
}
bool ifmatsquare(int row, int col){
if (row == col){
cout<<endl;
return true;}
else
cout<<"Determinant can be caclucated only for square matrix"<<endl;
return false;
}
int determinant(int **a, int size) {
int det = 0;
int **submatrix(0);
if (size == 2)
return ((a[0][0] * a[1][1]) - (a[1][0] * a[0][1]));
else {
for (int x = 0; x < size; x++) {
int subi = 0;
for (int i = 1; i < size; i++) {
int subj = 0;
for (int j = 0; j < size; j++) {
if (j == x)
continue;
submatrix[subi][subj] = a[i][j];
subj++;
}
subi++;
}
det = det + (pow(-1, x) * a[0][x] * determinant(submatrix, size - 1));
}
}
return det;
}
答案
谢谢您的帮助。
您是对的,我没有为子矩阵声明空格
我修改了函数,现在看起来像下面的样子(并按预期工作)
int determinant(int **a, int matsize) {
int det = 0;
int** submatrix = new int*[matsize];
for (int i = 0; i < matsize; ++i)
submatrix[i] = new int[matsize];
if (matsize == 1)
return a[0][0];
else if (matsize == 2)
return ((a[0][0] * a[1][1]) - (a[1][0] * a[0][1]));
else {
for (int x = 0; x < matsize; x++) {
int subi = 0;
for (int i = 1; i < matsize; i++) {
int subj = 0;
for (int j = 0; j < matsize; j++) {
if (j == x)
continue;
submatrix[subi][subj] = a[i][j];
subj++;
}
subi++;
}
det = det + (pow(-1, x) * a[0][x] * determinant(submatrix, matsize - 1));
}
}
for (int i = 0; i < matsize; ++i)
delete [] submatrix[i];
delete [] submatrix;
return det;
}
以上是关于((C ++)函数来计算动态矩阵的行列式]的主要内容,如果未能解决你的问题,请参考以下文章