Ask your own question, for FREE!
Computer Science 13 Online
OpenStudy (anonymous):

Hey Guys...I'm trying to write a C program to add two matrices, but it seems that there's something wrong. It keeps giving these errors: error: invalid types `int[int]' for array subscript error: invalid types `int[int]' for array subscript error: cannot convert `int (*)[10]' to `int*' for argument `1' to `int multi(int*, int*, int, int, int)' I think the errors has something to do with the function declaration or function calls! what u think?!

OpenStudy (anonymous):

Here's the program: #include <stdio.h> int sumat(int first[], int second[], int m, int n) { int arr[10][10]; for ( int c = 0 ; c < m ; c++ ) for ( int d = 0 ; d < n ; d++ ) arr[c][d] = first[c][d] + second[c][d]; printf("Sum of entered matrices:-\n"); for (int c = 0 ; c < m ; c++ ) for (int d = 0 ; d < n ; d++ ) printf("%d\t\n", arr[c][d]); } main() { int m, n, first[10][10], second[10][10]; printf("Enter the number of rows and columns of matrix "); scanf("%d%d", &m, &n); printf("Enter the elements of first & second matrix\n"); for (int c = 0 ; c < m ; c++ ) { for (int d = 0 ; d < n ; d++ ) { scanf("%d", &first[c][d]); scanf("%d", &second[c][d]); } } sumat(first, second, m, n); }

OpenStudy (farmdawgnation):

Can you give us line numbers for those errors? Might help. :)

OpenStudy (anonymous):

Alright, I just spent some time working on this...I haven't used c in a while so I'm a bit rusty. Here is what I did so far: 1. you need to define variables before the for loops error: ‘for’ loop initial declarations are only allowed in C99 mode 2. sumat(int first[], int second[], int m, int n) should be: sumat(int first[][50], int second[][50], int m, int n) beause you are trying to pass in arrays of arrays, you also need to specify the size (maximum) 3. I created a print matrix function for testing and to better break up the code, a function that is made for adding shouldn't be doing any printing. When printing you were adding a tab and a newline after each number (\t\n), there should just be a tab between and then the newline should only be once after each inner for loop (row) Here is the new code, I renamed variables to better describe them: \[#include <stdio.h> int sumMatrix(int first[][50], int second[][50], int row, int col) { int y, x; int arr[50][50]; for ( y = 0 ; y < row ; y++ ) for ( x = 0 ; x < col ; x++ ) arr[y][x] = first[y][x] + second[y][x]; } printMatrix(int matrix[][50],int row, int col) { int y, x; for ( y = 0 ; y < row ; y++ ) { for ( x = 0 ; x < col ; x++ ) { printf("stored in:first[%d][%d]", y, x); printf(":%d\t", matrix[y][x]); } printf("\n"); } } main() { //initialize variables int x, y, row, col; printf("Enter the number of rows and columns of matrix\nrows: "); scanf("%d", &row); printf("columns: "); scanf("%d", &col); int first[row][col], second[row][col]; printf("Enter the elements of first & second matrix\n"); for ( y = 0 ; y < row ; y++ ) { for ( x = 0 ; x < col ; x++ ) { scanf("%d", &first[y][x]); printf("stored in:first[%d][%d]\n\n", y, x); //scanf("%d", &second[y][x]); } } //sumat(first, second, row, col) ; printMatrix(first, row, col); }\] I'm just trying to properly pass the matrix into printMatrix and print it out but I'm getting this: \[Enter the number of rows and columns of matrix rows: 2 columns: 2 Enter the elements of first & second matrix 1 stored in:first[0][0] 2 stored in:first[0][1] 3 stored in:first[1][0] 4 stored in:first[1][1] stored in:first[0][0]:1 stored in:first[0][1]:2 stored in:first[1][0]:-1037170512 stored in:first[1][1]:32767 \] Only the entries in first[0] are being preserved, I'm probably making a stupid mistake. I'll put the ball back in your court now. Let me know what you come up with

OpenStudy (anonymous):

first the function sunat(..) is int so u have to return a value it is not done here 2nd deceleration of c and d cannot be done in the for loop it is only possible in c++ u have to declare it before the clrscr() or starting of any codes

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!