【发布时间】:2021-09-09 10:49:15
【问题描述】:
我试图得到一个二维数组的行列之和,但是结果是0。我不知道我的错误是什么。谢谢你的帮助。
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
int characters[50][50],rc,sum=0,horizontal[10]={0},vertical[10]={0},c,r;
cout<<"Please enter integer : ";
cin>>rc;
cout <<" The matrix is : \n";
for(r=0;r<rc;r++){
for( c=0;c<rc;c++){
sum=sum+1;
characters[r][c]=sum;
cout<<characters[r][c]<< " ";
horizontal[r]=horizontal[r]+characters[r][c];
vertical[c]=vertical[c]+characters[c][r];
}
cout<< "The sum of horizontal is = " <<horizontal[r]<<" and The sum of vertical is "<<vertical[c];;
cout<<endl;
}
return 0;
}
这是这段代码的结果
Please enter integer : 3
The matrix is :
1 2 3 The sum of horizontal is = 6 and The sum of vertical is 0
4 5 6 The sum of horizontal is = 15 and The sum of vertical is 0
7 8 9 The sum of horizontal is = 24 and The sum of vertical is 0
【问题讨论】:
-
vertical[i]=vertical[i]+characters[c][r];->characters[c][r];未初始化使用。 -
是
<< vertical[i] <<还是<<vertical[r]<<? -
重新考虑您的解决方案:当您还没有查看所有行时,应该如何在每行中打印列的总和,嗯?
标签: c++ multidimensional-array