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.