c++实现将两个给定的距阵(3*3)相乘得到另一个距阵并将其打印出来。提示: 要求: 1、从键盘输入两个矩阵,并输出结果矩阵。矩阵的输入输出要求行列对齐。 2、把矩阵相承写成函数形式,在主函数中完成矩阵的输入输出。
时间: 2023-05-10 21:55:33 浏览: 67
以下是实现将两个给定的矩阵(3*3)相乘得到另一个矩阵并将其打印出来的代码:
```c
#include <stdio.h>
void matrix_multiply(int mat1[][3], int mat2[][3], int res[][3]) {
int i, j, k;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
res[i][j] = 0;
for (k = 0; k < 3; k++) {
res[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
}
int main() {
int mat1[3][3], mat2[3][3], res[3][3];
int i, j;
printf("Enter the first matrix (3x3):\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &mat1[i][j]);
}
}
printf("Enter the second matrix (3x3):\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &mat2[i][j]);
}
}
matrix_multiply(mat1, mat2, res);
printf("Resultant matrix:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%d ", res[i][j]);
}
printf("\n");
}
return 0;
}
```
注意:矩阵的输入输出要求行列对齐。
相关推荐
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)