1.函数fun()的功能是统计s所指向的一维数组中0出现的次数(存在变量zero中)和1出现的次数(存在变量one中),并输出结果。 请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。不得增行或删行,也不得更改程序的结构!要求新建工程名为p1,c源文件名为a1.c。 #include <stdio.h> void fun( int *s, int n ) { /**************found**************/ int i, one=___1___, zero=___2___; for(i=0; i<n; i++) /**************found**************/ switch(___3___) { case 0: zero++; break; case 1: one ++; } printf( "one : %d zero : %d\n",one, zero); } main() { int a[20]={1,1,1,0,1,0,0,0,1,0,0,1,1,0,0,1,0,1,0,0},n=20; /**************found**************/ fun( ___4___, n ); }
时间: 2023-07-06 13:36:19 浏览: 44
修改后程序如下:
```c
#include <stdio.h>
void fun(int *s, int n) {
int i, one = 0, zero = 0;
for (i = 0; i < n; i++) {
switch(s[i]) {
case 0: zero++; break;
case 1: one++; break;
}
}
printf("one : %d zero : %d\n", one, zero);
}
int main() {
int a[20] = {1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0};
int n = 20;
fun(a, n);
return 0;
}
```
解析:
1. 在函数fun中,需要定义两个变量分别用来统计0出现的次数和1出现的次数,即变量zero和one,初始值都应该为0。
2. 在for循环中,需要遍历数组s中的所有元素,即s[0] ~ s[n-1],并根据元素的值来更新zero和one的值。
3. 在switch语句中,需要根据每个元素的值来决定执行哪个case语句。
4. 在main函数中,需要将数组a和数组长度n作为实参传递给函数fun。