收藏 分销(赏)

二级C填空修改.doc

上传人:仙人****88 文档编号:8369802 上传时间:2025-02-11 格式:DOC 页数:136 大小:601.55KB 下载积分:10 金币
下载 相关
二级C填空修改.doc_第1页
第1页 / 共136页
二级C填空修改.doc_第2页
第2页 / 共136页


点击查看更多>>
资源描述
浮莱伴您fly—C语言程序填空修改 浮莱培训 改错题(第二题modi.c) 改错题1、 给定程序modi.c中,函数fun的功能是:给定n个实数,输出平均值,并统计在平均值以上(含平均值)的实际个数。 例如,n=8时输入:193.199、195.673、195.757、196.051、196.092、196.596、196.579、196.763所得平均值为:195.838745,在平均值以上的实数个数应为:5 请改正程序中的错误,使它能得出正确结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构! #include <stdio.h> #include <windows.h> int fun(float x[],int n) /************found************/ int j,c=0;float xa=0.0; for(j=0;j<n;j++) xa+=x[j]/n; printf("ave=%f\n",xa); for(j=0;j<n;j++) /************found************/ if(x[j]=>xa) c++; return c; } main() {float x[100]={193.199f,195.673f,195.757f,196.051f,196.092f,196.596f,196.579f,196.763f}; system("cls"); printf("%d\n",fun(x,8)); } 改错题2、 给定程序modi.c中,fun函数的功能是:将n个无序整数从小到大排序。请改正程序中的错误,使它能得出正确结果。 fun(int n,int *a) {int i,j,p,t; for (j=0;j<n-1;j++) {p=j; /************found************/ for (i=j+1;i<n-1;i++) if (a[p] <a[i]) /************found************/ t=i; if (p!=j) {t=a[j];a[j]=a[p];a[p]=t;} } } putarr(int n,int *z) {int i; for (i=1;i<=n;i++,z++) {printf("%4d",*z); if (!(i%10)) printf("\n"); }printf("\n"); } main() { int aa[20]={9,3,0,4,1,2,5,6,8,10,7},n=11; system("cls"); printf("\n\nBefore sorting %d numbers:\n",n);putarr(n,aa); fun(n,aa); printf("\nAfter sorting %d numbers:\n",n);putarr(n,aa); } 改错题3、 给定程序modi.c中,函数fun的功能是:按顺序给s所指数组中的元素赋予从2开始的偶数,然后再按顺序对每五个元素求一个平均值,并将这些值依次存放在w所指的数组中,若s所指数组中元素的个数不是5的倍数,多余部分忽略不计。 例如,s所指数组有14个元素,则只对前10个元素进行处理,不对最后的4个元素求平均值。 #include <stdio.h> #define SIZE 20 fun(double *s,double *w) { int k,i; double sum; for (k=2,i=0;i<SIZE;i++) { s[i]=k; k+=2; } /************found************/ sun=0.0; for(k=0,i=0;i<SIZE;i++) { sum+=s[i]; /************found************/ if (i+1%5==0) { w[k]=sum/5;sum=0;k++;} } return k; } main( ) { double a[SIZE],b[SIZE/5]; int i,k; k=fun(a,b); printf("The original data:\n"); for (i=0;i<SIZE;i++) { if(i%5==0) printf("\n"); printf("%4.0f",a[i]); } printf("\n\nThe result:\n"); for(i=0;i<k;i++) printf("%6.2f ",b[i]); printf("\n\n"); } 改错题4、 给定程序modi.c中,函数fun的功能是:将从键盘上输入的每个单词的第一个字母写为大写字母,输入时各单词必须用空格隔开,用'.'结束输入。 #include <stdio.h> int fun(char *c,int status) { /************found************/ if (c==' ') return 1; else { if(status && *c<='z' && *c>='a') /************found************/ c+='A'-'a'; return 0; } } main() { int flag=1; char ch; printf("请输入一字符串,用点号结束输入!\n"); do { ch=getchar(); flag=fun(&ch,flag); putchar(ch); }while(ch!='.'); printf("\n"); } 改错题5、 给定程序modi.c中,函数fun的功能是:将十进制正整数m转换成k(2≤k≤9)进制数,并按位输出。例如,若输入8和2,则应输出1000(即十进制数8转换成二进制表示是1000)。 #include <stdio.h> #include <windows.h> void fun(int m,int k) { int aa[20],i; for (i=0;m;i++) { /************found************/ aa[i]=m/k; m/=k; } /************found************/ for(;i;i--) printf("%d",aa[i]); } main() { int b,n; system("cls"); printf("\nPlease enter a number and a base:\n"); scanf("%d %d",&n,&b); fun(n,b); printf("\n"); } 改错题6、 给定程序modi.c中,函数fun的功能是:根据整型形参m,计算如下公式的值。 例如,若m=2000,则应输出:0.000160。 #include <conio.h> #include <stdio.h> #include <windows.h> /************found************/ fun (int m) { double y=0,d; int i; /************found************/ for (i=100,i<=m,i+=100) { d=(double)i*(double)i; y+=1.0/d; } return(y); } main() { int n=2000; system("cls"); printf("\nThe result is %lf\n",fun(n)); } 改错题7、 给定程序modi.c中,函数fun的功能是:读入一个字符串(长度<20),将该字符串中的所有字符按ASCII码升序排序后输出。 例如:若输入:edcba,则应输出:abcde。 #include <stdio.h> #include <windows.h> #define M 20 void fun(char t[]) { char c; int i,j; /************found************/ for(i=strlen(t);i;i--) for(j=0;j<i;j++) /************found************/ if(t[j]<t[j+1]) { c=t[j]; t[j]=t[j+1]; t[j+1]=c; } } main() { char s[81]; system("cls"); printf("\nPlease enter a character string:"); gets(s); printf("\n\nBefore sorting:\n \"%s\"",s); fun(s); printf("\nAfter sorting decendingly:\n \"%s\"",s); } 改错题8、 给定程序modi.c中,函数fun的功能是:从低位开始取出长整型变量s中偶数位上的数,依次构成一个新数放在t中。 例如,当s中的数为:7654321时,t中的数为:642。 #include <conio.h> #include <stdio.h> #include <windows.h> /************found************/ void fun(long s,long t) { long s1=10; s/=10; *t=s%10; /************found************/ while(s<0) { s=s/100; *t=s%10*s1+*t; s1=s1*10; } } main() { long s,t; system("cls"); printf("\nPlease enter s:");scanf("%ld",&s); fun(s,&t); printf("The result is:%ld\n",t); } 改错题9、 给定程序modi.c中,函数fun的功能是:通过某种方式实现两个变量值的交换,规定不允许增加语句和表达式。例如变量a中的值为3,b中的值原为8,程序运行后a中的值为8,b中的值为3。 #include <conio.h> #include <stdio.h> #include <windows.h> int fun(int *x,int y) { int t; /************found************/ t=x;x=y; /************found************/ return(y); } main() { int a=3,b=8; system("cls"); printf("%d %d\n",a,b); b=fun(&a,b); printf("%d %d\n",a,b); } 改错题10、 给定程序modi.c中,函数fun的功能是:输出M行M列整数方阵,然后求两条对角线上各元素之和,返回此和数。 #include <conio.h> #include <stdio.h> #include <windows.h> #define M 5 /************found************/ int fun(int n,int xx[][]) { int i,j,sum=0; printf("\nThe %d x %d matrix:\n",M,M); for(i=0;i<M;i++) { for(j=0;j<M;j++) /************found************/ printf("%4f",xx[i][j]); printf("\n"); } for(i=0;i<n;i++) sum+=xx[i][i]+xx[i][n-i-1]; return(sum); } main() { int aa[M][M]={{1,2,3,4,5},{4,3,2,1,0},{6,7,8,9,0},{9,8,7,6,5},{3,4,5,6,7}}; system("cls"); printf("\nThe sum of all elements on 2 diagnals is %d.",fun(M,aa)); } 改错题11、 给定程序modi.c中,函数fun的功能是:求出以下分数序列的前n项之和。 和值通过函数值返回main函数。例如,若n的值为:5,则应输出:3.007051。 #include <stdio.h> double fun(int n) { int i; double t,s,a,b,c; /************found************/ s=1; a=1;b=2; for(i=0;i<n;i++) { t=a/b; s=s+t; /************found************/ ______; a=b; b=c; } return s; } main() { int n; printf("Enter n: ");scanf("%d",&n); printf("\nThe result: %f \n",fun(n)); } 改错题12、 给定程序modi.c中,函数fun的功能是:计算正整数num的各位上的数字之积,例如,若输入:252,则输出应该是:20。若输入:202,则输出应该是:0。 #include <windows.h> long fun(long num) { /************found************/ long k; do { k*=num%10; /************found************/ num\=10; } while(num); return (k); } main() { long n; system("cls"); printf("Please enter a number:");scanf("%ld",&n); printf("\n%ld\n",fun(n)); } 改错题13、 给定程序modi.c中,函数fun的功能是:计算函数F(x,y,z)=(x+y)/(x-y)+(z+y)/(z-y)的值。其中x和y的值不等,z和y的值不等。 例如,当x的值为9、y的值为11、z的值为15时,函数值为-3.50。 #include <stdio.h> #include <math.h> #define FU(m,n) (m/n) float fun(float a,float b,float c) { float value; /************found************/ value=FU(a+b,a-b)+FU(c+b,c-b); /************found************/ Return(Value); } main() { float x,y,z,sum; printf("Input x y z: "); scanf("%f%f%f",&x,&y,&z); printf("x=%f,y=%f,z=%f\n",x,y,z); if (x==y||y==z) {printf("Data error!\n");} sum=fun(x,y,z); printf("The result is : %5.2f\n",sum); } 改错题14、 给定程序modi.c中,函数fun的功能是:求两数平方根之和,作为函数值返回。 例如,输入12和20,输出结果是:y=7.936238。 #include <stdio.h> #include <conio.h> #include <math.h> #include <windows.h> /************found************/ double fun(double *a,*b) { double c; /************found************/ c=sqr(a)+sqr(b); return c; } main() { double a,b,y; system("cls"); printf("Enter a&b :");scanf("%lf%lf",&a,&b); y=fun(&a,&b); printf("y=%f\n",y); } 改错题15、 给定程序modi.c中,函数fun的功能是:用递归算法计算斐波拉契级数数列中第n项的值,从第1项起,斐波拉契级数序列为:1、1、2、3、5、8、13、21、…… 例如,若给n输入7,该项的斐波拉契级数值为:13。 #include <stdio.h> long fun(int g) { /************found************/ switch(g); { case 0:return 0; /************found************/ case 1;case 2:return 1; } return(fun(g-1)+fun(g-2)); } main( ) { long fib; int n; printf("Input n: ");scanf("%d",&n);printf("n=%d\n",n); fib=fun(n); printf("fib=%d\n\n",fib); } 改错题16、 给定程序modi.c中,函数fun的功能是:用插入排序法将n个字符进行排序(降序)。(提示:插入法排序的思路是:先对数组的头两个元素进行排序,然后根据前两个元素的情况插入第三个元素,再插入第四个元素…)。 #define N 81 #include <stdio.h> #include <string.h> void fun(char *aa,int n) { int a,b,t; for (a=1;a<n;a++) { t=aa[a];b=a-1; /************found************/ While((b>=0)&&(t>aa[b])) {aa[b+1]=aa[b];b--;} /************found************/ aa[b+1]=t } } main() {char a[N]; printf("\nEnter a string:");gets(a); fun(a,strlen(a)); printf("\nThe string:");puts(a); } 改错题17、 给定程序modi.c中函数fun的功能是:判断字符ch 是否与字符串str中的某个字符相同,若相同,什么也不做;若不同,则插在串的最后。 #include <conio.h> #include <stdio.h> #include <string.h> #include <windows.h> /**********found**********/ void fun(char str, char ch ) { while ( *str && *str != ch ) str++; /**********found**********/ if ( *str == ch ) { str [ 0 ] = ch; /**********found**********/ str[1] = '0'; } } main( ) { char s[81], c ; system("cls"); printf( "\nPlease enter a string:\n" ); gets ( s ); printf ("\n Please enter the character to search : " ); c = getchar(); fun(s, c) ; printf( "\nThe result is %s\n", s); } 改错题18、 文件modi.c中程序的功能是:读入一个整数m(1≤m≤20)和m位学生的学号、数学课考分和计算机课考分,并从中查找第一个数学课考分<80且计算机课考分<70的学生,若有则输出他的学号和两门课分数,否则输出"Not found!"。 例如:若输入"4"和 101 91 86 | 若输入"5"和 101 91 86 213 87 75 | 213 87 75 345 79 67 | 345 79 81 420 83 87 | 420 83 87 | 537 65 77 则输出 The one found: | 则输出 Not found. 345 79 67 | #include <conio.h> #include <stdio.h> #include <windows.h> #define M 10 struct student { int num; int math; int cmpt; }; int Find( int n, struct student ss[] ) { int i; for( i = 0; i < n; i++ ) if( ( ss[i].math < 80 )&&( ss[i].cmpt < 70 ) ) break; /*************found************/ ____________________ } main() { int i, m; struct student tt[M]; system("cls"); printf( "\nPlease enter number of students: " ); scanf( "%d", &m ); printf( "\nPlease enter their num and marks of math and cmpt: \n" ); /*************found************/ for( i = 0; i < m; i++ ) scanf( "%d %d %d", tt[i].num, tt[i].math, tt[i].cmpt ); printf( "\nThe students' num and marks entered: " ); for( i = 0; i < m; i++ ) printf( "\n%3d %3d %3d", tt[i].num, tt[i].math, tt[i].cmpt ); if( ( i = Find( m, tt ) ) == -1 ) printf( "\nNot found!\n" ); else { printf( "\nThe one found:" ); printf( "\n%3d %3d %3d\n", tt[i].num, tt[i].math, tt[i].cmpt ); } } 改错题19、 文件modi.c中程序的功能是:读入一个整数m,计算如下公式的值。 t = 1 - 1/2 - 1/3 - …… - 1/m 例如,若输入5,则应输出 -0.283333。 #include <windows.h> double MyLim( int m ) { double t = 1.0; int i; /************found**********/ for( i = 2; i <= m; i++ ) t = 1.0 -1.0/t; return( t ); } main() { int m ; system("cls"); printf( "\nPlease enter 1 integer numbers:\n" ); /************found**********/ scanf( "%d" &m ) printf( "\n\nThe result is %lf\n", MyLim( m ) ); } 改错题20、 给定程序modi.c中函数fun的功能是:把 m(1≤m≤10)个字符串连接起来,组成一个新串,放入pt中。例如:把3个串:"abc","CD","EF"串连起来,结果是"abcCDEF"。 #include <conio.h> #include <stdio.h> #include <string.h> #include <windows.h> int fun ( char str[][10], int m, char *pt ) { /************found************/ Int k, q, i ; for ( k = 0; k < m; k++ ) { q = strlen ( str [k] ); for (i=0; i<q; i++) /************found************/ pt[i] = str[k,i] ; pt += q ; pt[0] = 0 ; } } main( ) { int n, h ; char s[10][10], p[120] ; system("cls"); printf( "\nPlease enter n:" ) ; scanf("%d", &n) ; printf( "\nPlease enter %d string:\n", n ) ; for ( h = 0; h < n; h++ ) scanf("%s", s[h]) ; fun(s, n, p) ; printf( "\nThe result is : %s\n", p) ; } 改错题21、 给定程序modi.c中函数fun的功能是:将s所指字符串的反序和正序进行连接形成一个新串放在t所指的数组中。例如,当s所指字符串的内容为"ABCD"时,t所指数组中的内容为"DCBAABCD"。 #include <string.h> #include <windows.h> void fun (char *s, char *t) { int i, d; d = strlen(s); /************found************/ for (i = 1; i<d; i++) t[i] = s[d - 1 - i ]; for (i = 0; i<d; i++) t[ d + i ] = s[i]; /************found************/ t[2*d] = '/0'; } main() { char s[100], t[100]; system("cls"); printf("\nPlease enter string S:"); scanf("%s", s); fun(s, t); printf("\nThe result is: %s\n", t); } 改错题22、 给定程序modi.c中函数fun的功能是:找出100至n(不大于1000)之间三位数字相等的所有整数,把这些整数放在s所指数组中,个数作为函数值返回。 #include <stdio.h> #define N 100 int fun(int *s, int n) { int i,j,k,a,b,c; j=0; for(i=100; i<n; i++) { /**************found**************/ k=n; a=k%10; k/=10; b=k%10; k/=10; /**************found**************/ c=k/10; if( a==b && a==c ) s[j++]=i; } return j; } main() { int a[N], n, num=0, i; do { printf("\nEnter n( <=1000 ) : "); scanf("%d",&n); }while(n > 1000); num = fun( a,n ); printf("\n\nThe result :\n"); for(i=0; i<num; i++)printf("%5d",a[i]); printf("\n\n"); } 改错题23、 给定程序modi.c中函数fun的功能是:计算n的5次方的值(规定n的值大于2、小于8),通过形参指针传回主函数。并计算该值的个位、十位、百位上数字之和作为函数值返回。例如,7的5次方是16807,其低3位数的和值是15。 #include <stdio.h> #include <math.h> int fun( int n ,int *value ) { int d,s,i; /**************found**************/ d=0; s=1; for(i=1; i<=5; i++) d=d*n; *value=d; for(i=1; i<=3; i++) { s=s+d%10; /**************found**************/ s=s/10; } return s; } main() { int n, sum, v; do { printf("\nEnter n( 2<n<8): ");scanf("%d",&n); } while(n<=2||n>=8); sum=fun( n,&v ); printf("\n\nThe result:\n value=%d sum=%d\n\n",v,sum); } 改错题24、 给定程序modi.c中函数fun的功能是:从3个红球,5个白球,6个黑球中任意取出8个作为一组,进行输出。在每组中,可以没有黑球,但必须要有红球和白球。组合数作为函数值返回。正确的组合数应该是15。程序中i的值代表红球数,j的值代表白球数,k的值代表黑球数。 #include <stdio.h> int fun() { int i,j,k,sum=0; printf("\nThe result :\n\n"); /**************found**************/ for(i=0; i<=3; i++) { for(j=1; j<=5; j++) { k=8-i-j; /**************found**************/ if(k>=1 && k<=6) { sum=sum+1; printf("red:%4d white:%4d black:%4d\n",i,j,k); } } } return sum; } main() { int sum; sum=fun(); printf("sum =%4d\n\n",sum); } 改错题25、 给定程序modi.c中函数fun的功能是:为一个偶数寻找两个素
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 小学其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服