Satyajit Roy
Satyajit Roy

Reputation: 547

My C program is taking only one input instead of multiple input

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?

Upvotes: 0

Views: 527

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 310980

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) {

Moreover in the last two calls there are typos in the used identifiers. Instead of row and column you need to use rows and columns.

You need to write the function calls like for example

matrixInput(rows, columns, matrix1, 1);

Upvotes: 1

SecurityObscurity
SecurityObscurity

Reputation: 439

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);

Upvotes: 1

Related Questions