题目:求一个3*3矩阵对角线元素之和
程序分析:利用双重for循环控制输入二维数组,再将a[i][i]累加后输出。
实例:
1 #include<stdio.h> 2 #define N 3 3 int main() 4 { 5 int i,j,a[N][N],sum=0; 6 printf("请输入矩阵(3*3): "); 7 for(i=0;i<N;i++) 8 for(j=0;j<N;j++) 9 scanf("%d",&a[i][j]); 10 for(i=0;i<N;i++) 11 sum+=a[i][i]; 12 printf("对角线之和为:%d ",sum); 13 return 0; 14 }
以上实例输出结果为:
请输入矩阵(3*3): 1 2 3 4 5 6 7 8 9 对角线之和为:15
感谢你的阅读,请用心感悟!希望可以帮到爱学习的你!!分享也是一种快乐!!!请接力。。。