C++编程:二维数组主对角线之和和次对角线之和

2024-12-02 22:42:02
推荐回答(3个)
回答1:

int i,a[N][N];
int sum1 = 0,sum2 = 0;
for(i = 0; i < N; ++i) {
sum1 += a[i][i];
sum2 += a[i][N - 1- i];
}
cout << "主对角线之和:" << sum1 << endl;
cout << "次对角线之和:" << sum2 << endl;

回答2:

#include
void main()
{
int m,n;
int i,j;
int sum1=0,sum2=0;
int a[100][100];
cout<<"请输入数组的行数和列数:"<cin>>m>>endl;
int n=m;
cout<<"请输入数组的各元素:"<for(i=0;ifor(j=0;j{
cin>>a[i][j];
if(i==j)
sum1=sum+a[i][j];//主对角线元素之和
if(i==n-1)
{
sum2=sum2+a[i][j];//次对角线元素之和
n--;
}
}
cout<<"主对角线元素之和为:"<cout<<"次对角线元素之和为:"<}

回答3:

....................这是大一前半学期的练习题大哥,你自己慢慢画吧