【发布时间】:2020-09-10 05:17:14
【问题描述】:
#include <iostream>
using namespace std;
int main(){
int matrix_A[10][10],row, col,csum=0;
//Getting the rows from user and storing in row
cout<<"--------------------------"<<endl;
cout<<"|Enter The Number Of Rows|"<<"\n";
cout<<"--------------------------"<<endl;
cin>>row;
//Getting the columns from user and storing in col
cout<<"-----------------------------"<<endl;
cout<<"|Enter The Number Of Columns|"<<"\n";
cout<<"-----------------------------"<<endl;
cin>>col;
/* Asking the user to input the elements of matrix
* and storing them in the matrix array
*/
cout<<"---------------------------------------"<<endl;
cout<<"|Enter The Elements Of The Matrix A Is|"<<endl;
cout<<"---------------------------------------"<<"\n";
for(int i =0;i<row;i++) {
for(int j=0;j<col;j++) {
cin>>matrix_A[i][j];
}
}
// Display column-wise sum of matrix of size m x n
cout<<"----------------------------------------"<<"\n";
cout<<"|The Column-Wise Sum Of The Matrix A Is|"<<"\n";
cout<<"----------------------------------------"<<"\n";
for(int i=0;i<row;i++)
{
csum =0;
for(int j=0;j<col;j++){
csum = csum + matrix_A[j][i];
}
cout<<csum<<"\t";
}
return 0;
}
只有当我输入方阵 nxn 时,此代码才给我逐列总和的值,但当输入 mxn 矩阵时,它会给我地址。
1.是什么原因?
2.如何让程序在mxn矩阵中赋值?
【问题讨论】:
-
你认为它为什么会给你一个地址? (更仔细地查看您的索引。)
标签: c++ arrays matrix return-value col