用C语言编写程序:判断一个N*N的矩阵是否为对称矩阵. 输出原矩阵及判断结果. N用宏常量,矩阵元素为整数,通过键盘输入
时间: 2024-11-11 21:22:11 浏览: 31
在C语言中,你可以使用循环结构和数组来编写一个函数,用于判断给定的N*N矩阵是否是对称的。下面是一个简单的示例代码:
```c
#include <stdio.h>
#define N 5 // 宏定义矩阵大小
// 函数声明,用于判断矩阵是否对称
int is_symmetric(int matrix[N][N]);
int main() {
int matrix[N][N];
printf("请输入一个 %dx%d 的矩阵:\n", N, N);
// 输入矩阵
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
scanf("%d", &matrix[i][j]);
}
}
// 输出原始矩阵
printf("原始矩阵:\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
// 判断并输出结果
int result = is_symmetric(matrix);
if (result) {
printf("这个矩阵是对称的.\n");
} else {
printf("这个矩阵不是对称的.\n");
}
return 0;
}
// 判断矩阵是否对称的函数
int is_symmetric(int matrix[N][N]) {
for (int i = 0; i < N / 2; i++) { // 只需要检查上半部分,因为对角线是对称的
for (int j = 0; j < i + 1; j++) { // 对于每一行,检查对应列的值是否相等
if (matrix[i][j] != matrix[j][i]) {
return 0; // 如果发现不相等,直接返回0表示不对称
}
}
}
return 1; // 没有找到不相等的元素,说明矩阵是对称的,返回1
}
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)