判断两个矩阵相等ptaC语言
时间: 2023-07-26 15:24:20 浏览: 126
可以使用循环逐个比较两个矩阵的每一个元素是否相等,如果存在不相等的元素则判断矩阵不相等,否则判断矩阵相等。以下是一个示例代码:
```c
#include <stdio.h>
#include <stdbool.h>
#define ROWS 3
#define COLS 3
bool matrixEqual(int mat1[ROWS][COLS], int mat2[ROWS][COLS]) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (mat1[i][j] != mat2[i][j]) {
return false;
}
}
}
return true;
}
int main() {
int mat1[ROWS][COLS] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int mat2[ROWS][COLS] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int mat3[ROWS][COLS] = {{1, 2, 3}, {4, 5, 6}, {9, 8, 7}};
if (matrixEqual(mat1, mat2)) {
printf("mat1 and mat2 are equal.\n");
} else {
printf("mat1 and mat2 are not equal.\n");
}
if (matrixEqual(mat1, mat3)) {
printf("mat1 and mat3 are equal.\n");
} else {
printf("mat1 and mat3 are not equal.\n");
}
return 0;
}
```
输出结果为:
```
mat1 and mat2 are equal.
mat1 and mat3 are not equal.
```
阅读全文