第一部分改错题
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★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;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;j if((!(k%j))&&(IsPrime(j))) printf(" %4d,",j); printf("\n"); } 主要算法:涉及知识点:试题难度:★ ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★2.下列给定程序中,函数fun的功能是:逐个比较a、b两个字符串对应位置中的字符,把ASCII值大或相等的字符依次存放到c数组中,形成一个新的字符串。例如,若a中的字符串为:aBCDeFgH,b中的字符串为:ABcd,则C中的字符串应为:aBcdeFgH。 请改正程序中的错误,使它能得出正确结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。 试题程序: #include #include void fun(char *p,char *q,char *c) { /********found********/ int k=1; /********found********/ while(*p != *q) { if(*p<*q) c[k]=*q; else c[k]=*p; if(*p) p++; if(*q) q++; k++; } } main() { char a[10]="aBCDeFgH",b[10]="ABcd",c[80]={'\0'}; fun(a,b,c); printf("The string a:"); puts(a); printf("The string b:"); puts(b); printf("The result:"); puts(c); } 主要算法:涉及知识点:试题难度:★★★ ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★3.下列给定程序中,函数fun的功能是:依次取出字符串中所有数字字符,形成新的字符串,并取代原字符串。 请改正函数fun中的错误,使它能得出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构! 试题程序: #include #include void fun(char *s) { int i,j; /********found********/ for(i=0,j=0;s[i]!='\0';i++) if(s[i]>='0' && s[i]<='9') s[j]=s[i]; /********found********/ s[j]="\0"; } main() { char item[80]; clrscr(); printf("\nEnter a string :"); gets(item); printf("\n\nThe string is : \%s\n",item); fun(item); printf("\n\nThe string of changing is : \%s\n",item); } 主要算法:涉及知识点:试题难度:★★★ ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★4.下列给定程序中,fun函数的功能是:分别统计字符串中大写字母和小写字母的个数。例如,给字符串S输入:AaaaBBb123CCccccd,则应输出结果:upper=5,lower=9。 请改正程序中的错误,使它能计算出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构! 试题程序: #include #include /********found********/ void fun(char *s,int a,int b) { while(*s) { /********found********/ if (*s>='A'&&*s<='Z') a++; /********found********/ if(*s>='a'&&*s<='z') b++; s++; } } main() { char s[100]; int upper=0, lower=0; clrscr(); printf("\nPlease a string : "); gets(s); fun(s,&upper,&lower); printf("\n upper=%d lower=%d\n", upper,lower); } 主要算法:涉及知识点:试题难度:★★ ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★5.假定整数数列中的数不重复,并存放在数组中。下列给定程序中,函数fun的功能是:删除数列中值为X的元素,同时将其它元素前移。n中存放的是数列中元素的个数。 请改正程序中的错误,使它能得出正确结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。 试题程序: #include #define N 20 fun(int *a,int n,int x) { int p=0,i; a[n]=x; while(x!=a[p]) { p=p+1; } if(p==n) return -1; else { /********found********/ for(i=p;i a[i+1]=a[i]; return n-1; } } main() { int w[N]={-3,0,1,5,7,99,10,15,30,90},x,n,i; n=10; printf("The original data:\n"); for(i=0;i printf("\nInput x (to delete):"); scanf("%d",&x); printf("Delete :%d\n",x); n=fun(w,n,x); if(n==-1) printf("***Not be found!***\n\n"); else { printf("The data after delete :\n"); for(i=0;i printf("%5d",w[i]); printf("\n\n"); } } 主要算法:涉及知识点:试题难度:★★★ ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★6.下列给定程序中,函数fun的功能是:根据整型形参m的值,计算如下公式的值。 1 1 1 T=1- ― - - -…- 2x2 3x3 mxm 例如,若m中的值为5,则应输出:0.536389。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构! 试题程序: #include #include double fun(int m) { double y=1.0; int i; /********found********/ for(i=2;i /********found********/ y-=1/(i*i); return(y); } main() { int n=5; clrscr(); printf("\nThe result is %1f\n",fun(n)); } 主要算法:涉及知识点:试题难度:★ ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★7.下列给定程序中函数fun的功能是:用选择法对数组中的n个元素按从小到大的顺序进行排序。 请修改程序中的错误,使它能计算出正确的结果。 注意:不要改动main函数,不得增行和删行,也不得更改程序的结构! 试题程序: #include #define N 20 void fun(int a[],int n) { int i,j,t,p; for(j=0;j { /********found********/ p=j for(i=j;i { if(a[i] { /********found********/ p=i; t=a[p]; a[p]=a[i]; a[i]=t; } } } } main() { int a[N]={9,6,8,3,-1},i,m=5; printf("dfrgddfgd:"); for(i=0;i printf("%d",a[i]); printf("\n"); fun(a,m); printf("gfdghfg : "); for(i=0;i printf("%d",a[i]); printf("\n"); } 主要算法:涉及知识点:试题难度:★★ ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★8.下列给定程序中,函数fun的功能是:在字符串str中找出ASCII码值最大的字符,将其放在第一个位置上;并将该字符前的原字符向后顺序移动。例如,调用fun函数之前给字符串输入:ABCDeFGH,调用后字符串中的内容为:eABCDFGH。 请修改程序中的错误,使它能计算出正确的结果。 注意:不要改动main函数,不得增行和删行,也不得更改程序的结构! 试题程序: #include /********found********/ fun(char *p) { char max,*q; int i=0; max=p[i]; while(p[i]!=0) { if(max { /********found********/ max=p[i]; p=q+i; } i++; } while(q>p) { *q=*(q-1); q--; } p[0]=max; } main() { char str[80]; printf("Enter a string: "); gets(str); printf("\nThe original string: "); puts(str); fun(str); printf("\nThe string after moving: "); puts(str); printf("\n\n"); } 主要算法:涉及知识点:试题难度:★★★ ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★9.下列给定程序中,函数fun的功能是:从n个学生的成绩中统计出低于平均分的学生人数,此人数由函数值返回,平均分存放在形参aver所指的存储单元中。例如,若输入8名学生的成绩: 80.5 60 72 90.5 98 51.5 88 64 则低于平均分的学生人数为4(平均分为:75.5625)。 请改正程序中的错误,使它能统计出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构! 试题程序: #include #include #define N 20 int fun(float *s,int n,float *aver) { float ave,t=0.0; int count=0,k,i; /********found********/ for(k=0;k t=s[k]; ave=t/n; for(i=0;i if(s[i] count++; /********found********/ *aver=&ave; return count; } main() { float s[30],aver; int m,i; clrscr(); printf("\nPlease enter m: "); scanf("%d", &m); printf("\nPlease enter %d mark :\n ", m); for(i=0;i scanf("%f",s+i); printf("\nThe number of students : %d\n",fun(s,m,&aver)); printf("Ave=%f\n",aver); } 主要算法:涉及知识点:试题难度:★★ ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★10.下列给定程序中,函数fun的功能是:将s所指字符串中出现的t1所指子串全部替换成t2所指子字符串,所形成的新串放在w所指的数组中。在此处,要求t1和t2所指字符串的长度相同。例如,当s指字符串中的内容为abcdabfab,t1指子串中的内容为ab,t2所指子串中的内容为99时,结果,在w所指的数组中的内容应为99cd99f99。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构! 试题程序: #include #include #include /********found********/ int fun(char *s,char *t1,char *t2,char *w) { int i; char *p,*r,*a; strcpy(w,s); while(*w) { p=w; r=t1; /********found********/ while(r) if(*r==*p) { r++;p++; } else break; if(*r=='\0') { a=w; r=t2; while(*r) { *a=*r; a++; r++;} w+=strlen(t2); } else w++; } } main() { char s[100],t1[100],t2[100],w[100]; clrscr(); printf("\nPlease enter string s:"); scanf("%s",s); printf("\nPlease enter substring t1:"); scanf("%s",t1); printf("\nPlease enter substring t2:"); scanf("%s",t2); if(strlen(t1)==strlen(t2)){ fun(s,t1,t2,w); printf("\nThe result is :%s\n",w); } else printf("Error :strlen(t1)!=strlen(t2)\n"); } 主要算法:涉及知识点:试题难度:★★★★ ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★11.下列给定程序中,函数fun的功能是:将一个由八进制数字字符组成的字符串转换为与其面值相等的十进制整数。规定输入的字符串最多只能包含5位8进制数字。例如,若输入77777,则输出将是32767。 请改正程序中的错误,使它能得出正确结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。 试题程序: #include int fun(char *p) { int n; /********found********/ n=*p-'o'; p++; /********found********/ while(*p!=0) { n=n*7+*p-'o'; p++; } return n; } main() { char s[6]; int i; int n; printf("Enter a string (0catal digits): "); gets(s); if(strlen(s)>5) { printf("Error:string too longer !\n\n"); exit(0); } for(i=0;s[i];i++) if(s[i]<'0'||s[i]>'7') { printf("Error: %c not is ocatal digits!\n\n",s[i]); exit(0); } printf("The original string: "); puts(s); n=fun(s); printf("\n%s is convered to intege number: %d\n\n",s,n); } 主要算法:涉及知识点:试题难度:★★ ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★12.下列给定程序中函数fun的功能是:求出在字符串中最后一次出现的子字符串的地址,通过函数值返回,在主函数中输出从此地址开始的字符串;若未找到,则函数值为NULL。 例如,当字符串中的内容为abcdabfabcdx,t中的内容为ab时,输出结果应是:abcdx。当字符串中的内容为abcdabfabcdx,t中的内容为abd时,则程序输出未找到信息:not be found!。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构! 试题程序: #include #include #include char *fun(char *s,char *t) { char *p,*r,*a; /********found********/ a=NuLL; while(*s) { p=s;r=t; while(*r) /********found********/ if(r==p) { r++; p++; } else break; if(*r=='\0') a=s; s++; } return a; } main() { char s[100],t[100],*p; clrscr(); printf("\nplease enter string s:"); scanf("%s",s); printf("\nplease enter substring t:"); scanf("%s",t); p=fun(s,t); if(p) printf("\nthe result is:%s\n",p); else printf("\nnot found!\n"); } 主要算法:涉及知识点:试题难度:★★★ ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★13.下列给定程序中,fun函数的功能是:根据形参m,计算如下公式的值。 1 1 1 1 t=1+ - + - + -… + - 2 3 4 m 例如,若输入5,则应输出2.283333。 请改正程序中的错误或在横线处填上适当的内容并把横线删除,使它能计算出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构! 试题程序: #include #include double fun(int m) { double t=1.0; int i; /********found********/ for(i=2;i<=m;i++) t+=1.0/k; /********found********/ ___填空____ } main() { int m; clrscr(); printf("\nplease enter 1 integer numbers:\n"); scanf("%d",&m); printf("\n\nthe result is %lf\n",fun(m)); } 主要算法:涉及知识点:试题难度:★ ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★14.下列给定程序中,函数fun和funx的功能是:用二分法求方程2x3-4x2+3x-6=0的根,并要求绝对误差不超过0.001。例如,若给m输入-100,给n输入90,则函数求得的一个根值为2.000。 请改正程序中的错误,使它能得出正确结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。 试题程序: #include "stdio.h" #include "math.h" double funx(double x) { return (2*x*x*x-4*x*x+3*x-6); } double fun(double m,double n) { /********found********/ int r; r=(m+n)/2; /********found********/ while(fabs(n-m)<0.001) { if(funx(r)*funx(n)<0) m=r; else n=r; r=(m+n)/2; } return r; } main() { double m,n,root; printf("Enter m n : \n"); scanf("%lf%lf",&m,&n); root=fun(m,n); printf("root=%6.3f\n",root); } 主要算法:涉及知识点:试题难度:★★ ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★15.下列给定程序中,函数fun的功能是:判断字符ch是否与str所指串中的某个字符相同;若相同,则什么也不做,若不同,则将其插在串的最后。 请改正程序中的错误,使它能进行正确的操作。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构! 试题程序: #include #include #include /********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; clrscr(); printf("\n Please 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); } 主要算法:涉及知识点:试题难度:★★ ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★16.下列给定程序中的函数Creatlink的功能是:创建带头结点的单向链表,并为各结点数据域赋0到m-1的值。 请改正函数Creatlink中的错误,使它能得出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构! 试题程序: #include #include #include typedef struct aa { int data; struct aa *next; } NODE; NODE *Creatlink(int n, int m) { NODE *h=NULL, *p, *s; int i; s=(NODE *)malloc(sizeof(NODE)); h=p; /********found********/ p->next=NULL; for(i=1;i { s=(NODE *)malloc(sizeof(NODE)); /********found********/ s->data=rand()%m; s->next=p->next; p->next=s; p=p->next; } /********found********/ return p; } outlink(NODE *h) { NODE *p; p=h->next; printf("\n\nTHE LIST :\n\n HEAD"); while(p) { printf("->%d ",p->data); p=p->next; } printf("\n"); } main() { NODE *head; clrscr(); head=Creatlink(8,22); outlink(head); } 主要算法:涉及知识点:试题难度:★★ ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★17.下列给定程序中,函数fun的功能是:计算并输出k以内最大的10个能被13或17整除的自然数之和。k的值由主函数传入,若k的值为500,则函数值为4622。 请改正程序中的错误,使程序能输出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构! 试题程序: #include #include int fun(int k) { int m=0, mc=0, j; while((k>=2)&&(mc<10)) { /********found********/ if((k%13=0)||(k%17=0)) { m=m+k; mc++; } k--; } return m; /********found********/ 填空_____ main() { clrscr(); printf("%d\n", fun(500)); } 主要算法:涉及知识点:试题难度:★ ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★18.下列给定程序中,函数fun的功能是:实现两个整数的交换。例如给a和b分别输入60和65,输出为:a=65 b=60。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构! 试题程序: #include #include /********found********/ void fun(int a,int b) { int t; /********found********/ t=b; b=a; a=t; } main() { int a,b; clrscr(); printf("Enter a,b: "); scanf("%d%d",&a,&b); fun(&a,&b); printf("a=%d b=%d\n",a,b); } 主要算法:涉及知识点:试题难度:★★ ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★19.下列给定程序中函数fun的功能是:从低位开始取出长整型变量s中偶数位上的数,依次构成一个新数放在t中。例如,当s中的数为7654321时,t中的数为642。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构! 试题程序: #include #include /********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; clrscr(); printf("\nPlease enter s:"); scanf("%ld",&s); fun(s,&t); printf("The result is:%ld\n",t); } 主要算法:涉及知识点:试题难度:★★ ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★20.N个有序整数数列已放在一维数组中,给定下列程序中,函数fun的功能是:利用折半查找算法查找整数m在数组中的位置。若找到,则返回其下标值;反之,则返回-1。 折半查找的基本算法是:每次查找前先确定数组中待查的范围:low和high(low 请改正程序中的错误,使它能得出正确结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。 试题程序: #include #define N 10 /********found********/ void fun(int a[],int m) { int low=0,high=N-1,mid; while(low<=high) { mid=(low+high)/2; if(m high=mid-1; /********found********/ else if(m>=a[mid]) low=mid+1; else return(mid); } return (-1); } main() { int i,a[N]={-3,4,7,9,13,45,67,89,100,180},k,m; printf("a:"); for(i=0;i printf("%d ",a[i]); printf("Enter m:"); scanf("%d",&m); k=fun(a,m); if(k>=0) printf("m=%d,index=%d\n",m,k); else printf("Not be found!\n"); } 主要算法:涉及知识点:试题难度:★★★ ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★21.下列给定程序是建立一个带头结点的单向链表,并用随机函数为各结点数据域赋值。函数fun的作用是求出单向链表结点(不包括头结点)数据域中的最大值,并且作为函数值返回。 请改正函数fun中的错误,使它能得出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构! 试题程序: #include #include #include typedef struct aa { int data; struct aa *next; } NODE; /********found********/ fun(NODE *h) { int max=-1; NODE *p; p=h; while(p) { if(p->data>max) max=p->data; /********found********/ p=h->next; } return max; } outresult(int s,FILE *pf) { 程序改错------------------------------------- 1题目:在一个一维整型数组中找出其中最大的数及其下标。 程序中共有4条错误语句,请改正错误。 -----------------*/ #define N 10 /**********FOUND**********/ float fun(int *a,int *b,int n) { int *c,max=*a; for(c=a+1;cmax) { max=*c; /**********FOUND**********/ b=c-a;} return max; } main() { int a[N],i,max,p=0; printf("please enter 10 integers:\n"); for(i=0;i 二级(C语言程序设计)上机题汇总 一、程序调试题 (一)程序填空 练习一: /*该程序功能:对x=1,2,。。。。。,10,f(x)=x*x*-5*x+sin(x)的最大值。*/ #include void Dec2Bin(int m) { int bin[32],j; for(j=0;m!=0;j++) { bin[j]= __m%2___; m=m/2; } for(;j!=0;j--) printf("%d", ____bin[j-1]_ _____ ); } void main() { int n; scanf("%d",&n); Dec2Bin(n); } 练习四: /*该程序功能:输入数组x[7],调用函数f,去除数组中的负数,输出结果为:1 3 4 6*/ #include 下列程序是建立一个包含学生有关数据的单向链表。但该程序有2个错误,错误出现在每个注释行附近,请调试改正。调试改正中,不得改变程序结构,也不得增删语句。 #define NULL 0 struct stud { long num; char name[10]; float score; struct stud *next; }; /* …………… comment …………… */ int n; struct stud create() 序中有两处错误代码,请改正.注意,不得改变程序结构。 #include <> main() { int a[8],*p=a; int i,j; int tempmin,tempcode; printf("\n input the numbers:\n"); for (i=0;i<8;i++) scanf("%d",p+i);c语言程序改错题
二级(C语言程序设计)上机题汇总
C语言程序改错及填空题