Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.4 kB
1
Indexable
Never
#include <stdio.h>
#include <malloc.h>
#include <conio.h>

int** getNewMatrix(int ** matrix_p, int size, int exc_j) {
	int** new_matrix_p, i, j, add_j = 0;
	new_matrix_p = (int*)malloc((size - 1) * sizeof(int));

	for (i = 1; i < size; i++) {
		new_matrix_p[i - 1] = (int*)malloc((size - 1) * sizeof(int));
		for (j = 0; j < size; j++) {
			if (j == exc_j) {
				add_j = 1;
				continue;
			}

			new_matrix_p[i - 1][j - add_j] = matrix_p[i][j];
		}

		add_j = 0;
	}

	return new_matrix_p;
}

int getDefinition(int** matrix_p, int size) {
	if (size == 2) {
		return matrix_p[0][0] * matrix_p[1][1] - matrix_p[0][1] * matrix_p[1][0];
	}

	int result = 0, sign = 1, i;

	for (i = 0; i < size; i++) {
		result += sign * matrix_p[0][i] * getDefinition(getNewMatrix(matrix_p, size, i), size-1);
		sign *= -1;
	}

	return result;
}

int main() {
	int size;
	printf("Enter the size of matrix: ");
	scanf_s("%i", &size);
	
	int** matrix_p, i, j;
	matrix_p = (int*)malloc(size * sizeof(int));

	for (i = 0; i < size; i++) {
		matrix_p[i] = (int*)malloc(size * sizeof(int));
		for (j = 0; j < size; j++) {
			printf("Enter the number (%i, %i): ", i, j);
			scanf_s("%i", &matrix_p[i][j]);
		}
	}

	printf("\n");
	printf("Definition: %i", getDefinition(matrix_p, size));
	printf("\n");

	return 0;
}