我正在尝试创建一个转置矩阵(Adjugate矩阵)的函数。我尝试了不同的东西,但它仍然崩溃。你有什么想法可能是什么问题吗?
P.S我用adjoint(a, b);
P.S.S整个程序的目的是创建一个矩阵的逆矩阵。
已编辑的代码 (显示可能导致程序崩溃的代码和指向函数的更大部分)
#include <stdio.h>
#include <stdlib.h>
#define SIZE 3 //defining the size of the matrix (3x3)
#define SIZE2 2 //defining the size of the matrix (2x2)
//prototyping the functions used to calculate the inverse of the matrix
void readMatrix(double a[SIZE][SIZE]);
void printMatrix(double a[SIZE][SIZE]);
void printMinorMatrix(double b[SIZE2][SIZE2]);
void selecting(double a[SIZE][SIZE], double b[SIZE2][SIZE2]);
double calculating(double a[SIZE][SIZE],double m[8]);
void anArray(double m[8]);
double convert(double m[8], double [SIZE][SIZE]);
double determ(double m[8], double n[SIZE][SIZE]);
double adjoint(double a[SIZE][SIZE],double b[SIZE][SIZE]);
double multiplyMatrix(double a[SIZE][SIZE], double b[SIZE][SIZE], double
result[SIZE][SIZE]);
int main()
{
double a[SIZE][SIZE];
double b[SIZE2][SIZE2];
double m[8];
double n[SIZE][SIZE];
double d;
double q[SIZE][SIZE];
int i,j,k;
printf("Adjoint of the Matrix:\n");
printf("_________________________________________\n\n");
adjoint(a,b);
printf("\n\n");
printf("Scalar Multiplication:\n");
printf("_________________________________________\n\n");
multiplyMatrix(i,j,k);
printf("\n\n");
return 0;
}
//Reading a 3x3 Matrix
void readMatrix(double a[SIZE][SIZE])
//Printing a 3x3 Matrix
void printMatrix(double a[SIZE][SIZE])
//Printing a 2x2 Matrix
void printMinorMatrix(double b[SIZE2][SIZE2])
//Selecting a 2x2 Matrix from a 3x3 Matrix
void selecting(double a[SIZE][SIZE],double b[SIZE2][SIZE2])
//Calculating the determinant of a 2x2 matrix
double calculating(double a[SIZE][SIZE], double m[8])
//Printing an Array of Length 9
void anArray(double m[8])
//Calculating the determinant of a 3x3 matrix
double determ(double m[8], double n[SIZE][SIZE])
//Converting an Array into a Matrix
double convert(double m[8], double K[3][3])
//Transposing a Matrix
double adjoint(double a[SIZE][SIZE],double b[SIZE][SIZE])
{
int i,j;
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
if(i!=j&&i<j)
{
b[i][j]=a[j][i];
}
else b[i][j]= a[i][j];
}
return b[SIZE][SIZE];
}
//Scalar multiplication
double multiplyMatrix(double a[SIZE][SIZE], double b[SIZE][SIZE], double result[SIZE][SIZE])
{
int i, j, k;
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
{
result[i][j]=0.0;
for(k=0;k<SIZE;k++){
result[i][j] += a[i][k]*b[k][j];
}
}
}
}
答案 0 :(得分:2)
问题是
return b[SIZE][SIZE];
您正在返回不存在的值。最后的入境值是
return b[SIZE-1][SIZE-1];
查看整个代码,在执行代码之前,您会收到很多警告......
提到问题的主要原因是。
b传递给adjoint
函数的矩阵是 2x2矩阵,而不是 3x3矩阵。
在主要宣布
double a[SIZE][SIZE];
double b[SIZE2][SIZE2];
,其中
#define SIZE 3 //defining the size of the matrix (3x3)
#define SIZE2 2 //defining the size of the matrix (2x2)