How do I declare an empty array as a function parameter correctly?

Viewed 42

I'm currently doing some free exercises on codeStepByStep, and I particularly don't know how to solve the max_row one.

I need to input a 2D array as a function parameter, but it keeps showing me this error:

note: declaration of ‘arr’ as a multidimensional array must have bounds for all dimensions except the first *

It seems like arrays should at least have a predefined column length, but it can't be dynamic like that right? Here is my code :

int max=row(int numColumn, int arr[][numColumn], int numRow) {
    
    int i, j, sum = 0, maxSum = 0, a, b=0;
    
    for (i=0; i<numRow; i++) {
        for (j=0; j<numColumn; j++) {
            sum = arr[i][j] + sum;
        }
    if (maxSum < sum) {
        maxSum = sum;
        a = i;
    }
    if (maxSum = sum) {
        b = i;
    }
    if (b < a) {
        a = b;
    }
    }
    
    return a; 
}



int main()
{
    int list[4][3] = {
    {  3,   8,  12},
    {  2,   9,  17},
    { 43,  -8,  46},
    {203,  14,  97}
    };
    
    printf("%d", max_row(3, list, 4));

    return 0;
}

After asking here and there I found that I should start by declaring the column variable, so instead of this :

int max_row(int arr[][numColumn], int numRow, int numColumn)

it should be something like this as in the code above:

int max_row(int numColumn, int arr[][numColumn], int numRow)

but it won't be tested by the website.

The test question:

Write a function named max_row that accepts a number of rows and columns, and a 2-D array of integers, as parameters and that returns the index of the row where the elements add up to the greatest value. For example:

int list[4][3] = {
    {  3,   8,  12},
    {  2,   9,  17},
    { 43,  -8,  46},
    {203,  14,  97}
};

Then the call of max_row(list, 4, 3) should return 3. If there is a tie between two or more rows, return the row with the smaller index.

Your code should work for an array of any size at least 1x1.

Everything in main is me trying to see if the code works.

1 Answers

It's because you can't declare a multi-dimensional array with a variable as dimension (c and c++ don't support it unless you declare them in the heap memory). A solution could be this one:

#define MAX 10
int row(int numColumn, int arr[][MAX], int numRow)
{
int i, j, sum = 0, maxSum = 0, a, b = 0;

    for (i = 0; i < MAX; i++) {
        for (j = 0; j < MAX; j++) {
            sum = arr[i][j] + sum;
        }
        if (maxSum < sum) {
            maxSum = sum;
            a = i;
        }
        if (maxSum == sum) {
            b = i;
        }
        if (b < a) {
            a = b;
        }
    }

    return a;
}

In the first line you declare a constant named MAX with value 10, then you use it to initialize the array. Also I don't understand the use of maxSum and b, since you never actually use maxSum (you just initialized it) and you don't return b. Plus you didn't even initialize a (you can't return a not initialized variable). Moreover REMEMBER that you have to write == to make a comparison in an if statement, not =. Hope it helps, let me know if you need something else or if I wasn't clear enough

Related