c - 变长多维数组

标签 c function multidimensional-array variable-length-array

我为家庭作业编写了这个程序,运行时它崩溃了,没有任何错误。

此外,在更正后,任何提高我的编码方法效率的建议都将受到赞赏。

首先,我将 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++)
...

关于c - 变长多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41418554/

相关文章:

javascript - 如何按列比较多维数组

检查路径是绝对路径还是相对路径

C 数组赋值使用大括号语法

c++ - 使用 JNI 可移植地获取编译器包含(和链接器库)搜索 C 代码的路径

JavaScript 参数传递

javascript - javascript库如何忽略某些参数,或使某些函数参数为 "optional"?

c - 在 C 中使用 MPI_Type_vector 接收

c - 在 C 中尝试 catch 语句

R 将不同的函数应用于不同的数据框列

php - 通过组合值将多维数组转换为单个数组