第一部分 改错题
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;
for ( i=2; i<n; i++)
/**********found***********/
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("nnThe prime factor(s) of %d is (are):",k);
for (j=2;j<=k;j++)
if ((!(k%j))&&(IsPrime(j)) printf("n %4d",j);
printf("n");
}
2、下列给定程序中,函数 fun 的功能是:逐个比较 a、b 两个字符串对应位置中的字符,把
ASCII 值大或等于的字符一次存放到 c 数组中,形成一个新的字符串。例如,若 a 中的字符
串为 aBCDeFgH,b 中的字符串为:ABcd,则 c 中的字符串为:aBcdeFgh。
请改正程序中的错误,使程序能得出正确的结果。
注意,不要改多 main 函数,不得增行或删行,也不得更改程序的结构!
试题程序:
#include <studio.h>
#include <string.h>
/**********found***********/
void fun(char *p,char *q,char *c)
/**********found***********/
{int k=1;
/**********found***********/
while(*p!=*q)
/**********found***********/
{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 的功能是:依次取出字符串中所有数字字符,形成新的字符
串,并取代原字符串。
请改正程序中的错误,使程序能得出正确的结果。
注意,不要改多 main 函数,不得增行或删行,也不得更改程序的结构!
试题程序
#include <stdio.h>
#include <conio.h>
void fun (char *s)
{ int i,j;
for (i=0,j=0;s[i]!='\0';i++)
/**********found***********/
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=6,lower=8。
请改正程序中的错误,使程序能得出正确的结果。
注意,不要改多 main 函数,不得增行或删行,也不得更改程序的结构!
试题程序
#include <conio.h>
#include <stdio.h>
/**********found***********/
void fun (char *s,int a, int b)
{ while(*s)
{ if (*s>='A' && *s<='Z')
/**********found***********/
a++;
if (*s>='a' && *s<='z')
/**********found***********/
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=%dn",upper,lower);
}
5、假定整数数列中的数不重复,并存放在数组中。下列给定程序中,函数 fun 的功能是:
删除数列中值为 x 的元素。n 中存放的是数列中元素的个数。
请改正程序中的错误,使程序能得出正确的结果。
注意,不要改多 main 函数,不得增行或删行,也不得更改程序的结构!
试题程序
#include <stdio.h>
#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
{for (i=p;i<n;i++)
/**********found***********/
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<n;i++) printf("%5d",w[i]);
printf("nInput x (to delete):");scanf("%d",&x);
printf("Delete:%dn",x);
n=fun(w,n,x);
if (n==-1) printf("***Nor be found!***nn");
else
{printf("The data after deleted:n");
for (i=0,i<n;i++) printf("%5d",w[i]);printf("nn");
}
}
6、下列给定程序中,函数 fun 的功能是:根据整型形参 m 的值,计算如下公式的值。t=1-1/2
×2-1/3×3-…-1/m×m
例如,若 m 中的值为 5,则应输出:0.536389。
请改正程序中的错误,使程序能得出正确的结果。
注意,不要改多 main 函数,不得增行或删行,也不得更改程序的结构!
试题程序:
#include <conio.h>
#include <stdio.h>
double fun (int m)
{ double y=1.0;
int i;
/**********found***********/
for (i=2;i<m;i++)
/**********found***********/
y-=1/(i*i);
return(y);
}
main ()
{int n=5;
clrscr();
printf("\nRhe result is %1f\n",fun(n));
}
7、下列给定程序中,函数 fun 的功能是:用选择法对数组中的 n 个元素按从小到大的顺序
进行排序。
请改正程序中的错误,使程序能得出正确的结果。
注意,不要改多 main 函数,不得增行或删行,也不得更改程序的结构!
试题程序:
#include <stdio.h>
#define N 20
void fun(int a[],int n)
{ int i,j,t,p;
for (j=0;j<n-1;j++)
/**********found***********/
{p=j
for (i=j;i,n;i++)
if (a[i]<a[p])
/**********found***********/
p=j;
t=a[p];a[p]=a[j];a[j]=t;
}
}
main()
{
int a[N]={9,6,8,3,-1},i,m=5;
printf("排序前的数据:");
for (i=0;i<m;i++) printf("%d",a[i]);printf("\n");
fun(a,m);
printf("排序后的数据:");
for (i=0;i<m;i++) printf("%d",a[i]);printf("\n");
}
8、下列给定程序中,函数 fun 的功能是:在字符串 str 中找出 ASCII 码值最大的字符,将
其放在第一个位置上;并将该字符前的原字符向后顺序移动。例如,调用 fun 函数之前给字
符串输入:ABCDeFGH,调用后字符串中的内容为 eABCDFGH。
请改正程序中的错误,使程序能得出正确的结果。
注意,不要改多 main 函数,不得增行或删行,也不得更改程序的结构!
试题程序:
#include <stdio.h>
fun(char *p)
{ char max,*q;int i=0;
max=p[i];
while(p[i]!=0)
{ if(max<p[i])
{max=p[i];
/**********found***********/
p=q+i;
}
i++;
}
/**********found***********/
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 agter moving:");puts (str);ptintf("\n\n");
}
9、下列给定程序中,函数 fun 的功能是:从 n 个学生的成绩中统计出低于平均分的学生人
数,此人数由函数值返回,平均分存放在形参 aver 所指的存储单元中。例如,若输入 8 名
学生的成绩:
80.5 60 72 90.5 98 51.5 88 64
则低于平均分的学生人数为 4(平均分为:75.5625)。
请改正程序中的错误,使程序能得出正确的结果。
注意,不要改多 main 函数,不得增行或删行,也不得更改程序的结构!
试题程序:
#include <conio.h>
#include <stdio.h>
#define N 20
int fun (float *s,int n,float *aver)
{float ave,t=0.0;
int count=0,k,i;
for (k=0;k<n;k++)
/**********found***********/
t=s[k];
ave=t/n;
for (i=0;i<n;i++)
if (s[i]<ave) 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<m;i++) scanf("%f",s+i);
printf("nThe number of students:%dn",fun(s,m,&aver));
printf("Ave=%fn",aver);
)
10、下列给定程序中,函数 fun 的功能是:将 s 所指字符串中出现的 t1 所指子串全部替换
成 t2 所指子字符串,所形成的新串放在 w 所指的数组中。在此处,要求 t1 和 t2 所指字符
串的长度相同。例如,当 s 所指字符产中的内容为 abcdabfab,t1 所指子串中的内容为 ab,
t2 所指子串中的内容为 99 时,结果,在 2 所指的数组中内容应为 99cd99f99。
请改正程序中的错误,使程序能得出正确的结果。
注意,不要改多 main 函数,不得增行或删行,也不得更改程序的结构!
试题程序:
#include<conio.h>
#include<stdio.h>
#include<string.h>
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=='')
{a=w;r=t2;
/**********found***********/
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 string t1:");scanf("%s",t1);
printf("nPlease enter string t2:");scanf("%s",t2);
if (strlen(t1)==strlen(t2))
{
printf("nThe result is :%sn",w);
}
else printf("Error:strlen(t1)!=strlen(t2)n");
}