求一个3x3的整型矩阵对角线元素之和
#include<stdio.h>
int main()
{
int arr1[3][3] = { 0 };
int arr2[3][3] = { 0 };
int i = 0, j = 0;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
scanf("%d", &arr1[i][j]);
}
}
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
arr2[i][j]=arr1[j][i];
}
}
int sum = 0;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
if (i == j)
sum = arr1[i][j] + sum;
}
}
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
if (i == j)
sum = arr2[i][j] + sum;
}
}
sum = sum - arr1[1][1];
printf("%d\n",sum);
return 0;
}
输出结果: