用C语言编程,二维数组实验题:利用矩阵相乘公式,编程计算M*N阶矩阵A和N*M阶矩阵B之积C(M*M阶矩阵)。 要求如下: 1.用宏定义形式分别定义M、N的值为3、4。 2.函数原型分别定义成: void MultiplyMatrix(int a[M][N], int b[N][M], int c[M][M]);/* 函数功能:计算矩阵相乘之积,结果存于二维数组c中 */ void InputMatrixA(int a[M][N]);/* 函数功能:输入矩阵a中的元素 */ void InputMatrixB(int b[N][M]);/* 函数功能:输入矩阵b中的元素 */ void OutputMatrix(int a[M][M]);/* 函数功能:输出矩阵a中的元素 */ 3.主函数定义成: int main() { int a[M][N], b[N][M], c[M][M]; InputMatrixA(a); InputMatrixB(b); MultiplyMatrix(a,b,c); OutputMatrix(c); return 0; } 4.输入输出格式: 输入矩阵A的提示:"Input %d*%d matrix a:\n" 输入矩阵A中元素的提示:"a[%d,%d]:" 输入矩阵A中元素的格式:"%d" 输入矩阵B的提示:"Input %d*%d matrix b:\n" 输入矩阵B中元素的提示:"b[%d,%d]:" 输入矩阵B中元素的格式:"%d" 输出结果提示:"Results:\n" 输出元素格式:"%6d"(注:每行末有printf("\n");) 程序运行示例: Input 3*4 matrix a:↙ a[0,0]:1 a[0,1]:2 a[0,2]:3 a[0,3]:4 a[1,0]:1 a[1,1]:2 a[1,2]:3 a[1,3]:4 a[2,0]:1 a[2,1]:2 a[2,2]:3 a[2,3]:4 Input 4*3 matrix b:↙ b[0,0]:1 b[0,1]:2 b[0,2]:1 b[1,0]:2 b[1,1]:1 b[1,2]:2 b[2,0]:1 b[2,1]:2 b[2,2]:1 b[3,0]:2 b[3,1]:1 b[3,2]:2 Results:↙ 16 14 16↙ 16 14 16↙ 16 14 16↙ ↙
时间: 2023-12-14 11:02:02 浏览: 72
好的,下面是代码实现:
```
#include <stdio.h>
#include <stdlib.h>
#define M 3
#define N 4
void MultiplyMatrix(int a[M][N], int b[N][M], int c[M][M]);
void InputMatrixA(int a[M][N]);
void InputMatrixB(int b[N][M]);
void OutputMatrix(int a[M][M]);
int main() {
int a[M][N], b[N][M], c[M][M];
InputMatrixA(a);
InputMatrixB(b);
MultiplyMatrix(a, b, c);
printf("Results:\n");
OutputMatrix(c);
return 0;
}
void MultiplyMatrix(int a[M][N], int b[N][M], int c[M][M]) {
int i, j, k;
for (i = 0; i < M; i++) {
for (j = 0; j < M; j++) {
c[i][j] = 0;
for (k = 0; k < N; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
}
void InputMatrixA(int a[M][N]) {
int i, j;
printf("Input %d*%d matrix a:\n", M, N);
for (i = 0; i < M; i++) {
for (j = 0; j < N; j++) {
printf("a[%d,%d]:", i, j);
scanf("%d", &a[i][j]);
}
}
}
void InputMatrixB(int b[N][M]) {
int i, j;
printf("Input %d*%d matrix b:\n", N, M);
for (i = 0; i < N; i++) {
for (j = 0; j < M; j++) {
printf("b[%d,%d]:", i, j);
scanf("%d", &b[i][j]);
}
}
}
void OutputMatrix(int a[M][M]) {
int i, j;
for (i = 0; i < M; i++) {
for (j = 0; j < M; j++) {
printf("%6d", a[i][j]);
}
printf("\n");
}
}
```
希望能够帮到您!
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)