【问题标题】:Why C++ Program Give me address instead value in MxN Array Col-Wise Sum but give me value in NxN Array?为什么 C++ 程序给我地址而不是 MxN 数组 Col-Wise Sum 中的值,但给我 NxN 数组中的值?
【发布时间】: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


【解决方案1】:

解决方法是在开始列求和之前转置矩阵

// Display column-wise
    cout<<"----------------------------------------"<<"\n";
    cout<<"|The Column-Wise Sum Of The Matrix A Is|"<<"\n";
    cout<<"----------------------------------------"<<"\n";
// Create transpose of matrix A of size n x m to get column-wise sum like row-wise sum
  for(int i=0;i<row;i++) {
    for(int j=0;j<col;j++) {
      matrix_B[j][i] = matrix_A[i][j];
    }
  }
// Display column-wise sum of matrix of size m x n
  for(int i=0;i<col;i++) {
        csum =0;
    for(int j=0;j<row;j++) {
      cout<<matrix_B[i][j]<<"\t";
    csum = csum + matrix_B[i][j];
      /* This is just to format the output
       * so you can see the matrix format
       * in the output transpose matrix.
       */
      if(j==row-1)
        cout<<"|  "<<csum;
    }
    cout<<"\n\n";
  }

【讨论】:

    【解决方案2】:

    你可以如下使用。

    #include <iostream>
    
    using namespace std;
    
    int main(){
       int matrix_A[10][10],row, col, csum=0, rsum = 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<col;i++)
            {
                csum =0;
                for(int j=0; j<row;j++){
                    csum += matrix_A[j][i];
                }
            cout<<csum<<"\t";
    
            }
    // Display row-wise sum of matrix of size m x n
        cout<<"----------------------------------------"<<"\n";
        cout<<"|The Row-Wise Sum Of The Matrix A Is|"<<"\n";
        cout<<"----------------------------------------"<<"\n";
            for(int i=0;i<row;i++)
            {
                rsum =0;
                for(int j=0; j<col;j++){
                    rsum += matrix_A[i][j];
                }
            cout<<rsum<<endl;
    
            }
    return 0;
    }
    

    【讨论】:

      【解决方案3】:

      仅当我输入方阵 nxn 时,此代码才给我逐列总和的值,但当输入 mxn 矩阵时,它会给我地址。

      好吧,让我们看看为什么

      row = 3 和col = 2

      你的矩阵是

      [ 
        [1,2],
        [3,4],
        [6,7]
      ]
      
      

      现在在你的循环中

      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";
      

      i 将是 0,1,2

      j 将是 0,1

      现在什么时候

      csum = csum + matrix_A[j][i];

      发生。在循环期间,这也会发生:

      csum = csum + matrix_A[0][2];

      matrix_A[0][2] 未定义。

      【讨论】:

      • 谢谢你的解释,你帮我找出解决方案,如果我们在得到 csum 之前转置矩阵,问题就会解决。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多