#include <stdio.h> #define N 9 int fun(int a[], int n) { int i,j; j = 0; for (i=0; i<n; i++) if (___1___== 0) { ___2___ = a[i]; j++; } return ___3___; } main() { int b[N]={9,1,4,2,3,6,5,8,7}, i, n; printf("\nThe original data :\n"); for (i=0; i<N; i++) printf("%4d ", b[i]); printf("\n"); n = fun(b, N); printf("\nThe number of even :%d\n", n); printf("\nThe even :\n"); for (i=0; i<n; i++) printf("%4d ", b[i]); printf("\n"); }怎么完善
时间: 2024-03-26 16:41:41 浏览: 17
这是一段 C 语言代码,需要填空来完善函数 fun 的实现。下面是完整代码及注释:
```c
#include <stdio.h>
#define N 9
// 统计数组 a 中有多少个偶数,并将偶数放在数组开头
int fun(int a[], int n) {
int i, j, evenCount; // 声明变量 evenCount 用于记录偶数个数
j = 0;
evenCount = 0; // 初始化偶数个数为 0
for (i = 0; i < n; i++) {
if (a[i] % 2 == 0) { // 判断是否为偶数
a[j] = a[i]; // 将偶数放在数组开头
j++; // j 记录偶数的个数
evenCount++; // 偶数个数加一
}
}
return evenCount; // 返回偶数个数
}
int main() {
int b[N] = {9, 1, 4, 2, 3, 6, 5, 8, 7}, i, n;
printf("\nThe original data :\n");
for (i = 0; i < N; i++) {
printf("%4d ", b[i]);
}
printf("\n");
n = fun(b, N);
printf("\nThe number of even :%d\n", n);
printf("\nThe even :\n");
for (i = 0; i < n; i++) {
printf("%4d ", b[i]);
}
printf("\n");
return 0;
}
```
其中,第一个空需要填写的是 a[i] % 2 == 0,用于判断是否为偶数;第二个空需要填写的是 a[j],用于将偶数放在数组开头;第三个空需要填写的是 evenCount,用于记录偶数的个数。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)