I am a beginner and I am learning the C programming language. I am trying to make a program of two matrices that will take input and show output.
I have written a C program for that but it is taking only one input. After taking one input the program is terminating automatically.
Here is my C code:
#include <stdio.h>
void matrixInput(int rows, int cols, int matrix[rows][cols], int matrixNo) {
printf("Matrix %d input:\n", matrixNo);
for (int i = 0; i < rows; i ) {
for (int j = 0; j < cols; j ) {
printf("Matrix %d [%d, %d]: ", matrixNo, i 1, j 1);
scanf("%d", &matrix[i][j]);
}
}
printf("\n");
}
void matrixDisplay(int rows, int cols, int matrix[rows][cols], int matrixNo) {
printf("Matrix %d output:\n", matrixNo);
for (int i = 0; i < rows; i ) {
for (int j = 0; j < cols; j ) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
printf("\n");
}
int main() {
int rows, columns;
printf("Rows: ");
scanf("%d", &rows);
printf("Columns: ");
scanf("%d", &columns);
int matrix1[rows][columns], matrix2[rows][columns];
matrixInput(matrix1, rows, columns, 1);
matrixInput(matrix2, rows, columns, 2);
matrixDisplay(matrix1, row, column, 1);
matrixDisplay(matrix2, row, column, 2);
return 0;
}
Why this is happening? How can I solve the problem?
CodePudding user response:
void matrixInput(int rows, int cols, int matrix[rows][cols], int matrixNo)
but you are calling
matrixInput(matrix1, rows, columns, 1);
while it must be
matrixInput(rows, columns,matrix1, 1);
CodePudding user response:
The order of arguments in these calls
matrixInput(matrix1, rows, columns, 1);
matrixInput(matrix2, rows, columns, 2);
matrixDisplay(matrix1, row, column, 1);
matrixDisplay(matrix2, row, column, 2);
does not corresponds to the order of the function parameter declarations
void matrixInput(int rows, int cols, int matrix[rows][cols], int matrixNo) {
and
void matrixDisplay(int rows, int cols, int matrix[rows][cols], int matrixNo) {
You need to write for example
matrixInput(rows, columns, matrix1, 1);