-7

In the following program:

#include <iostream>
#include <cmath>
using namespace std;
int diagonalDifference(int x[][],int n)
{
    int sum1=0,sum2=0,y;

    for(int i=0;i<n;i++)
    {
        sum1+=x[i][i];
        sum2+=x[i][n-1-i];
    }
    y=abs(sum1-sum2);
    return y;   
}
int main()
{
    int n,**z;
    cin>>n;
    int arr[n][n];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cin>>arr[i][j];
        }
    }
    z=diagonalDifference(arr,n);
    cout<<x;
    return 0;
}

I get a compilation error I don't understand.

error:declaration of 'x' as multidimensional array must have bounds for all dimensions except the first

Could you help me fix it?

YSC
  • 38,212
  • 9
  • 96
  • 149
  • 3
    You have *multiple* problem with your code. First of all C++ doesn't support [variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array). Secondly compare the type of `z` and the return-type of `diagonalDifference`. – Some programmer dude Oct 10 '18 at 07:35

1 Answers1

2

int[][] is not a valid type:

int diagonalDifference(int x[][],int n)

You declare z as an int**:

int n,**z;

But you assign it an int:

int diagonalDifference(int x[][],int n);
z=diagonalDifference(arr,n);

And finally you print x which does not exist:

cout<<x;

As rules of thumb:

.

int diagonalDifference(int x**,int n) { /* .... */ }

int matrix_size = 0;
std::cin >> matrix_size;

std::vector<std::vector<int>> matrix{matrix_size, std::vector<int>{matrix_size}};
/* fill the matrix */

const int diag_diff = diagonalDifference(matrix, matrix_size);
std::cout << diag_diff << '\n';
YSC
  • 38,212
  • 9
  • 96
  • 149