简体   繁体   English

如何使用 C++ 中的用户定义函数添加二维方阵的左对角线

[英]How to add left diagonal of a 2D square matrix using user-defined functions in C++

I am trying to find the sum of the elements of the left diagonal of a 2D matrix.我试图找到二维矩阵左对角线元素的总和。 But it shows the following error:但它显示以下错误:

.\sum_matrix.cpp: In function 'int main()':
.\sum_matrix.cpp:44:33: error: invalid conversion from 'int' to 'int (*)[10]' [-fpermissive]
  cout<<sum_diagonal(arr[MAX][MAX]);
                     ~~~~~~~~~~~~^
.\sum_matrix.cpp:7:5: note:   initializing argument 1 of 'int sum_diagonal(int (*)[10])'
 int sum_diagonal(int arr_[][MAX]){
     ^~~~~~~~~~~~

This is my code:这是我的代码:

#include <iostream>
using namespace std;

const int MAX = 10;

//adding the elements of the left diagonal
int sum_diagonal(int arr_[][MAX]){
    int sum = 0;
    for(int i = 0; i<MAX; i++){
        for(int j = 0; j<MAX; j++){
            if(i == j){ 
                sum += arr_[i][j];
            }   
        }
    }
    return sum;
}

int main()
{   
    int arr[MAX][MAX];

    //entering array elements
    cout<<"Enter the values of rows and columns of array:\n";
    for(int i = 0; i<MAX; i++){
        for(int j = 0; j<MAX; j++){
            cout<<"arr["<<i<<"]["<<j<<"] = ";
            cin>>arr[i][j];
        }
    }

    //displaying array elements
    cout<<"The array is:\n";
    for(int i = 0; i<MAX; i++){
        for (int j = 0; i < MAX; j++)
        {
            cout<<"\t"<<arr[i][j];
        }
    }   
    cout<<endl;

    //sum of the left diagonal
    cout<<"Sum of diagonal elements: ";
    cout<<sum_diagonal(arr[MAX][MAX]);
    return 0;
}

I looked up into several websites but couldn't figure it out.我查看了几个网站,但无法弄清楚。 Any kind of help will be appreciated.任何形式的帮助将不胜感激。

There were a few things in your code:您的代码中有几件事:

  1. (not a bug) avoid using namespace std; (不是错误)避免using namespace std;

  2. There was a typo i changed for j in the 2nd loop j i了一个错字

  3. (improvement) Sum of matrix was O(N^2) unnecessarily, made O(N) (改进)矩阵总和不必要地为O(N^2) ,使O(N)

  4. (not a bug) Printing as a matrix (and not in the same line) (不是错误)打印为矩阵(而不是在同一行)

  5. Passing matrix only needs the variable name not matrix size传递矩阵只需要变量名而不是矩阵大小

So the code became所以代码变成了


#include <iostream>

const int MAX = 10;

//adding the elements of the left diagonal
int sum_diagonal(int arr_[MAX][MAX]){
    int sum = 0;
    for(int i = 0; i<MAX; i++){
        sum += arr_[i][i];
    }
    return sum;
}


int main()
{   
    int arr[MAX][MAX];

    //entering array elements
    for(int i = 0; i<MAX; i++){
        for(int j = 0; j<MAX; j++){
            std::cin >> arr[i][j];
        }
    }

    //displaying array elements
    std::cout<<"The array is :\n";
    for(int i = 0; i<MAX; i++){
        for (int j = 0; j < MAX; j++)
        {
            std::cout<< arr[i][j] << " ";
        }
        std::cout << std::endl;
    }   
    std::cout << std::endl;

    //sum of the left diagonal
    std::cout<<"Sum of diagonal elements: ";
    std::cout<< sum_diagonal(arr);
    return 0;
}

Godbolt: https://godbolt.org/z/TG1exK8Yn Godbolt: https://godbolt.org/z/TG1exK8Yn

Runs as (I changed MAX=4 in the example to simplify)运行为(我在示例中更改了 MAX=4 以简化)

Program stdout
The array is :
1 2 3 4 
5 6 7 8 
9 10 11 12 
13 14 15 16 

Sum of diagonal elements: 34

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM