首頁  >  文章  >  後端開發  >  C語言中的身份矩陣程序

C語言中的身份矩陣程序

WBOY
WBOY轉載
2023-08-30 10:45:05624瀏覽

C語言中的身份矩陣程序

給定一個方陣M[r][c],其中“r”是一定數量的行,“c”是列,使得r = c,我們必須檢查“ M”是否是單位矩陣。

恆等矩陣

恆等矩陣又稱為大小為nxn方陣的單位矩陣,其中對角元素的整數值為1,非對角元素的整數值為0 p>

就像下面給定的範例-

$$I1=\begin{bmatrix}1 \end{bmatrix},\ I2=\begin{bmatrix}1 & 0 \0 & 1 \end{bmatrix},\ I3=\begin{bmatrix}1 &0 & 0 \0 &1 & 0 \0 &0 &1 \end{bmatrix},\In=\begin{bmatrix}

1 &0 &0  &...&0 \

0 &1 &0  &...&0\

0 &0 &1  &...&0\

。 &. &. &...&.\

。 &. &. &...&.\

0 &0 &0  &...&1\

#\end{bmatrix} $$

範例

Input: m[3][3] = { {1, 0, 0},
   {0, 1, 0},
   {0, 0, 1}}
Output: yes
Input: m[3][3] == { {3, 0, 1},
   {6, 2, 0},
   {7, 5, 3} }
Output: no

演算法

Start
Step 1 -> declare function for finding identity matrix
   int identity(int num)
      declare int row, col
      Loop For row = 0 and row < num and row++
         Loop For col = 0 and col < num and col++
            IF (row = col)
               Print 1
            Else
               Print 0
      End
   End
Step 2 -> In main()
   Declare int size = 4
   Call identity(size)
Stop

範例

#include<stdio.h>
int identity(int num){
   int row, col;
   for (row = 0; row < num; row++){
      for (col = 0; col < num; col++){
         if (row == col)
            printf("%d ", 1);
         else
            printf("%d ", 0);
      }
      printf("</p><p>");
   }
   return 0;
}
int main(){
   int size = 4;
   identity(size);
   return 0;
}

輸出

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

以上是C語言中的身份矩陣程序的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除