c语言 二维数组 对角线相加
程序出错,运行不了,求改正,使得这个程序可以输出二维数组aa对角线上的元素的和#include<stdio.h>#defineM5intfun(intn,intxx[][...
程序出错,运行不了,求改正,使得这个程序可以输出二维数组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("%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("%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));
} 展开
1个回答
展开全部
正确代码
#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];
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));
}
#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];
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));
}
追问
我可能没说清,指的是两个对角线之和,
把你的代码求和那里改成sum=sum+xx[i][i]+xx[i][n-i-1];就对了,
感谢~
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询