编写一个 C 程序来执行 3X3 矩阵运算!
问题
运行时输入任意9个数字,用C语言将数字按行、列、对角线相加
算法
Step 1: Declare 9 variables Step 2: enter 9 numbers at runtime Step 3: store the numbers in 3 X 3 matrix form //xyz p q r a b c Step 4: Do row calculation: add all the numbers in a row and print //(x+y+z),(p+q+r),(a+b+c) Step 5: Do column calculation: add all the numbers in a Column and print //(x+p+a)(y+q+b)(z+r+c) Step 6: Do Diagonal Calculation: add the numbers in diagonal and print //(x+q+c),(a+q+z)
程序
#include输出结果int main(){ int x,y,z,p,q,r,a,b,c; printf(“enter any 9 numbers :\n"); scanf(“%d%d%d%d%d%d%d%d%d",&x,&y,&z,&p,&q,&r,&a,&b,&c);//读取所有9个整数 printf(“%d %d %d\n",x,y,z); printf(“%d %d %d\n",p,q,r); printf(“%d %d %d\n",a,b,c); printf(“Row total:%d %d %d\n",(x+y+z),(p+q+r),(a+b+c));//行计算 printf(“column total: %d %d %d\n",(x+p+a),(y+q+b),(z+r+c));//列计算 printf(“diagonal total :%d %d\n",(x+q+c),(a+q+z));//对角线计算 return 0; }
enter any 9 numbers : 2 4 6 3 5 7 8 9 2 2 4 6 3 5 7 8 9 2 Row total:12 15 19 column total: 13 18 15 diagonal total :9 19