I am a beginner and I am learning the C programming language. I am trying to code a program that multiplies two matrices.
I am taking two inputs. The first is for the row and the second is for the column. The rows and columns of the two matrices will be the same. So I am taking row and column input only for one time.
For the first matrix, the input is working properly. But for the second matrix, it is taking more input than the first input. In my test case, the number of rows was 2 and the number of columns was 3.
My C code:
#include <stdio.h>
void matrixInput(int matrix[][10], int row, int column, int matrixNo) {
printf("Matrix %d input:\n", matrixNo);
for (int i = 0; i < row; i ) {
for (int j = 0; j < column; j ) {
printf("Matrix %d [%d, %d]: ", matrixNo, i 1, j 1);
scanf("%d", &matrix[i][j]);
}
}
printf("\n");
}
void matrixDisplay(int matrix[][10], int row, int column, int matrixNo) {
printf("Matrix %d output:\n", matrixNo);
for (int i = 0; i < row; i ) {
for (int j = 0; j < column; j ) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
printf("\n");
}
int main() {
int row, column;
printf("Row: ");
scanf("%d", &row);
printf("Column: ");
scanf("%d", &column);
int matrix1[row][column], matrix2[row][column];
matrixInput(matrix1, row, column, 1);
matrixInput(matrix2, row, column, 2);
matrixDisplay(matrix1, row, column, 1);
matrixDisplay(matrix2, row, column, 2);
return 0;
}
When I am using void matrixInput(int matrix[row][column], int row, int column, int matrixNo)
, I am getting these errors:
error: 'row' undeclared here (not in a function)
error: 'column' undeclared here (not in a function)
How can I solve this problem?
CodePudding user response:
You have function signatures:
void matrixInput(int matrix[][10], int row, int column, int matrixNo);
void matrixDisplay(int matrix[][10], int row, int column, int matrixNo);
but the arrays you are passing to the functions do not have 10 columns.
You should make the dimensions match:
void matrixInput(int rows, int cols, int matrix[rows][cols], int matrixNo);
void matrixDisplay(int rows, int cols, int matrix[rows][cols], int matrixNo);
I've renamed the size variables and placed them before they are used — using C99 variable-length array (VLA) notation. Renaming them is optional; defining the sizes before they are used in the array definition is mandatory. Obviously, you must also fix your function calls.
You could replace the [rows]
subscript in the function definition/declaration with []
, but I regard it as clearer to specify both dimensions.
The extra input requested probably arises because you've invoked undefined behaviour because the array sizes don't match. The chances are that the first input has overwritten some of the key data. You could try printing rows
and cols
(or row
and column
in your naming convention) on entry to (and maybe exit from) the matrixInput()
function. Doing so would probably show you that there is a problem. There isn't much point in working out what's going on in detail; undefined behaviour is erratic and can vary between compilers or releases of a single compiler (or even between compilations by the same compiler — though that would be unusual in practice). You could also consider printing the first array after entering it; you might be surprised by what you see in the output. Fix the problem by making the sizes match.
CodePudding user response:
The parameter int matrix[][10]
of the functions matrixInput
and matrixDisplay
is wrong. You should not use an arbitrary size, instead it should be equal to column
.
Therefore, you should change
int matrix[][10]
to
int matrix[][column]
in both function prototypes.
However, you can only do this after declaring column
. Therefore, you must also change the order of the parameters. For example, you could change
void matrixInput(int matrix[][10], int row, int column, int matrixNo)
to
void matrixInput(int row, int column, int matrix[][column], int matrixNo)
and change the function calls accordingly.