variable length multidimensional arrays

169 views Asked by At

I did this program for a homework and it crashes without any error when run.

Also after correcting it, any suggestions to increase the efficiency of my coding approach are appreciated.

First I declared m,n,p,q as global variables an I passed only the arrays to the functions, but the program behaved weird.

Then I included the dimensions of the arrays as arguments in every function and declared it everywhere. CRASH

*VLA SUPPORTED

//functions on matrices
#include<stdio.h>

int i, j;
void getMatrix(int m, int n, int values[m][n]);
void displayMatrix(int m, int n, int values[m][n]);
void transposeMatrix(int m, int n, int values[m][n]);
void addMatrices(int m, int n, int p, int q, int A[m][n], int B[p][q]);
void multiplyMatrices(int m, int n, int p, int q, int A[m][n], int B[p][q]);

int main()
{
  int m, n, p, q, A[m][n], B[p][q];

  printf("Enter the no. of Rows    of the first  Matrix :  ");
  scanf("%d", &m);
  printf("Enter the no. of Columns of the first  Matrix :  ");
  scanf("%d", &n);

  printf("Enter the elements of the first matrix: \n");
  getMatrix(m, n, A);
  printf("The entered Matrix:\n");
  displayMatrix(m, n, A);
  printf("The transpose of the entered Matrix:\n");
  transposeMatrix(m, n, A);

  printf("Enter the no. of Rows    of the second Matrix :  ");
  scanf("%d", &p);
  printf("Enter the no. of Columns of the second Matrix :  ");
  scanf("%d", &q);

  printf("Enter the elements of the secong matrix: \n");
  getMatrix(p, q, B);
  printf("The entered Matrix:\n");
  displayMatrix(p, q, B);
  printf("The transpose of the entered Matrix:\n");
  transposeMatrix(p, q, B);

  printf("Addition of the Matrices:\n");
  addMatrices(m, n, p, q, A, B);
  printf("Multiplication of the Matrices:\n");
  multiplyMatrices(m, n, p, q, A, B);

  return 0;
}

void getMatrix(int m, int n, int values[m][n])
{
  for(i = 0; i < m; ++i)
    for(j = 0; j < n; ++j)
      scanf("%d", &values[i][j]);
}

void displayMatrix(int m, int n, int values[m][n])
{
  for(i = 0; i < m; ++i)
  {
    for(j = 0; j < n; ++j)
      printf("%3d  ", values[i][j]);

    printf("\n");
  }
}

void transposeMatrix(int m, int n, int values[m][n])
{
  int transpose[n][m];

  for(i = 0; i < n; ++i)
    for(j =0; j < m; ++j)
      transpose[i][j] = values[j][i];

  displayMatrix(n, m, transpose);
}

void addMatrices(int m, int n, int p, int q, int A[m][n], int B[p][q])
{
  int C[m][n];

  if(m == p && n == q)
  {
    for(i = 0; i < m; ++i)
    for(j = 0; j < n; ++j)
      C[i][j] = A[i][j] + B[i][j];

    displayMatrix(m, n, C);
  }
  else
    printf("Cannot add these Matrices!\n");
}

void multiplyMatrices(int m, int n, int p, int q, int A[m][n], int B[p][q])
{
  int C[m][q], k, sum = 0;
  if(n == p)
  {
    for(i = 0; i < m; ++i)
      for(j = 0; j < q; ++j)
      {
        for(k = 0; k < n; ++k)
          sum += A[i][j] * B[j][i];

        C[i][j] = sum;
        sum = 0;
      }

    displayMatrix(m, q, C);
  }
  else
    printf("Cannot multiply these Matrices!\n");
}
1

There are 1 answers

2
user2736738 On BEST ANSWER

Initiliazie m and n so that you don't get UB when using them in array index in VLA.

Unsed loop in multiply matrices

for(k = 0; k < n; ++k)
          sum += A[i][j] * B[j][i];

will be

for(k = 0; k < n; ++k)
          sum += A[i][k] * B[k][j];

Don't use global variables unless you need to. It is good practice to make index variables of for loop local.

for(int i=0;i<n;i++)
...