C语言中的身份矩阵程序
给定一个方阵M[r][c],其中“r”是一些行,而“c”是列,使得r=c,我们必须检查“M”是否为单位矩阵。
身份矩阵
单位矩阵也称为大小为nxn方阵的单位矩阵,其中对角元素将仅具有整数值1,非对角元素将仅具有整数值0
像下面给定的示例一样-
$$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("\n");
}
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