행렬식 값 구하기 (귀속)

10759 단어
/*
#include 
#include 
#define N 100
#define LIM -100000000
float det(float a[N][N],int n){
    if(n==1)
        return a[0][0];
    if(n==2)
        return a[0][0]*a[1][1]-a[0][1]*a[1][0];// the base situation
    else{
        int j,i,flag=1;
        float ret=0;
        for(j=0;j*/

#include 
#include 
 
#define  CONST 1e-6
#define  SIZE 20
 
void InputMatrix (double a[][SIZE], int n);
double DeterminantValue(double a[][SIZE], int n);
void SubMatrix(double a[][SIZE], double b[][SIZE], int n, int row, int col);
void PrintMatrix(double a[][SIZE], int n);
int main(void)
{
    double  a[SIZE][SIZE];
    int     n;
    double  result;
    printf("Please enter matrix size n(1<=n", SIZE);
    scanf("%d", &n);
    printf("Please input matrix line by line:
"); InputMatrix(a, n); printf("matrix a:
"); PrintMatrix(a, n); printf("
"); result = DeterminantValue(a, n); printf("result = %f
", result); return 0; } // : n×n void InputMatrix (double a[][SIZE], int n) { int i, j; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { scanf("%lf", &a[i][j]); } } } // : n×n double DeterminantValue(double a[][SIZE], int n) { int i = 0, j = 0; double temp, result, b[SIZE][SIZE]; if (n == 1) { result = a[0][0]; } else if (n == 2) { result = a[0][0] * a[1][1] - a[0][1] * a[1][0]; } else { result = 0.0; for (j = 0; j < n; j++)// { SubMatrix(a, b, n, i, j);//the key printf("Submatrix:
"); PrintMatrix(b, n - 1); temp = DeterminantValue(b, n - 1); // result += pow(-1, i + j) * a[0][j] * temp; printf("DValue of the Submatrix is %6.1f
", temp); } } return result; } // : n×n a row col (n-1)×(n-1) b void SubMatrix(double a[][SIZE], double b[][SIZE], int n, int row, int col) { int i, j, ii = 0, jj = 0; for (i = 0; i < n; i++) { jj = 0;// 0 for (j = 0; j < n; j++)// j { if (i != row && j != col)// i j { b[ii][jj] = a[i][j]; jj++; } } if (i != row && j != col)// { ii++;//////////////////////////////////////////////////////// } /////// i!=row&&... row,col ///////// } //////////////////////////////////////////////////////// } // : n×n void PrintMatrix(double a[][SIZE], int n) { int i, j; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { printf("%6.1f\t", a[i][j]); } printf("
"); } }

 
다음으로 전송:https://www.cnblogs.com/xzenith/p/3702688.html

좋은 웹페이지 즐겨찾기