用c帮我写一个函数查找数组arr={12,34,56,78,53,14,67,88}中88是否在数组中
时间: 2024-03-06 22:46:36 浏览: 25
好的,可以使用以下的 C 语言代码实现:
```c
#include <stdio.h>
int search(int arr[], int size, int target) {
for (int i = 0; i < size; i++) {
if (arr[i] == target) {
return 1;
}
}
return 0;
}
int main() {
int arr[] = {12, 34, 56, 78, 53, 14, 67, 88};
int size = sizeof(arr) / sizeof(arr[0]);
int target = 88;
if (search(arr, size, target)) {
printf("%d is in the array.\n", target);
} else {
printf("%d is not in the array.\n", target);
}
return 0;
}
```
在上面的代码中,`search` 函数接收一个整型数组 `arr`,数组的大小 `size`,以及要查找的目标值 `target`。该函数使用一个循环遍历整个数组,如果找到了目标值,即 `arr[i] == target`,则返回 1,表示目标值存在于数组中。如果循环结束后仍未找到目标值,则返回 0,表示目标值不存在于数组中。
在 `main` 函数中,我们定义了数组 `arr`,并计算出其大小。我们要查找的目标值是 88。然后调用 `search` 函数进行查找,如果返回值为 1,则输出目标值存在于数组中的信息;否则输出目标值不存在于数组中的信息。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)