C ++实现:取矩阵的实部并输出
时间: 2023-10-03 17:08:32 浏览: 63
假设矩阵用二维数组表示,可以使用以下代码实现取矩阵的实部并输出:
```cpp
#include <iostream>
using namespace std;
int main() {
int rows, cols;
cout << "Enter the number of rows and columns for the matrix: ";
cin >> rows >> cols;
double matrix[rows][cols];
// Input the matrix
cout << "Enter the elements of the matrix: " << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> matrix[i][j];
}
}
// Output the real part of the matrix
cout << "The real part of the matrix is:" << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << matrix[i][j].real() << " ";
}
cout << endl;
}
return 0;
}
```
这里使用了 `std::complex` 类型,因为它可以表示复数,其中 `real()` 函数可以返回复数的实部。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)