二维数组两个对角线相加求和
我没看出来错误的版本是错在哪里额,求教错误版本
#include<stdio.h>
#define M 5
int fun(int n,int xx[][M])
{
int i,j,sum=0;
printf("\nthe %d x %d matrix :\n",M,M);
for(i=0;i<M;i++)
{for(j=0;j<M;j++)
printf("%d",xx[i][j]);
printf(\n);
}
for(i=0;i<n;i++)
sum+=xx[i][i]+xx[i][n-i-1];
return sum;
}
main()
{int aa[M][M]={{1,2,3,4,5},{4,3,2,1,0},{6,7,8,9,0},{9,8,7,6,5},{3,4,5,6,7}};
printf("\nthe sum of elements on 2 dignals is %d",fun(M,aa));
}
正确版本
#include<stdio.h>
#define M 5
int fun(int n,int xx[][M])
{
int i,j,sum=0;
printf("\nthe %d x %d matrix :\n",M,M);
for(i=0;i<M;i++)
{
for(j=0;j<M;j++)
printf("%3d",xx[i][j]);
printf("\n");
}
for(i=0;i<n;i++)
sum=sum+xx[i][i]+xx[i][n-i-1];
return sum;
}
main()
{
int aa[M][M]={{1,2,3,4,5},{4,3,2,1,0},{6,7,8,9,0},{9,8,7,6,5},{3,4,5,6,7}};
printf("\nthe sum of elements on 2 dignals is %d\n",fun(M,aa));
}