在C上编写一个程序,用于创建和显示整数矩阵a [4] [4]。矩阵是使用索引形成的。矩阵逐行显示,有人可以帮我吗?
这应该是它的样子 enter image description here
答案 0 :(得分:1)
#include <stdio.h>
int main()
{
int n = 4;
int a[n][n];
//creating
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i==j)
a[i][j] = 1;
else
a[i][j] = 0;
}
}
//printing
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
return 0;
}
答案 1 :(得分:0)
您也可以这样声明矩阵:
#include <stdio.h>
int main()
{
int a[4][4] = {{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1}};
for(int i=0;i<4;i++) {
for(int j=0;j<4;j++) printf("%d ", a[i][j]);
printf("\n");
}
return 0;
}
答案 2 :(得分:0)
您也可以用零填充数组,并在必要时粘贴1:
service apache2 restart