C语言程序求两个矩阵的乘积


2023年2月9日, Learn eTutorial
1905

为了更好地理解,我们始终建议您学习下面列出的C语言编程基础主题

C语言中矩阵如何相乘?

在这个C语言程序中,我们需要计算两个矩阵的乘积。矩阵乘法是一种二元运算,它接受一对矩阵并生成另一个矩阵。矩阵是数字数组,因此没有特殊的方法来定义数组的乘法。因此,总的来说,“矩阵乘法”允许各种不同的方法来复制矩阵。

任何矩阵乘法的关键特征包括第一个矩阵的行数和列数(称为“大小”、“阶数”或“维数”),以及确定格子(lattices)的通道如何生成新矩阵。这意味着第一个矩阵的行数将等于第二个矩阵的列数。如果 A 是“n * m”矩阵,B 是“m * p”矩阵,则它们的矩阵乘积 AB 是“n * p”矩阵。

如何使用函数实现矩阵乘法?

在这个C语言程序中,我们要做的是从用户那里接收两个矩阵。并显示这些矩阵。现在我们打开一个嵌套的for循环,用于使用公式 C[i][j] = C[i][j] + A[i][k] * B[k][j] 进行矩阵乘法,最后将结果作为输出矩阵显示。

这个程序的主要逻辑是声明三个矩阵 a, m, c。然后我们需要从用户那里读取矩阵的阶数,并将其存储到变量 mn 中。然后,我们将通过调用 read matrix() 函数从用户那里接收矩阵 a。然后通过调用 print matrix() 函数显示矩阵 a

然后我们将通过调用 read Matrix() 函数从用户那里接收矩阵 B。然后通过调用 print Matrix() 函数显示矩阵 B。然后我们将调用 Product Matrix() 来找出矩阵 AB 的乘积。矩阵 AB 的乘积将存储在矩阵 C 中。现在我们可以通过调用 print Matrix() 函数打印矩阵 C 作为乘积矩阵。

算法

步骤 1:包含头文件以在 C 程序中使用内置函数。

步骤 2:声明三个矩阵 A[10][10]、B[10][10]、c[10][10] 和两个整数变量 MN

步骤 3:声明三个函数 read Matrix()、print Matrix()、product Matrix()

步骤 4:从用户那里接收矩阵的阶数到 MN 中。

步骤 5:通过调用 read Matrix() 函数从用户那里接收矩阵 A

步骤 6:通过调用 print Matrix() 函数打印矩阵 A

步骤 7:通过调用 read Matrix() 函数从用户那里接收矩阵 B

步骤 8:通过调用 print Matrix() 函数打印矩阵 B

步骤 9:调用函数 product Matrix(A, B, C, M, N) 以找出矩阵 AB 的乘积,并将结果存储在矩阵 C 中。

步骤 10:通过调用函数 print Matrix() 打印矩阵 C

函数 void readMatrix(int arr[][MAXCOLS], int M, int N)

步骤 1:声明变量 i,j 并设置 i=0

步骤 2:使用 for 循环检查 i

步骤 3:初始化 j=0 并检查 j

步骤 4:j 增加并重复步骤 3。

步骤 5:将 'i' 增加并重复步骤 2。

函数 printMatrix(int arr[][MAXCOLS], int M, int N)

步骤 1:声明变量 'i, j',并设置 i=0

步骤 2:使用 for 循环检查 'i'

步骤 3:初始化 'j=0' 并检查 'j'

步骤 4:将 'j' 增加一并重复步骤 3。

步骤 5:将 'i' 增加一并重复步骤 2

函数 productMatrix(int A[][MAXCOLS], int B[][MAXCOLS], int C[][MAXCOLS],int M, int N))

步骤 1:声明变量 'i, j, k',并设置 i=0

步骤 2:使用 for 循环检查 'i'

步骤 3:初始化 j=0

步骤 4:检查 j

步骤 5:设置 C[i][j] = 0

步骤 6:初始化 k=0 并检查 k

步骤 7: C[i][j] = C[i][j] + A[i][k] * B[k][j]

步骤 8:k 增加 1 并重复步骤 7,直到 k 达到 N

步骤 9:j 增加 1 并重复步骤 4,直到 k 达到 'N'

步骤 10:将 'i' 增加 1 并重复步骤 2,直到 'i' 达到 'N'

C 语言源代码

                                          #include <stdio.h>

#define MAXROWS 10
#define MAXCOLS 10

void main() {
  int A[MAXROWS][MAXCOLS], B[MAXROWS][MAXCOLS], C[MAXROWS][MAXCOLS];
  int M, N;
  void readMatrix(int arr[][MAXCOLS], int M, int N); /*Function declarations*/
  void printMatrix(int arr[][MAXCOLS], int M, int N);
  void productMatrix(int A[][MAXCOLS], int B[][MAXCOLS], int C[][MAXCOLS],
    int M, int N);
  printf("Enter the value of M and N\n");
  scanf("%d %d", & M, & N);
  printf("Enter matrix A\n");
  readMatrix(A, M, N);
  printf("Matrix A\n");
  printMatrix(A, M, N);
  printf("Enter matrix B\n");
  readMatrix(B, M, N);
  printf("Matrix B\n");
  printMatrix(B, M, N);
  productMatrix(A, B, C, M, N);
  printf("The product matrix is\n");
  printMatrix(C, M, N);
}
/*Input matrix A*/
void readMatrix(int arr[][MAXCOLS], int M, int N) {
  int i, j;
  for (i = 0; i < M; i++) {
    for (j = 0; j < N; j++) {
      scanf("%d", & arr[i][j]);
    }
  }
}
void printMatrix(int arr[][MAXCOLS], int M, int N) {
  int i, j;
  for (i = 0; i < M; i++) {
    for (j = 0; j < N; j++) {
      printf("%d", arr[i][j]);
    }
    printf("\n");
  }
}
void productMatrix(int A[][MAXCOLS], int B[][MAXCOLS], int C[][MAXCOLS],
  /* Multiplication of matrices */
  int M, int N) {
  int i, j, k;
  for (i = 0; i < M; i++) {
    for (j = 0; j < N; j++) {
      C[i][j] = 0;
      for (k = 0; k < N; k++) {
        C[i][j] = C[i][j] + A[i][k] * B[k][j];
      }
    }
  }
}
                                      

输出

Enter the value of M and N
3 3

Enter matrix A
1 1 1
2 2 2
3 3 3

Matrix A
1  1  1
2  2  2
3  3  3

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

Matrix B
1  2  3
4  5  6
7  8  9

The product matrix is
12 15 18
24 30 36
36 45 54