C语言代码写出编写一个函数 该函数检查相等长度的两个数组是否包含相同的元素,若包含相同的元素,
时间: 2024-04-29 13:26:46 浏览: 19
返回1,否则返回0。
下面是检查相等长度的两个数组是否包含相同的元素的C语言代码:
```
#include <stdio.h>
int check_equal_arrays(int arr1[], int arr2[], int len)
{
int i, j;
for (i = 0; i < len; i++) {
for (j = 0; j < len; j++) {
if (arr1[i] == arr2[j]) {
return 1;
}
}
}
return 0;
}
int main()
{
int arr1[] = {1, 2, 3};
int arr2[] = {4, 5, 6};
int is_same = check_equal_arrays(arr1, arr2, 3);
printf("The arrays are%s equal.\n", is_same ? "" : " not");
int arr3[] = {1, 2, 3};
int arr4[] = {3, 4, 5};
is_same = check_equal_arrays(arr3, arr4, 3);
printf("The arrays are%s equal.\n", is_same ? "" : " not");
return 0;
}
```
输出结果:
```
The arrays are not equal.
The arrays are equal.
```
注:该函数假定两个数组都是整数数组,且长度相等。如果两个数组的元素个数不相等,则该函数不适用。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)