华中科技大学2003-2006级信息学院《C语言程序设计》考试试题及答案(DOC)

2003级信息学院《C语言程序设计》考试试题 系 班级

姓名 学号 成绩 一. 判断下列语句或程序的对错。(“×”表示错,“√”表示对)(10分)

1 int x=y=z=20; ( × ) y,z需要在前定义 2 #define M 50; ( × ) 不能有分号 3 printf(“%\\t”,(40,50,60)); ( √ ) 4 float a[100],*p;

p=a++; ( × ) 数组名是地址常量,不能做++操作 5 char str[20];

str[20]=0; ( × ) str[20]单元不可用

6 int data[4]={0,1,2,3,4}; ( × ) 0,1,2,3,4是5个元素,与data[4]不匹

7 int x=’\\014’; ( √ )

8 int xyz.1=2; ( × ) xyz.1不是合法的标识符(变量名) 9 char *p=”c:\\test\\prg.c”; ( × ) 修改为c:\\\\test\\\\prg.c 10 int *p;

*p=20; ( × ) 指针不能用常量进行赋值

二. 计算下列表达式的值 (10分)

设unsigned int a=15,b=21,c=5,d=1; float f;

(1) f=b/c ( 4.0 ) b/c是整数除,先得到4,然后赋值给f (2) !(a+b)+c-1&&b+c/2 ( 1 ) (3) a^b+b&0x55+c<<3|015 ( 0x000f )

(4) d*=c--%b++ ( 5 ) (5) a%=(c%=2) ( 0 )

三.程序改错(10分)

1从键盘上输入三个字符串,然后按字典(从小到大)顺序进行输出 # include # include

void swap(char *,char *); //函数声明需要分号结束 main() {

char a[80],b[80],c[80];

scanf(“%s%s%s”,&a,&b,&c); //去掉&号,数组名即表示地址

if(a>b)swap(a,b); strcmp(a,b)>0 a,b比较需要用字符串比较函数 if(b>c)swap(b,c); strcmp(b,c)>0 b,c比较需要用字符串比较函数 printf(“%s\\n%s\\n%s\\n”,a,b,c); }

void swap(char *pstr1,char *pstr2) {

char *p; //char p[80];

p=pstr1; //strcpy(p,pstr1);字符串赋值需要用拷贝函数

pstr1=pstr2; //strcpy(pstr1,pstr2); pstr2=p; //strcpy(pstr2,p);

}

(2)求某班30个学生数学成绩的最高分和平均分. # include

flaot process(float *p1,int n,int *p2); 添加函数声明 main() {

float a[30],aver; int ;

for(m=0;m<30;m++) scanf(“%f”,&a[m]);

max=process(a,30,&aver);

printf(“max=%f,ave=%f\\n),max,aver); }

float process(float *p1,int n,int *p2) {

char x;

int temp; //float temp 与函数返回类型匹配 for(x=0;x<=n;x++) //for(x=0;x

if (p1[x]>temp) temp=p1[x]; *p2+=p1[x]; }

p2=p2/n; //*p2=*p2/n; return temp;

}

四.程序填空

(1) 有一分数系列:2/1,3/2,/5/3,8/5,13/8,21/13……求出这数列的前50项和 # include main() {

int m;

float__ ________; // t,s=0,a=2,b=1 for(m=1;___ _______;m++) //m<=50 {

s=s+a/b; t=a;

a=_a+b_____; b=_t_; }

printf(“sum=%f\\n:,s);

}

(2) 按如图所示格式打印杨辉三角形的前10行 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 ……… main() {

int m,n,a[10][10]; for(m=0;m<10;m++) {

a[m][0]=1;

__________________; a[m][m]=1; }

for(__m=2___;m<10;m++)

for(___n=1____;__n

for(n=0;__n<=m_____________;n++) printf(“m”,a[m][n]); _printf(“]”,a[m][n]); } }

五.写输出结果 (1)

# include void main() {

int m,n,p,s; s=0;

for(m=1;m<=4;m++) {

for(n=1;n<=m;n++) { p=1;

for(n=1;n<=m;n++) p=p*n; }

s=s+p; }

printf(“s=%d\\n”,s);

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4