Korean, Edit

Calculation of Determinant in C Language

Higher category : 【C Language】 C Language Index


a. GitHub



#include <stdio.h>
#include <stdlib.h>
/* This source is for finding determinant value by using minor determinant expansion */

int det(int n, int (*Matrix)[n]){
	if(n == 1) return Matrix[0][0];
	
    int i, j, k;
	int minor_Matrix[n][n-1][n-1];
	for(k = 0; k < n; k++){
		for(i = 0; i < n - 1; i++)
			for(j = 0; j < n; j++){
				if(j < k)
					minor_Matrix[k][i][j] = Matrix[i + 1][j];
				else if(j > k)
					minor_Matrix[k][i][j - 1] = Matrix[i + 1][j];
			}
	}
	int sum = 0;
	int test = 1;
	for(k = 0; k < n; k++){
		sum += test * Matrix[0][k] * det(n - 1, minor_Matrix[k]);
		test *= -1;
	}
	return sum;
}

int main(void) {
	int n, i, j; scanf("%d", &n);
	int Matrix[n][n];
	for(i = 0; i < n; i++)
		for(j = 0; j < n; j++)
			scanf("%d", &Matrix[i][j]);
	printf("%d", det(n, Matrix));
	return 0;
}


Input: 2016.02.24 17:35

results matching ""

    No results matching ""