How do i output sum of rows of a 2D array C++ -
edit: i'm new c++. began working language 2 weeks ago.
sorry if have been asked before, have searched everywhere on web on how sum individual rows in 2d array , not found answer looking for.
i need display sum of each individual row in a[m][n], reason works when array 2x2, if it's 3x3 or bigger, following output in terminal:
for intsance, a[3][3]= {1,2,3}, //this determined user {1,2,3}, {1,2,3}; following output: 9179942 //address of sort??? 6 // actual sum. code working here (yay :d) 469090925// again dont understand this have far
#include <iostream> using namespace std; int main(){ int m,n; cout<<"enter number of rows array"<<endl; cin>>m; if (m>10){ cout<<"more 10 rows big"<<endl; return 1; } cout<<"enter number of collumns array"<<endl; cin>>n; if (n>10){ cout<<"more 10 collumns big"<<endl; return 1; } int a[m][n]; for(int i=0; i<m;i++){ cout<<"enter "<<m<<" values 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; }
int avg[m]; int el_less_avg; for(int i=0; i<m; i++){ for(int j=0; j<n;j++){ you're not initializing these values they're free whatever cruft on stack @ time. need initialize them.
int avg[m]; (int = 0; < m; ++i) { avg[i] = 0; (int j = 0; j < n; ++j) { avg[i] += a[i][j]; } }
Comments
Post a Comment