用C语言编译:设计一个子函数,判断二维数组是否为对称数组(对称矩阵),如果是,则返回1;如果不是,则返回0,并在主函数中调用此函数,判断一个4×4的数组是否为对称数组
时间: 2024-05-15 22:14:19 浏览: 124
以下是用C语言编写的代码:
```c
#include <stdio.h>
int isSymmetric(int arr[][4], int n) {
for(int i = 0; i < n; i++) {
for(int j = i + 1; j < n; j++) {
if(arr[i][j] != arr[j][i]) {
return 0;
}
}
}
return 1;
}
int main() {
int arr[4][4] = {
{1, 2, 3, 4},
{2, 5, 6, 7},
{3, 6, 8, 9},
{4, 7, 9, 10}
};
if(isSymmetric(arr, 4)) {
printf("The array is symmetric.\n");
} else {
printf("The array is not symmetric.\n");
}
return 0;
}
```
解释一下:
- `isSymmetric` 函数接收一个二维数组和数组的行数 `n`,它会遍历数组中的每个元素,检查是否满足对称矩阵的条件(即 `arr[i][j] == arr[j][i]`),如果不满足,则返回 0,否则返回 1。
- 在 `main` 函数中,我们定义了一个 4×4 的数组,并调用了 `isSymmetric` 函数来判断它是否为对称数组。如果是对称数组,输出 `"The array is symmetric."`,否则输出 `"The array is not symmetric."`。
在上面的例子中,数组是对称数组,所以程序输出 `"The array is symmetric."`。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![.rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.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://img-home.csdnimg.cn/images/20241226111658.png)