0

I've got the program body, most of my errors are in my function. I've tried fixing it but i can't change the data types.

#include <stdio.h>

int sumrow (int matrix);
int sumcol (int matrix);

int main () {

int matrix [3][4] = { {5, 7, 4, 8}, {6, 8, 2, 4}, {2, 7, 9, 6} };
int sum;

sumrow = matrix[3][4];

[Error] assignment of function 'int sumrow(int)' This data type error makes no sense to me and I can't change it. [Error] cannot convert 'int' to 'int(int)' in assignment

sumcol = matrix[3][4];

[Error] assignment of function 'int sumcol(int)' [Error] cannot convert 'int' to 'int(int)' in assignment The errors here are identical and I am probably calling the error incorrectly.

return 0;
}


int sumrow (int matrix){
int i, j, sum = 0;

for (i = 0; i < 4; ++i) 
    {
        for (j = 0; j < 3; ++j) 
        {
            sum = sum + matrix[i][j] ;

[Error] invalid types 'int[int]' for array subscript Both functions have the same error and I think it's because of the variables i used.

        }
    }
return printf("Sum of the %d row is = %d\n", sum);
}


int sumcol (int matrix){

int i, j, sum = 0;
    for (j = 0; j < 3; ++j) 
    {
        for (i = 0; i < 4; ++i)
        {
            sum = sum + matrix[i][j];

[Error] invalid types 'int[int]' for array subscript I don't know how to fix this error.

        }
    }
return printf("Sum of the %d column is = %d\n", sum);
 }
3
  • int sumcol (int matrix); and sumcol = matrix[3][4];: Is sumcol supposed to be a function or an int? Same question with int sumrow (int matrix);. Commented Jun 19, 2020 at 13:45
  • 1
    You accessing memory outside the array here: sumrow = matrix[3][4]; and sumcol = matrix[3][4];. Commented Jun 19, 2020 at 13:47
  • int sumrow (int matrix) should be int sumrow (int matrix[3][4]) and the call should be sumrow(matrix) Commented Jun 19, 2020 at 14:48

1 Answer 1

1

The subroutine should be defined to take a 2D array instead of a single int. Note that you can leave the leftmost dimension empty.

int sumrow (int matrix[][4]){
    int i, j, sum = 0;  
    for (i = 0; i < 4; ++i)
    etc... 

And you need to call it by passing the array, not one element:

int sum = sumrow(matrix);

The return value of sumrow should be the sum that you calculated:

return sum;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.