0

My Solution: I am running this program in CodeBlocks. It is not working and it shows:

       //error: array type has incomplete elements type 'int[]'
      //warning: return type of 'main' is not 'int'

I want to function which calculates the sum of each row and their total. This solution doesn't work because it shows an error in function declaration.

   //function declaration
  void findSumEachRowAndTotalSum(int a[][], int c, int r){
     int i, j, rowSum, totalSum;
     //ask the user to give elements of rows
     for(i=0; i<c; i++){
       printf("\nGive elements of row %d:\n", i+1);
        for(j=0; j<r; j++)
        scanf("%d", &a[i][j]);
     }
   totalSum = 0;
   for(i=0; i<c; i++){
     rowSum = 0;
     for(j=0; j<r; j++){
     //calculates the sum of each row and total sums
      rowSum = rowSum + a[i][j];
      totalSum = totalSum + a[i][j];
   }
   //displays sum of each row on the screen
  printf("\nSum of row %d is %d", i+1, rowSum);
 }
  //displays the total sum of all rows on the screen
printf("\nTotal sum is %d\n", totalSum);
}

//main function
void main(void){
  int col, row, m[50][50], i, j;
  //...
  //function call
  findSumEachRowAndTotalSum(a,c,r);

}
Arsel Doe
  • 21
  • 2
  • 2
    `findSumEachRowAndTotalSum(a,c,r);` You never declared `a`, `c` and `r` in main. Maybe you meant `findSumEachRowAndTotalSum(m,col,row);`? Beside that if you declare a two dimensional array as function parameter you have to define second dimension. – Osiris Aug 22 '18 at 17:05
  • The compiler is also correct about the return type from main. It should be `int main(void)` – Tim Randall Aug 22 '18 at 17:19
  • Please note that in C, multidimentional array are stored row-wise. E.g. `int m[3][2] = {{1,2}, {3,4}, {4,5}};` the first row is `{1,2}`. You may ignore this in your program, but you should do it consistently, while in your code you are apparently trying to pass the number of cols as `c` in your function and using it as the number of rows, which is confusing. – Bob__ Aug 22 '18 at 18:45
  • Also, you could update the value of `totalSum` outside the inner loop using the value of `rowSum`. Consider splitting the posted function into smaller functions. – Bob__ Aug 22 '18 at 18:49
  • You might be interested in [flexible array member](https://en.wikipedia.org/wiki/Flexible_array_member)s like in [here](https://stackoverflow.com/a/41410503/841108) – Basile Starynkevitch Aug 23 '18 at 05:15

2 Answers2

3

The error is due to the definition of the parameter a to the function findSumEachRowAndTotalSum:

void findSumEachRowAndTotalSum(int a[][], int c, int r){

When an array of more than one dimension is a parameter to a function, only the first dimension is allowed to be left blank. All others must be specified.

Since you seem to be using c and r as the dimensions, you need to give those parameters first and subsequently use them as the dimensions of the array:

void findSumEachRowAndTotalSum(int c, int r, int a[c][r]){

Then you would call the function like this:

findSumEachRowAndTotalSum(50, 50, m);

Regarding the warning, the main function must be defined to return type int, and you need to subsequently return a value:

int main(void){
   ...
   return 0;    
}
dbush
  • 205,898
  • 23
  • 218
  • 273
  • I think the question here is a bit unclear. My understanding was that the maximum dimensions are 50x50 and `col` and `row` defines the used subset, otherwise there would be no need for `col` and `row` at all. – Osiris Aug 22 '18 at 17:20
0

@Osiris, you must post your comment as an answer. You are right, @ArselDoe might have mistakenly overlooked what name was given to the array in main().

@ArselDoe, you have declared the array with the identifier as m[][], but have passed a as the parameter to the function call. Changing this should fix the error.

Harshith Rai
  • 3,018
  • 7
  • 22
  • 35