资源预览内容
第1页 / 共16页
第2页 / 共16页
第3页 / 共16页
第4页 / 共16页
第5页 / 共16页
第6页 / 共16页
第7页 / 共16页
第8页 / 共16页
第9页 / 共16页
第10页 / 共16页
亲,该文档总共16页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
C C 语言题库之编程语言题库之编程第三部分 编程题1.m 个人的成绩存放在 score 数组中,请编写函数 fun,它的功能是:将低于平均分的人数作为函数值返回,将低于平均分的分数放在below 所指的数组中。例如,当 score 数组中的数据为10、20、30、40、50、60、70、80、90 时,函数返回的人数应该是4,below 中的数据应为 10、20、30、40。?#include #include #include /*第一个 for()循环用来计算 score 数组中分数的总和,然后用aver/=m 求出平均值,第二个循环用来找出小于平均分的元素,并放到数组 below 中,这里要注意 j 的递增方式。*/int fun(int score,int m, int below) int i,j=0,aver=0;for(i=0;i#include /*该题关键就是如何表示能被 7 或 11 整除,但不能同时被 7 和 11整除。用(i%7=0)/(i%11=0)来表示能被 7 或 11 整除,用 !(i%7=0)for(i=1;i#include /*由于要求的数不能是偶数故编程时从 1 开始循环而步长为 2 这样正好保正 i 永远的奇数。这里也要注意存放结果的数组 pp 的下标变化方式。*/void fun(int x, int pp, int *n) int i,j=0;for( i=1;i#include/* 34 题:本题采用的是字母的 ASCII 码值与数组 pp 下标的对应转换关系分别求出对应字母的个数。第一个 for()是给 pp 赋初值 0,第二个 for()用于控制在字符串内从头到尾移动。由于字母a的ASCII 码值为 97 而它的个数要放在 pp0中,而a-97 的值正好为0,其它的依此类推。*/void fun(char *tt,int pp) int i;for(i=0;i=a)pp*tt-97+; main() char aa1000;int bb26,k;clrscr();printf(“nPlease enter a char string:“);scanf(“%s“,aa);fun(aa,bb);for(k=0;k#include /*本题中的 for()循环用来判断一个数是否为素数,由于个数只能是 k 个所以用 do-while 循环来控制。*/void fun(int m, int k, int xx) int i,j=0,p=m+1;dofor( i=2; i=p) xxj+=p;p+; while(j#include #define LEN 20void fun (char a , char b , int n)int i,k=0;for(i=0;ai!=0;i+)if(i!=n) bk+=ai;bk=0; main( )FILE *wf;char strlLEN, str2LEN;int n ;printf (“Enter the string : n“) ;gets (strl) ;printf (“Enter the position of the string deleted: “) ;scanf (“%d“, fun (strl, str2, n) ;printf (“The new string is : %s n“, str2) ;wf=fopen(“out.dat“,“w“);fun(“world“,str2,3);fprintf(wf,“%s“,str2);fclose(wf);7.请编写一个函数 int fun(int *s,int t,int *k),用来求出数组的最大元素在数组中的下标并存放在 k 所指的存储单元中。例如,输入如下整数:876 675 896 101 301 401 980 431 451 777则输出结果为:6,980。?#include#include int fun(int *s,int t,int *k) int i , max ;max=s0;for ( i=0; imax) max=si;*k=i; main()int a10=876,675,896,101,301,401,980,431,451,777,k;clrscr() ;fun(a,10,8.编写函数 fun,函数的功能是:根据以下公式计算 s,计算结果作为函数值返回;n 通过形参传入。s=1+1/(1+2)+1/(1+2+3)+.+1/(1+2+3+4+.+n) 例如:若n 的值为 11 时,函数的值为 1.833333。?#include float fun(int n) int i,s;s=0;for(i=0,in。p=m!/n!(m-n)!。例如:m=12,n=8 时,运行结果为 495.000000。?#include #include float fun( int m, int n) int i,j,k;long int t=1,s=1,p=1;float q;for(i=1; i#include #include float fun() float x0,x1=0;do x0=x1;x1=cos(x0);while( fabs(x0-x1)1e-006);return x1;main() printf(“root=%fn“,fun();11.下列程序定义了 NN 的二维数组,并在主函数中自动赋值。请编写函数 fun(int aN),该函数的功能是:使数组左下半三角元素中的值全部置成 0。例如:a 数组中的值为,则返回主程序后 a 数组中的值应为 ?#include#include#include#define N 5/* 29 题:该题的关键是如何表示出左下半角的元素,当从上往下到第 i 行时只有左边的i 个元素要置为 0(每行总是如此) 。用外层 for()来控制行而内嵌的 for()来控制每行的左边元素,故而在内嵌 for()中 j 最大只能循环到 i(即表示出第 i行左边的 i 个元素) 。*/int fun(int aN)int i,j;for(i=0;i#include #include #define N 5/* 注意不能重复计算,第一个 for()循环求出第一列与最后一列的和,第二个 for()循环求出第一行与最后一行(已除去两头元素) ,要注意第二个 for()中起始为 1,最后为 N-2。*/double fun (int wN) int i,t=0;double s=0;for(i=0;i#include #define M 4#define N 5/*注:该题的第一个 for()循环是计算矩阵的最上一行和最下一行的总和,第二个 for()是计算除两头元素以外的最左一列和最右一列的元素的和,最后 sun 就是周边元素的和。*/int fun(int aMN)int sum=0,i;for(i=0;i#include /* 注:由于 unsigned 型整数在 0-65535 这间,只要它大于 10000则对 10000 求余即得出后面 4 位,否则如果大于 1000 则对 1000 求余得出后 3 位数,这样一层一层往小的判断。由于 return 的作用除了返回值以外,还有当执行到 return 时就跳出该程序,所以可以连续的用 if()语句。 */unsigned fun( unsigned w ) if(w=10000) return w%10000;if(w=1000) return w%1000;if(
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号