C Exercise Example 38
100 cases of classic C language
Topic: seeking a 3 * 3 matrix and diagonal elements of the
Program analysis: the use of a double for loop control input two-dimensional array, then a [i] [i] after cumulative output.
Source Code:
// Created by www.w3big.com on 15/11/9. // Copyright © 2015年 本教程. All rights reserved. // #include<stdio.h> #define N 3 int main() { int i,j,a[N][N],sum=0; printf("请输入矩阵(3*3):\n"); for(i=0;i<N;i++) for(j=0;j<N;j++) scanf("%d",&a[i][j]); for(i=0;i<N;i++) sum+=a[i][i]; printf("对角线之和为:%d\n",sum); return 0; }
The above example output is:
请输入矩阵(3*3): 1 2 3 4 5 6 7 8 9 对角线之和为:15