用C++编写函数判断两个int数组是否相等
时间: 2024-05-08 10:20:17 浏览: 99
好的,我可以帮您回答您的问题。
以下是用 C 编写函数判断两个 int 数组是否相等的示例代码:
```c
#include <stdio.h>
#include <stdbool.h> // 需要使用 bool 类型
bool isEqual(int arr1[], int arr2[], int size)
{
for (int i = 0; i < size; i++) {
if (arr1[i] != arr2[i]) {
return false;
}
}
return true;
}
int main()
{
int arr1[] = {1, 2, 3, 4, 5};
int arr2[] = {1, 2, 3, 4, 5};
int arr3[] = {1, 2, 3, 4, 6};
int size = sizeof(arr1) / sizeof(arr1[0]);
if (isEqual(arr1, arr2, size)) {
printf("Array arr1 and arr2 are equal.\n");
} else {
printf("Array arr1 and arr2 are not equal.\n");
}
if (isEqual(arr1, arr3, size)) {
printf("Array arr1 and arr3 are equal.\n");
} else {
printf("Array arr1 and arr3 are not equal.\n");
}
return 0;
}
```
输出结果为:
```
Array arr1 and arr2 are equal.
Array arr1 and arr3 are not equal.
```
希望这个代码示例对您有所帮助!
阅读全文