+ 1

Error message "array type has incomplete element type 'int[]'" in C programming

I wrote a function called max that returns the maximum value in the 2D array and Functions should not have pointers implementations. I have stuck with the line "int max (int array[][], int row, int col)" and this prints out the error message. My code is below: #include <stdio.h> #include <stdlib.h> int main(void) { printf("Let's create 2D Arrays!\n"); printf("How many rows? "); int row; scanf("%d", &row); printf("How many columns? "); int column; scanf("%d", &column); int A[row][column]; int i, j; for (i=0; i<row; ++i){ for (j=0; j<column; ++j){ printf("Enter value at [%d][%d]: ", i, j); scanf("%d",&A[i][j]); } } // print max value in 2D Arrays printf("\nThe max value in 2D Arrays is: \n"); max(A,row,column); return (EXIT_SUCCESS); } int max (int array[][], int row, int col){ int max = array[0][0]; int i; int j; for (i=0; i<row; ++i){ for (j=0; j<col; ++j){ if (array[i][j] > max){ max = array[i][j]; } } } return max; printf("%d",max); } Can someone help me to fix this without using the pointer implementation. Thanks!

22nd Sep 2018, 1:43 AM
Tuyen Pham
Tuyen Pham - avatar
9 Antworten
+ 5
You need to replace the code with this one: //print the sum element of each column in 2D Array printf("\n"); for (j=0; j<column; ++j){ int c = colSum(row,column,j,A); printf("\nSum of column %d is %d",j+1,c); } printf("\n"); return 0; } int colSum (int row, int col, int index, int array[row][col]){ int i; int sum = 0; for (i=0; i<row; ++i){ sum += array[i][index]; } return sum; } In the main function, inside for loop: colSum(row,j,A); when j = 0, you sent it to the col variable in the colSum function and that value was also used for column size. So on each iteration column size of the array was different that's why the output was different. You need to send rows and columns separately colSum(row,column,j,A); where value of j will be assigned to a separate variable in colSum function
26th Sep 2018, 7:31 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 5
Tuyen Pham You also need to add a variable in the function prototype int colSum(int row, int col, int index, array[row][col]);
26th Sep 2018, 7:34 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 4
Here corrected your code. When passing the 2D array you have to define the number of columns of the array in function parameter list. Or else you can create dynamic arrays and then pass them, anther way is you define the sizes Globally and then use those sizes when passing 2D array. In this program I have converted 2D array to 1D array and then back to 2D array https://code.sololearn.com/ciFCf41JJXG3/?ref=app
22nd Sep 2018, 11:00 AM
blACk sh4d0w
blACk sh4d0w - avatar
+ 3
If I have understood you correctly you mean to ask if you define the no. of columns of the array and user inputs a different number than what would happen? If that's so, then it is not possible to change the size of the static arrays. In that case you can create dynamic array and you can change its size. I would prefer to use vectors instead of dynamic arrays
22nd Sep 2018, 8:10 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 3
Tuyen Pham what's the issue?
26th Sep 2018, 7:03 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 1
awesome. I got it. Thanks nAutAxH AhmAd! but i have another question. As my code before nt colSum (int row, int col, int array[row][col]){ int i; int sum = 0; for (i=0; i<row; ++i){ sum += array[i][col]; } return sum; } col in this function is sent from column in the main and it stills keep in 1 col to count for the sum of the col and the for loop will pass in each rows. I am confused in this?
26th Sep 2018, 7:43 PM
Tuyen Pham
Tuyen Pham - avatar
0
I am confused that if I define the number of columns of the array in function for exam int max (int array[][3], int row, int col). so when the user enters the number of columns on the top, this number can be different 3?
22nd Sep 2018, 3:44 PM
Tuyen Pham
Tuyen Pham - avatar
0
Hey guys, could you help me to fix this code since I dont know what wrong with this. Thanks! https://code.sololearn.com/cl9i2QH9cUAo/#c
26th Sep 2018, 7:00 PM
Tuyen Pham
Tuyen Pham - avatar
0
I entered 2 rows, 3 columns and the outputs are 10,20,30,40,50,60 but it can not print out the correctly the sum of each columns. Could you help me to fix it what wrong in my code?
26th Sep 2018, 7:09 PM
Tuyen Pham
Tuyen Pham - avatar