2017年电大电大年春《C语言程序设计》形成性考核册答案 下载本文

C语言程序设计形成性考核册

6. #include #include void main() {

int i,n;

for(n=2;n<=20;n++){

int temp=(int)sqrt(n);//sqrt(n)求出n的平方根并取整 for(i=2;i<=temp;i++) if(n%i==0)break;

if(i>temp)printf(\ }

printf(\}

输出结果为:

2 3 5 7 11 13 17 19

Press any key to continue

7. #include #include const int M=20; void main() {

int i,c2,c3,c5; c2=c3=c5=0;

for(i=1;i<=M;i++){ if(i%2==0)c2++; if(i%3==0)c3++; if(i%5==0)c5++; }

printf(\}

输出结果为: 10 6 4

Press any key to continue

8. #include #include const int M=20; void main() {

int i,s;

for(i=1,s=0;i<15;i++){

if(i%2==0 || i%3==0)continue; printf(\

共25页 - 5 -

C语言程序设计形成性考核册

s+=i; }

printf(\}

输出结果为: 1 5 7 11 13 37

Press any key to continue

C语言程序设计形成性考核 作业二

一、 选择题

1. 在下面的一维数组定义中,( C )语句有语法错误。

A.int a[]={1,2,3}; B.int a[10]={0}; C.int a[]; D.int a[5];n 2.在下面的二维数组定义中,( C )语句是正确的。

A.int a[5][]; B.int a[][5]; C.int a[][3]={{1,3,5},{2}}; D.int a[](10) 3.假定一个二维数组的定义语句为“int a[3][4]={{3,4},{2,8,6}};”,则元素a[1][2]的值为(C)。 A.2 B.4 C.6 D.8 4.假定一个二维数组的定义语句为“int a[3][4]={{3,4},{2,8,6}};”,则元素a[2][1]的值为(A)。 A.0 B.4 C.8 D.6

5.将两个字符串连接起来组成一个字符串时,选用( C )函数。 A.strlen() B.strcap() C.strcat() D.strcmp()

二、填空题

1.假定一维数组的定义为“char * a[8];”,则该数组所含元素的个数为___8_______。 2.假定一维数组的定义为“char * a[8];”,则该数组所占存储空间的字节数为___32______。 3.假定二维数组的定义为“int a[3][5]”,则该数组所占存储空间的字节数为___60______。 4.假定二维数组的定义为“char a[M][N];”,则该数组所所含元素的个数为__M*N_____。 5.假定二维数组的定义为“double a[M][N];”,则每个数组元素的行下标取值范围在__0~M-1___之间。

6.假定二维数组的定义为“double a[M][N];”,则每个数组元素的列下标取值范围在__0~N-1___之间。

7.使用“typedef char BB[10][50];”语句定义__BB______为含有10行50列的二维字符数组类型。

8.存储字符’a’需要占用存储器的_1_____个字节空间。 9.空字符串的长度为__0_______。

10.存储一个空字符串需要占用__1___个字节。 11.字符串”a:\\\\xxk\\\\数据”的长度为___11______。

12.用于存储一个长度为n的字符串的字符数组的长度至少为__n+1______。 13.strcmp函数用于进行两个字符串之间的___大小比较___。

14.Strcpy函数用于把一个字符串___复制到___另一个字符数组空间中。 15.一个二维字符数组a[10][20]能够存储__ 10___个字符串。

16.一个二维字符数组a[10][20]能够存储的每个字符串的长度至多为___19___.

共25页 - 6 -

C语言程序设计形成性考核册

三、写出下列每个程序运行后的输出结果 1. #include void main() { int a[10]={12,39,26,41,55,63,72,40,83,95}; int i,i1=0,i2=0; for(i=0;i<10;i++) if(a[i]%2==1)i1++;else i2++; printf(\}

输出结果为: 6 4

Press any key to continue

2. #include #include void main() { int i; char *a[5]={\ char *p1,*p2; p1=p2=a[0]; for(i=0;i<5;i++){ if(strcmp(a[i],p1)>0)p1=a[i]; if(strcmp(a[i],p2)<0)p2=a[i]; } printf(\}

输出结果为: worker cadre

Press any key to continue

3. #include

int a[10]={4,5,6,15,20,13,12,7,8,9}; void main() { int i,s0,s1,s2; s0=s1=s2=0; for(i=0;i<10;i++){ switch(a[i]%3){ case 0:s0+=a[i];break; case 1:s1+=a[i];break; case 2:s2+=a[i];break; }

共25页 - 7 -

C语言程序设计形成性考核册

} printf(\ }

输出结果为: 42 24 33

Press any key to continue

4. #include void main() { char a[]=\ int i1=0,i2=0,i=0; while(a[i]){ if(a[i]=='a')i1++; if(a[i]=='b')i2++; i++; } printf(\ }

输出结果为: 2 3 11

Press any key to continue

5. #include void main() { int a[3][4]={{1,2,7,8},{5,6,10,6},{9,12,3,4}}; int m=a[0][0]; int ii=0,jj=0; int i,j; for(i=0;i<3;i++) for(j=0;j<4;j++) if(a[i][j]>m){m=a[i][j];ii=i;jj=j;} printf(\}

输出结果为: 2 1 12

Press any key to continue

共25页 - 8 -