变长多维数组

variable length multidimensional arrays

我做这个程序是为了做作业,当 运行 时它崩溃了,没有任何错误。

此外,在更正后,如果有任何提高我的编码方法效率的建议,我们将不胜感激。

首先我将 m、n、p、q 声明为全局变量,我只将数组传递给函数,但程序表现得很奇怪。

然后我将数组的维度作为参数包含在每个函数中,并在所有地方声明它。崩溃

*支持 VLA

//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");
}

启动 mn 以便在 VLA 中的数组索引中使用它们时不会出现 UB。

乘法矩阵中的Unsed循环

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

将会

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

除非必要,否则不要使用全局变量。最好将 for 循环的索引变量设为本地。

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