简体   繁体   中英

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;

  2. There was a typo i changed for j in the 2nd loop

  3. (improvement) Sum of matrix was O(N^2) unnecessarily, made 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

Runs as (I changed MAX=4 in the example to simplify)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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