C程序检查矩阵是否为奇数

给定矩阵为mat [row] [column],我们的任务是通过函数检查给定矩阵是否为奇数并显示结果。

奇异矩阵是行列式为零的矩阵,如果行列式不为零,则矩阵是非奇异的。

因此,要确定矩阵是奇异的还是非奇异的,我们需要先计算行列式。矩阵的行列式可以计算为-

$$M1 [3] [3] \:= \:\ begin {bmatrix} a&b&c \\ d&e&f \\ g&h&i \ end {bmatrix} $$

| m1 | = a(e * i-f * h)-b(d * i-f * g)+ c(d * h-e * g)

示例

Input-: mat[3][3]= { 4, 10, 1 },
   { 0, 2, 3 },
   { 1, 4, -3 }
Output-: matrix is non-singular
Input-: mat[3][3]= { 0, 0, 0 },
   { 10, 20, 30 },
   { 1, 4, -3 }
Output-: matrix is singular
Since the entire first row is 0 the determinant will be zero only

算法

Start
In function cofactor(int matrix[N][N], int matrix2[N][N], int p, int q, int n)
{
   Step 1-> Declare and initialize i = 0, j = 0, row, col
   Step 2-> Loop For row = 0 and row < n and row++
   Loop For col = 0 and col < n and col++
      If row != p && col != q then,
      Set  matrix2[i][j++] as matrix[row][col]
         If j == n – 1 then,
            Set j = 0
            Increment i by 1
         End for
      End for
In function int check_singular(int matrix[N][N], int n)
   Step 1-> Declare and initialize int D = 0;
   Step 2-> If n == 1 then,
      Return matrix[0][0]
   Step 3-> Declare matrix2[N][N], sign = 1
   Step 4-> Loop For f = 0 and f < n and f++
      Call function cofactor(matrix, matrix2, 0, f, n)
         Set D += sign * matrix[0][f] * check_singular(matrix2, n - 1)
         Set sign = -sign
      End loop
   Step 5-> Return D
In main()   Step 1-> Declare and initialize a matrix[N][N]
   Step 2-> If call check_singular(matrix, N) returns non 0 value then,
      Print "Matrix is Singular "
   Step 3-> Else
      Print "Matrix is non-Singular "
Stop

示例

#include <stdio.h>
#define N 4
//找到辅因子
int cofactor(int matrix[N][N], int matrix2[N][N], int p, int q, int n) {
   int i = 0, j = 0;
   int row, col;
   //为矩阵的每个元素循环
   for (row = 0; row < n; row++) {
      for (col = 0; col < n; col++) {
         //仅复制到临时矩阵
         //那些没有给出的元素
         //行和列
         if (row != p && col != q) {
            matrix2[i][j++] = matrix[row][col];
            //行已填充,因此增加行
            //索引并重置列索引
            if (j == n - 1) {
               j = 0;
               i++;
            }
         }
      }
   }
   return 0;
}
/* Recursive function to check if matrix[][] is singular or not. */
int check_singular(int matrix[N][N], int n) {
   int D = 0; // Initialize result
   //基本情况:如果矩阵包含单个元素
   if (n == 1)
   return matrix[0][0];
   int matrix2[N][N]; // To store cofactors
   int sign = 1; // To store sign multiplier
   //对第一行的每个元素进行迭代
   for (int f = 0; f < n; f++) {
      //获取矩阵的辅因子[0] [f]
      cofactor(matrix, matrix2, 0, f, n);
      D += sign * matrix[0][f] * check_singular(matrix2, n - 1);
      //术语应添加备用符号
      sign = -sign;
   }
   return D;
}
//驱动程序测试以上功能
int main() {
   int matrix[N][N] = { { 4, 10, 1 },
   { 0, 2, 3 },
   { 1, 4, -3 } };
   if (check_singular(matrix, N))
      printf("Matrix is Singular\n");
   else
      printf("Matrix is non-Singular\n");
   return 0;
}

输出结果

如果运行上面的代码,它将生成以下输出-

Matrix is non-Singular