从键盘输入一个矩阵a[3][3]、b[3][3],先在屏幕上输出该矩阵,然后将矩阵对应数据相乘,即aij*bij将相乘后的数据放到矩阵a[3][3],再输出求乘后的矩阵。(注意:要程序实现过程中调用到自己编写的函数。
时间: 2023-11-13 15:04:38 浏览: 26
好的,以下是程序实现:
```c++
#include <iostream>
using namespace std;
void inputMatrix(int matrix[3][3]) {
cout << "请输入一个3*3矩阵:" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cin >> matrix[i][j];
}
}
}
void outputMatrix(int matrix[3][3]) {
cout << "矩阵为:" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
void multiplyMatrix(int a[3][3], int b[3][3]) {
int temp[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
temp[i][j] = 0;
for (int k = 0; k < 3; k++) {
temp[i][j] += a[i][k] * b[k][j];
}
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
a[i][j] = temp[i][j];
}
}
}
int main() {
int a[3][3], b[3][3];
inputMatrix(a);
outputMatrix(a);
inputMatrix(b);
outputMatrix(b);
multiplyMatrix(a, b);
cout << "相乘后的矩阵为:" << endl;
outputMatrix(a);
return 0;
}
```
以上程序中,`inputMatrix`函数用于输入矩阵,`outputMatrix`函数用于输出矩阵,`multiplyMatrix`函数用于矩阵相乘。相乘后的结果放在了原来的矩阵`a`中。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)