• Предмет: Информатика
  • Автор: ninjazhanibek
  • Вопрос задан 3 месяца назад

Дам корону і 100 балів
Необхідно написати код в Си (не С++ бо за С++ від мене жодной вдячністі), в якому потібно ввести квадратну матрицю і знайти детерминант. Бажано використовуваючі рекурсівний метод але можно і методом Гаусса. Головне не в С++

Ответы

Ответ дал: itop26top
3

Ответ:

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

#define N 10

void getCofactor(double matrix[N][N], double temp[N][N], int p, int q, int n) {

   int i = 0, j = 0;

   for (int row = 0; row < n; row++) {

       for (int col = 0; col < n; col++) {

           if (row != p && col != q) {

               temp[i][j++] = matrix[row][col];

               if (j == n - 1) {

                   j = 0;

                   i++;

               }

           }

       }

   }

}

double determinant(double matrix[N][N], int n) {

   double det = 0;

   if (n == 1) {

       return matrix[0][0];

   } else if (n == 2) {

       return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];

   }

   double temp[N][N];

   int sign = 1;

   for (int f = 0; f < n; f++) {

       getCofactor(matrix, temp, 0, f, n);

       det += sign * matrix[0][f] * determinant(temp, n - 1);

       sign = -sign;

   }

   return det;

}

int main() {

   int n;

   printf("Enter the size of the square matrix (max 10): ");

   scanf("%d", &n);

   double matrix[N][N];

   printf("Enter the elements of the square matrix:\n");

   for(int i = 0; i < n; i++) {

       for(int j = 0; j < n; j++) {

           scanf("%lf", &matrix[i][j]);

       }

   }

   double det = determinant(matrix, n);

   printf("Determinant of the matrix is: %lf\n", det);

   return 0;

}

Объяснение:

Удачи)


itop26top: Спасибо
itop26top: Если надо - спрашивай
ninjazhanibek: Корона будет спустя суток , хоть ща бы дал но тут правила что нужно ждать 24ч
itop26top: корона это лучший ответ?
itop26top: или ответ проверенный экспертом?
ninjazhanibek: Да
itop26top: Понял, спасибо
Вас заинтересует