文档详情

C语言编程训练、测试题库(偏难).doc

发布:2017-08-24约12.15万字共163页下载文档
文本预览下载声明
C语言编程训练试题库 第一套 编程题: 1.m个人的成绩存放在score数组中,请编写函数fun,它的功能是:将低于平均分的人数作为函数值返回,将低于平均分的分数放在below所指的数组中。 例如,当score数组中的数据为10、20、30、40、50、60、70、80、90时,函数返回的人数应该是4,below中的数据应为10、20、30、40。 注意:部分源程序给出如下。 请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。 #include conio.h #include stdio.h #include string.h int fun(int score[],int m, int below[]) { int i; double avg; int sum=0,j=0; for(i=0;im;i++) sum+=score[i]; avg=sum/m; for(i=0;i=m;i++) if(score[i]avg) below[j++]=score[i]; return --j; } main() { int i,n,below[9]; int score[9]={10,20,30,40,50,60,70,80,90}; clrscr(); n=fun(score,9,below); printf(\nBelow the average score are :); for(i=0;in;i++) printf(%d ,below[i]); } 8.编写函数fun,函数的功能是:根据以下公式计算s,计算结果作为函数值返回;n通过形参传入。 S=1+1/(1+2)+1/(1+2+3)+……1/(1+2+3+…+n) 例如:若n的值为11时,函数的值为1.833333。 注意:部分源程序给出如下。 请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。 #includeconio.h #includestdio.h #includestring.h float fun (int n) { int i,j; float sum=0.0; float s=0.0; for(i=1;i=n;i++) { sum=0; for(j=0;j=i;j++) sum+=j; s+=1.0/sum; } return s; } main() { int n; float s; clrscr(); printf(\nPlease enter N:); scanf(%d,n); s=fun(n); printf(The result is: %f\n,s); } 改错题: 1.下列给定程序的功能是:读入一个整数k(2≤k≤10000),打印它的所有质因子(即)。例如,若输入整数2310,则应输出:2、3、5、7、11。 请改正程序中的错误,使程序能得出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!试题程序:#include conio.h #include stdio.h /********found********/ IsPrime ( int n ) ;-去掉“;”。 { int i, m; m=1; /********found********/ for(i=2;in;i++) if (!(n%i)) -添加。 { m=0; break; } return(m); } main() { int j, k; clrscr(); printf(\nplease enter an integer number between 2 and 10000:); scanf(%d,k); printf(\n\nThe prime factor(s) of %d is(are):,k); for(j=2;jk;j++) if((!(k%j))(IsPrime(j))) printf( %4d,,j); printf(\n); } 6.下列给定程序中,函数fun的功能是:根据整型形参m的值,计算如下公式的值。T=1- ― - - -… - 2x2 3x3 mxm 例如,若m中的值为5,则应输出:0.536389。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得
显示全部
相似文档