編輯:我相當新的C++。兩週前開始使用這種語言。我如何輸出二維數組的行總和C++
對不起,如果之前已經詢問過這個問題,但是我已經在網上搜索瞭如何在二維數組中搜索單個行並且找不到我正在尋找的答案。
我需要顯示[m] [n]中每個單獨行的總和,但由於某種原因,這隻適用於我的數組爲2x2,但如果它是3x3或更大,那麼我得到以下輸出終端:
for intsance, a[3][3]=
{1,2,3}, //this is determined by the user
{1,2,3},
{1,2,3};
then i get the following output:
9179942 //address of some sort???
6 // actual sum. code is working here (yay :D)
469090925// again something i dont understand
這是我迄今爲止
#include <iostream>
using namespace std;
int main(){
int m,n;
cout<<"Enter number of rows for array"<<endl;
cin>>m;
if (m>10){
cout<<"More than 10 rows will be too big"<<endl;
return 1;
}
cout<<"Enter number of collumns for array"<<endl;
cin>>n;
if (n>10){
cout<<"More than 10 collumns will be too big"<<endl;
return 1;
}
int a[m][n];
for(int i=0; i<m;i++){
cout<<"Enter "<<m<<" values into row "<<i+1<<endl;
for(int j=0; j<n; j++){
cout<<"a ["<<i<<"]["<<j<<"]: ";
cin>>a[i][j];
}
}
cout<<"Array dimensions: "<<m<<"x"<<n<<'\n'<<"resulting array: "<<endl;
for(int i=0; i<m;i++){
for(int j=0; j<n; j++){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
int avg[m];
int el_less_avg;
for(int i=0; i<m; i++){
for(int j=0; j<n;j++){
avg[i]+=a[i][j];
}
}cout<<"\n\n";
for(int i=0; i<m; i++){
cout<<avg[i]<<endl;
}
return 0;
}
初始化您的數據。 – alcedine
[如何在C++中打印二維數組?]可能的重複(http://stackoverflow.com/questions/12311149/how-to-print-2d-arrays-in-c) – johnbakers
我的問題是爲什麼我得到奇怪的輸出...有什麼我想念? –