Write a calculation and output the sum of the diagonal elements of a 3*3 matrix. Example of program running result: Please enter the array element: (3*3)↙ 1 2 3 4 5 6 7 8 9↙ ↙ The array is: cqupt

Write a calculation and output the sum of the diagonal elements of a 3*3 matrix.

Example of program running result:
Please enter the array elements: (3*3) ↙
1 2 3 4 5 6 7 8 9↙
↙The
array is: ↙
1 2 3↙
4 5 6↙
7 8 9↙
↙The
sum of the diagonals: ↙
25↙

Input prompt: "Please enter array elements: (3*3)\n"
Input format: "%d"
Output prompt: "\nThe array is:\n"
Output format: "%4d"
Output prompt: "\nYes Sum of angles:\n"
Output format: "%d\n"

 #include<stdio.h>
int main()
{
    
    
	int a[3][3],sum=0;
    int i,j;
    printf("请输入数组元素:(3*3)\n");
    for(i=0;i<3;i++)
            for(j=0;j<3;j++)
                scanf("%d",&a[i][j]);
       printf("\n数组为:\n"); 
    for(i=0;i<3;i++)
            for(j=0;j<3;j++)
              {
    
    
              	 printf("%4d",a[i][j]); 
              	 if(j==2)
				   {
    
    
				   	printf("\n");
					} 
			  }
  
    sum=(a[0][0]+a[1][1]+a[2][2]+a[0][2]+a[2][0])  ;
    printf("\n对角线的和:\n");
    printf("%d\n",sum);	
} 

Guess you like

Origin blog.csdn.net/X131644/article/details/117111636#comments_26956342