c++写一个对称矩阵的
时间: 2023-01-11 14:55:36 浏览: 86
对称矩阵是一种矩阵,它的对称元素是等于它对应位置的元素,即对于矩阵 A,若有 A[i][j] = A[j][i],则称 A 为对称矩阵。
要在 C 语言中写一个函数来判断一个给定的矩阵是否为对称矩阵,你可以使用以下代码:
```
#include <stdbool.h>
bool isSymmetricMatrix(int matrix[][N], int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] != matrix[j][i]) {
return false;
}
}
}
return true;
}
```
其中 N 是矩阵的大小,isSymmetricMatrix 函数接受一个整型矩阵和它的大小作为参数,并返回一个布尔值,表示该矩阵是否为对称矩阵。
你可以在主函数中使用这个函数来判断给定的矩阵是否为对称矩阵,例如:
```
int main() {
int matrix[N][N] = {{1, 2, 3}, {2, 4, 5}, {3, 5, 6}};
if (isSymmetricMatrix(matrix, N)) {
printf("The matrix is symmetric.\n");
} else {
printf("The matrix is not symmetric.\n");
}
return 0;
}
```
上面的代码将会输出 "The matrix is not symmetric."。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)