0
#include<iostream>
using namespace std;

int n;

int diagonal(int m[][n]){
    int r = 0,l = 0;
    for(int i=0;i<n;i++){
        l += m[i][i];
        r += m[n-i][i];
    }
    if(r>l) return r - l;
    else return l - r;
}

int main(){
    cin >> n;
    int a[n][n];
    for(int i=0;i<n;i++) for(int j=0;j<n;j++) cin >> a[i][j];
    cout << diagonal(a) << endl;
    return 0;
}

I don't know why I am getting errors running above code.

Error No.1: array bound is not an integer constant before ']' token, how am I supposed to pass a constant value if I am taking it from the user.

Error No.2: 'n' was not declared in this scope 6 | for(int i=0;i<n;i++){, no idea about this one.

  • 4
    You can switch to `std::vector>` which can be sized at runtime, and provides a `.size()` method to query the number of elements – Cory Kramer Aug 20 '20 at 11:30
  • 5
    `int a[n][n];` is invalid C++ (use VLA extension). – Jarod42 Aug 20 '20 at 11:34
  • If you must use arrays, why not pass `n` as a separate argument? (Also, you don't declare `n` in `main`.) – Beta Aug 20 '20 at 11:38
  • 1
    [https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – drescherjm Aug 20 '20 at 11:40
  • Whichever C++ textbook gave this example of passing `int m[][n]` as a function parameter -- this is not valid C++ so you should get a better C++ textbook and throw away this one. – Sam Varshavchik Aug 20 '20 at 11:46

2 Answers2

2

Error No.1: array bound is not an integer constant before ']' token, how am I supposed to pass a constant value if I am taking it from the user.

You can't. When you want to use variable length arrays, you should usually replace them with std::vectors.

Example:

#include <cstdlib>
#include <iostream>
#include <vector>

int diagonal(const std::vector<std::vector<int>>& m) {
    int r = 0, l = 0;

    for(size_t i = 0; i < m.size(); ++i) {
        l += m[i][i];
        // r += m[n - i][i]; //  m[n][0]` when `i == 0`.
        r += m[m.size() - i - 1][i];
    }

    // this is most likely implemented without branching:
    return std::abs(r - l);
}

int main() {
    // use an unsigned type suitable for indexing like size_t
    if(size_t n; std::cin >> n) {

        // int a[n][n];               // not valid C++

        // vector replacement:
        std::vector<std::vector<int>> a(n, std::vector<int>(n));

        for(size_t i = 0; i < n; i++) {
            for(size_t j = 0; j < n; j++) {
                std::cin >> a[i][j];
            }
        }
        std::cout << diagonal(a) << '\n';
    }
}
Ted Lyngmo
  • 37,764
  • 5
  • 23
  • 50
0

Using templates:

template<auto X, auto Y>
int diagonal(int (&m)[X][Y]){
  • I'd say `const int (&m)[Y][X]` but you still have to deal with `int a[n][n];` to make use of that function. – Ted Lyngmo Aug 20 '20 at 16:12
  • Why would you prefer Y before X? – Hi - I love SO Aug 21 '20 at 07:46
  • Because multidimensional arrays are stored in [row-major](https://en.wikipedia.org/wiki/Row-_and_column-major_order) order in C++ - and the common usage is to have columns in the inner loop and rows in the outer loop. – Ted Lyngmo Aug 21 '20 at 07:53