1

I have a function "mod_kadane" defined in class "MAX_SUB_MAT" which returns an object.

#define dx 101
class MAX_SUB_MAT
{


public:
int curr_sum, max_sum, left, right, up, down;

MAX_SUB_MAT mod_kadane(int mat[dx][dx], int row, int column)
{
    MAX_SUB_MAT objx;
    curr_sum = objx.max_sum = INT_MIN;
    int sub_mat[row];

    for(int L = 0; L < row; L++)
    {
        memset(sub_mat, 0, sizeof sub_mat);
        for(int R = L; R < column; R++)
        {
            for(int i = 0; i < row; i++)
            {
                sub_mat[i] += i;//mat[i][R];
            }

            MAX_SUB_ARR obj;
            obj = obj.kadane(sub_mat, row);
            curr_sum = obj.maxima;

            if(curr_sum > objx.max_sum)
            {
                objx.max_sum = curr_sum;

                objx.left = L;
                objx.right = R;
                objx.up = obj.left;
                objx.down = obj.right;
            }
        }
    }
    return objx;
}
};

But when I'm trying to call it from "main":

cin >> row >> column;
int mat[row][column];
for(int i = 0; i < row; i++)
    {
        for(int j = 0; j < column; j++)
        {
            cin >> mat[i][j];
        }
    }

MAX_SUB_MAT objx;
objx = objx.mod_kadane(mat, row, column);

then an error is shown:

error: no matching function for call to 'MAX_SUB_MAT::mod_kadane(int [row][column], int&, int&)'|

But if I remove the 2d array "mat" from both sides then the code works fine.

Here is my full code:

#include<bits/stdc++.h>

#define dx 101

using namespace std;

class MAX_SUB_ARR
{
public:
int max_curr, maxima, left, right;


MAX_SUB_ARR kadane(int arr[], int n)
{
    MAX_SUB_ARR obj;

    obj.maxima = max_curr = INT_MIN;
    //cout << obj.maxima << endl;
    /*for(int i = 0; i < n; i++)
        cout << arr[i] << endl;*/
    for(int i = 0; i < n; i++)
    {
        if(max_curr < 0)
        {
            max_curr = arr[i];
            obj.left = i;
        }
        else
        {
            max_curr += arr[i];
        }

        //maxima = max(maxima, max_curr);
        if(max_curr > obj.maxima)
        {
            obj.maxima = max_curr;
            obj.right = i;
            //cout << obj.maxima << endl;
        }
    }

    return obj;
}
};

class MAX_SUB_MAT
{


public:
int curr_sum, max_sum, left, right, up, down;
/* MAX_SUB_MAT(int r, int c)
{
    row = r;
    column = c;
}*/

MAX_SUB_MAT mod_kadane(int mat[dx][dx], int row, int column)
{
    MAX_SUB_MAT objx;
    curr_sum = objx.max_sum = INT_MIN;
    int sub_mat[row];

    for(int L = 0; L < row; L++)
    {
        memset(sub_mat, 0, sizeof sub_mat);
        for(int R = L; R < column; R++)
        {
            for(int i = 0; i < row; i++)
            {
                sub_mat[i] += i;//mat[i][R];
            }

            MAX_SUB_ARR obj;
            obj = obj.kadane(sub_mat, row);
            curr_sum = obj.maxima;

            if(curr_sum > objx.max_sum)
            {
                objx.max_sum = curr_sum;

                objx.left = L;
                objx.right = R;
                objx.up = obj.left;
                objx.down = obj.right;
            }
        }
    }
    return objx;
}
};


int main()
{

int row, column;

printf("Number of rows you want to insert?:\n");
cin >> row;

printf("Number of columns you want to insert?:\n");
cin >> column;

int mat[row][column];

if(row && column)
{
    for(int i = 0; i < row; i++)
    {
        for(int j = 0; j < column; j++)
        {
            cin >> mat[i][j];
        }
    }

    //int curr_sum, max_sum, left, right, up, down;


    //MAX_SUB_MAT objx = new MAX_SUB_MAT(row, column);
    MAX_SUB_MAT objx;
    objx = objx.mod_kadane(mat, row, column);

    for(int i = objx.up; i <= objx.down; i++)
    {
        for(int j = objx.left; j <= objx.right; j++)
        {
            cout << mat[i][j] << " ";
        }

        cout << endl;
    }
}

return 0;
}
Anis Mia
  • 13
  • 4
  • Take a look at http://stackoverflow.com/questions/9446707/correct-way-of-passing-2-dimensional-array-into-a-function – sg7 Jun 11 '16 at 05:21
  • 3
    `int mat[row][column]` isn't valid in C++ because the dimensions aren't constant. And it definitely isn't the same as `int mat[101][101]` which your parameter is declared as. – Alan Stokes Jun 11 '16 at 05:25
  • @AlanStokes Thanks. But I don't understand why the dimensions need to be constant. The 2d array was a local variable not global. Was it because of passing as a parameter to a function? – Anis Mia Jun 11 '16 at 12:20
  • All array dimensions in C++ must be constant. Allowing "variable length arrays" is a non-portable extension (they're part of standard C though). See http://stackoverflow.com/q/1887097/212870. In C++ you're better off with `vector` or a class designed to support matrices (e.g. Eugene). – Alan Stokes Jun 11 '16 at 12:47
  • (By Eugene I meant Eigen. Sorry, autocorrect. http://eigen.tuxfamily.org/) – Alan Stokes Jun 12 '16 at 17:33

1 Answers1

1

It is not true that in C or C++ we cannot declare a function with two dimensional array parameter with both sizes specified.

See: http://c-faq.com/aryptr/pass2dary.html

The problem has been correctly explained by @AlanStokes in his comments!

sg7
  • 6,108
  • 2
  • 32
  • 40
  • 2
    Yes you can (https://ideone.com/PmLiGZ). It's perfectly valid, although potentially misleading since the first bound is actually ignored. – Alan Stokes Jun 11 '16 at 05:40
  • The parameter declaration here `int mat[dx][dx]` is exactly equivalent to `int mat[][dx]` which is one of the recommendations of that FAQ entry. – Alan Stokes Jun 11 '16 at 05:44
  • @AlanStokes I agree. My statement that "we cannot declare a function with two dimensional array parameter with both sizes specified" was incorrect! Thank you for pointing that! – sg7 Jun 11 '16 at 05:49