semicolon
semicolon

Reputation: 5

Write element of matrix in C

A matrix is taking all of my inputs but B matrix taking 2 or 3 elements and crashing. When i take B elements first this time it crashed in first loop.

int main (void)
{
    unsigned int row1, row2, column1, column2;
    int A[ row1 ][ column1 ];
    int B[ row2 ][ column2 ];

    printf ("Enter rows and columns of A matrix: ");
    scanf ("%u%u", &row1, &column1);

    printf ("Enter rows and columns of B matrix: ");
    scanf ("%u%u", &row2, &column2);

    printf ("Enter elements of A matrix\n");

    for ( i = 0; i < row1; i++ )
    {
        for ( j = 0; j < column1; j++ )
        {
            scanf ("%d", &A[ i ][ j ]);
        }
    } 

    printf ("\n\nEnter elements of B matrix\n");

    for ( i = 0; i < row2; i++ )
    {
        for ( j = 0; j < column2; j++ )
        {
            scanf ("%d", &B[ i ][ j ]);
        }
    }
}

Upvotes: 0

Views: 111

Answers (3)

mlwn
mlwn

Reputation: 1187

Try the code below (it works on linux with gcc):

#include <stdio.h>
#include <stdlib.h>

void print_matrix(int *M, int r, int c) {
  int i, j;
  for (i = 0; i < r; i++) {
    for (j = 0; j < c; j++) {
      printf("%d ", *(M + i*c + j));
    }
    printf("\n");
  }
}

int main (void) {
  int i, j, temp;
  unsigned int row1, row2, column1, column2;

  printf ("Enter rows and columns of A matrix: ");
  scanf ("%u%u", &row1, &column1);
  int *A = (int *)malloc(row1 * column1 * sizeof(int));

  printf ("Enter rows and columns of B matrix: ");
  scanf ("%u%u", &row2, &column2);
  int *B = (int *)malloc(row2 * column2 * sizeof(int));

  printf ("Enter elements of A matrix\n");
  for ( i = 0; i < row1; i++ ) {
    for ( j = 0; j < column1; j++ ) {
      scanf ("%d", &temp);
      *(A + i*column1 + j) = temp;
    }
  }

  printf ("\n\nEnter elements of B matrix\n");
  for ( i = 0; i < row2; i++ ) {
    for ( j = 0; j < column2; j++ ) {
      scanf ("%d", &temp);
      *(B + i*column2 + j) = temp;
    }
  }

  printf("\n\nMatrix A:\n");
  print_matrix( A, row1, column1);

  printf("\n\nMatrix B:\n");
  print_matrix( B, row2, column2);

}

OUTPUT

$ ./m2
Enter rows and columns of A matrix: 2 3
Enter rows and columns of B matrix: 4 3
Enter elements of A matrix
1
2
3
4
5
6


Enter elements of B matrix
1
2
3
4
5
6
7
8
9
0
1
2


Matrix A:
1 2 3 
4 5 6 


Matrix B:
1 2 3 
4 5 6 
7 8 9 
0 1 2 

Upvotes: 0

machine_1
machine_1

Reputation: 4454

You have to move the declarations of the arrays A and B to just before the first for loop, after the second scanf()

This is to allow the variables row1, row2, column1, and column 2 to be initialized with valid values.

Using uninitialized variables causes undefined behavior

Upvotes: 1

Cheatah
Cheatah

Reputation: 1835

row1, row2, column1 and column2 are uninitialised. That means that A and B are of an unpredictable size. A and B just point to some memory. You then read data into that memory and expect everything to go well. This is however a failure waiting to happen. You should allocate the memory once you know how much you will need.

Upvotes: 0

Related Questions