/*************found**************/
fun(char *p)
{ char max, *q;int I=0;
max=p[I];
while (p[I]!=0)
{if (maxp)
下列给定程序中,函数fun的功能是:求出两个数的最大公约数,并作为函数值返回
。例如,若给num1和num2输入49和21,则输出的最大公约数为7;若给num1和num2分
别输入27和81,则输出最大公约数为27。
请改正函数fun中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
int fun (int a, int b)
{ int r,t;
if(a
/*************found**************/
void fun(char a)
{ if(*a)
{
fun(a+1);
printf("&c",*a);
}
}
main()
{ char s[10]="abcd";
printf("处理前字符串=%s\n处理后字符串=",s);
fun(s);
printf("\n");
}
【参考答案】
(1)错误:void fun(char a) 正确:void fun(char *a)
下列给定程序中,函数fun的功能是:求整数x的y次方的低3位值。例如,整数5的6次
方为15625,此值的低3位值为625。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
long fun(int x, int y, long *p)
{ int i;
long t=1;
/*************found**************/
for(i=1;i
#include
#include
#include
#define N 80
void fun(char *s, char t[])
{
int I, j=0;
/*************found**************/
for(I=0; I<(int)strlen(s);I++)
if(I%2 && s[I]%2==0)
t[j++]=s[I] ;
/*************found**************/
t[I]='\0';
}
void main()
{
char s[N], t[N];
system("CLS");
printf("\nPlease enter string s :");
gets(s);
fun(s,t);
printf("\nThe result is :%s\n",t);
}
【参考答案】
(1)错误:if (i%2 && s[i]%2 ==0)
正确: if(i%2 || s[i]%2==0) 或 if(i%2 !=0 || s[i]%2==0)
(2)错误:t[i] ='\0'
正确:t[j]= '\0';或t[j]=0;
下列给定程序中,函数fun的功能是:首先把b所指字符串中的字符按逆序存放,然后
将a所指字符串中的字符和b所指字符串中的字符,按排列的顺序交叉合并到c所指
数组中,过长的剩余字符接在c所指数组的尾部。例如,当a所指字符串中的内容为
abcdefg,b所指字符串中的内容为1234时,c所指数组中的内容应该为a4b3c2d1efg;
而当a所指字符串中的内容为1234,b所指字符串中的内容为abcdefg时,c所指数组
中的内容应该为1g2f3e4dcba。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
#include
#include
#include
void fun(char *a, char *b, char *c)
{
int I, j; char ch;
I=0; j=strlen(b)-1;
/*************found**************/
while (I>j)
{ch=b[I]; b[I]=b[j]; b[j]=ch;
I++; j--;
}
while (*a||*b)
{ if (*a){*c=*a; c++; a++;}
if(*b){*c=*b; c++; b++;}
}
/*************found**************/
*c=0 ;
}
void main()
{
char s1[100],s2[100],t[200];
system("CLS");
printf("\nEnter s1 string: ");
scanf("%s",s1);
printf("\nEnter s2 string: ");
scanf("%s",s2);
fun(s1,s2,t);
printf("\nThe result is :%s\n",t);
}
【参考答案】
(1)错误:while(i>j) 正确:while(i
#include
#include
double fun(double eps)
{double s,t; int n=1;
s=0.0;
/*************found**************/
t=1;
/*************found**************/
while(t>eps)
{ s+=t;
t=t*n/(2*n+1);
n++;
}
/*************found**************/
return (s);
}
void main()
{ double x;
printf("\nPlease enter a precision: ");
scanf("%lf",&x);
printf("\nPi=%1f\n ",fun(x));
}
【参考答案】
(1)错误:t=1; 正确:t=1.0;
(2)错误:while(t>eps) 正确:while(t>=eps)
(3)错误:return(s); 正确:return(s*2);
下列给定程序中函数fun的功能是:将长整型数中每一位上为偶数的数依次取出,构
成一个新数放在t中。高位仍在高位,低位仍在低位。例如,当s中的数为87653142
时,t中的数为8642。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
#include
#include
void fun(long s,long *t)
/*************found**************/
{ int d;
long s1=0;
*t=0;
while(s>0)
{ d=s%10;
if(d%2==0)
{*t=d*s1+*t;
s1*=10;
}
/*************found**************/
s\=10;
}
}
void main()
{ long s,t;
system("CLS");
printf("\nPlease enter s: ");
scanf("%ld",&s);
fun(s,&t);
printf("The result is :%ld\n",t);
}
【参考答案】
(1)错误:long s1=0; 正确:long s1=1;
(2)错误:s\=10; 正确:s/=10;
下列给定程序中,函数fun的功能是:交换主函数中两个变量的值。例如,若变量a中
的值为8,b中的值为3,则程序运行后a中的值为3,b中的值为8。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
/*************found**************/
void fun(int x,int y)
{ int t;
/*************found**************/
t=x;x=y;y=t;
}
void main()
{ int a,b;
a=8;
b=3;
fun(&a, &b);
printf("%d %d\n ", a,b);
}
【参考答案】
(1)错误:void fun(int x, int y)
正确:void fun(int *x, int *y)
(2)错误:t=x;x=y;y=t;
正确:t=*x;*x=*y;*y=t;
下列给定程序中,函数fun的功能是:从N个字符串中找出最长的那个串,并将其地址
作为函数值返回。各字符串在主函数中输入,并放入一个字符串数组中。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
#include
#define N 5
#define M 81
/*************found**************/
fun(char (*sq)[N])
{int i; char *sp;
sp=sq[0];
for(i=0;i
#include
/*************found**************/
IsPrime(int n);
{
int i,m;
m=1;
/*************found**************/
for(i=2;i
#include
#include
void fun (char *s, char *t )
{
int I,s1;
s1=strlen(s);
/*************found**************/
for (I=0;I<=s1;I++)
t[I]=s[I];
for (I=0;I
#include
#include
/*************found**************/
fun (int n)
{ int a=2,b=1,c, k;
double s=0.0;
for(k=1;k<=n;k++)
{s=s+1.0*a/b;
/*************found**************/
c=a;a+=b;b+=c;
}
return(s);
}
void main()
{ int n=5;
system("CLS");
printf("\nThe value of function is:%1f\n",fun(n));
}
下列给定程序中,函数fun的功能是:输出M行、N列整数方阵,然后求两条对角线上
的各元素之和,返回此和数。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
#include
#include
#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
#include
#include
#include
#include
void fun(char *p)
{
int k=0;
for ( ;*p;p++)
if (k)
{
/*************found**************/
if (p==' ')
{
k=0;
/*************found**************/
*p=toupper( *(p-1));
}
}
else
k=1;
}
void main()
{char chrstr[64];
int d;
system("CLS");
printf("\nPlease enter an English sentence within 63 letters: ");
gets(chrstr);
d=strlen(chrstr);
chrstr[d]=' ';
chrstr[d+1]=0;
printf("\nBofore changing:\n %s",chrstr);
fun(chrstr);
printf("\nAfter changing:\n %s",chrstr);
}
【参考答案】
(1)错误:if(p==' ')
正确:if(*p==' ')
(2)错误:*p=toupper(*(p-1));
正确:*(p-1)=toupper(*(p-1));
下列给定程序中,函数fun的功能是:从整数1到55之间,选出能被3整除且有一位上
的数是5的数,并把这些数放在b所指的数组中,这些数的个数作为函数值返回。规
定,函数中a1放个位数,a2放十位数。
请改正程序中的错误,使程序能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
/*************found**************/
int fun(int *b);
{
int k,a1,a2,I=0;
/*************found**************/
for(k=10;k<=55;k++)
{
a2=k/10;
a1=k-a2*10;
if((k%3==0 && a2==5)|| (k%3==0 && a1==5))
{
b[I]=k;
I++;
}
}
/*************found**************/
return k;
}
void main()
{
int a[100],k,m;
m=fun(a);
printf("The result is: \n");
for(k=0;k
#include
char fun(char c)
{ /*************found**************/
if(c>='A' && c<='Z')
c=c-32;
/*************found**************/
if(c>='a' && c<='u')
c=c-5;
else if(c>='v' && c<='z')
c=c-21;
return c;
}
main()
{
char c1,c2;
printf("\nEnter a letter(A-Z): ");
c1=getchar();
if(isupper(c1))
{
c2=fun(c1);
printf("\n\nThe letter %c change to %c\n",c1,c2);
}
else
{
printf("\nEnter (A-Z)!\n");
}
}
【参考答案】
(1)错误:c=c-32; 正确:c=c+32;
(2)错误:c=c-5; 正确:c=c+5;
下列给定程序中,函数fun的功能是:将s所指字符串中最后一次出现的t1所指子串
替换成t2所指子串,所形成的新串放在w所指的数据中。在此处,要求t1和t2所指字
符串的长度相同。例如,当s所指字符串中的内容为abcdabfabc,t1所指子串中的内
容为ab,t2所指子串中的内容为99时,结果,在w所指的数组中的内容为abcdabf99c
。请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
#include
#include
/*************found**************/
void fun(char *s,t1,t2,w)
{ char *p,*r,*a;
strcpy(w,s);
/*************found**************/
while(w)
{
p=w;
r=t1;
while(*r)
if(*r==*p)
{
r++;
p++;
}
else
{
break;
}
if(*r=='\0')
a=w;
w++;
}
r=t2;
while(*r)
{
*a=*r;
a++;
r++;
}
}
main()
{
char s[100],t1[100],t2[100],w[100];
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("\nError:strlen(t1)!=strlen(t2)\n");
}
}
【参考答案】
(1)错误:void fun(char *s,t1,t2,w)
正确:void fun(char *s,char *t1,char *t2,char *w)
(2)错误:while (w) 正确:while (*w)
下列给定的程序中,函数fun的功能是:将s所指字符串中出现的t1所指字符串全部
替换成t2所指字符串,所形成的新的字符串放在w所指的数组中。在此处,要求t1和
t2所指字符串的长度相同。例如,当s所指字符串中所指的内容为abcdabfab,t1所
指字符串中的内容为ab,t2所指字符串中的内容为99时,结果在w所指的数组中的内
容应为99cd99f99。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
#include
#include
#include
/*************found**************/
int fun (char *s,char *t1, char *t2, char *w)
{
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;
/*************found**************/
while (*r){*a=*r;a++;r++}
w+=strlen(t2);
}
else w++;
}
}
void main()
{char s[100],t1[100],t2[100],w[100];
system("CLS");
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(t2)\n");
}
【参考答案】
(1)错误:int fun(char *s,char *t1,char *t2, char *w)
正确:void fun(char *s,char *t1,char *t2, char *w)
(2)错误:while(r) 正确:while(*r)
(3)错误:r++ 正确:r++;
下列给定程序中,函数fun的功能是:把主函数中输入的3个数,最大的放在a中,中间
的放在b中,最小的放在c中。例如,输入的数为:55 12 34,输出结果应当
是:a=55.0,b=34.0,c=12.0。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
void fun(float *p,float *q,float *s)
{
/*************found**************/
float *k;
if(*p<*q)
{
k =*p;
*p=*q;
*q=k;
}
/*************found**************/
if(*s<*p)
{
k =*s;
*s=*p;
*p=k;
}
if(*q<*s)
{
k =*q;
*q=*s;
*s=k;
}
}
main()
{
float a,b,c;
printf("Input a b c: ");
scanf("%f%f%f",&a,&b,&c);
printf("a=%4.1f,b=%4.1f,c=%4.1f\n\n",a,b,c);
fun(&a,&b,&c);
printf("a=%4.1f,b=%4.1f,c=%4.1f\n\n",a,b,c);
}
_
【参考答案】
(1)错误:float *k; 正确:float k;
(2)错误:if (*s < *p) 正确:if(*s > *p)
下列给定程序中函数fun的功能是:从低位开始取出长整型变量s中奇数位上的数,
构成一个新数放在t中,例如,当s中的数为7654321时,t中的数为7531。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
#include
/*************found**************/
void fun(long s,long t)
{ long s1=10;
*t=s%10;
while(s>0)
{ s=s/100;
*t=s%10*s1+*t;
/*************found**************/
s1=s1*100;
}
}
void main()
{ long s, t;
system("CLS");
printf("\nPlease enter s: ");
scanf("%ld",&s);
fun(s,&t);
printf("The result is: %ld\n ",t);
}
【参考答案】
(1)错误:void fun(long s,long t)
正确:void fun(long s,long *t)
(2)错误:s1=s1*100; 正确:s1=s1*10;
下列给定程序中,函数fun的功能是:利用插入排序法对字符串中的字符按从小到大
的顺序进行排序。插入法的基本MATCH_
word
word文档格式规范word作业纸小票打印word模板word简历模板免费word简历
_1713868547612_0是:先对字符串中的头两个元素进行排序,然
后把第3个字符插入到前两个字符中,插入后前3个字符依然有序;再把第4个字符插
入到前3个字符中,待排序的字符串已在主函数中赋予。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
#include
#define N 80
void insert(char *aa)
{ int i, j, n; char ch;
n=strlen(aa);
for(i=1;i=0)&&(ch
#include
#include
#include
#include
#define N 10
typedef struct ss
{ char num[10];
int s;
} STU;
STU *fun(STU a[], int m)
{ STU b[N],*t;
int i, j,k;
/*************found**************/
*t=calloc(m,sizeof(STU));
for(i=0;ib[j].s) j=i;
/*************found**************/
t[k].num=b[j].num;
t[k].s=b[j].s;
b[j].s=0;
}
return t;
}
outresult(STU a[],FILE *pf)
{ int i;
for(i=0;i10)
{ printf("\nGive the number of the students who have better
score: ");
scanf("%d",&m);
}
pOrder=fun(a,m);
printf("***** THE RESULT*****\n");
printf("The top :\n");
for(i=0;i
#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=s;
/*************found**************/
p->next=NULL;
for(i=1;idata=rand()%m;
s->next=p->next;p->next=s; p=p->next;
}
s->next=NULL;
/*************found**************/
return p;
}
outlink(NODE *h)
{ NODE *p;
p=h->next;
printf("\n The LIST :\n\n HEAD");
while(p)
{ printf("->%d",p->data);
p=p->next;}
printf("\n");
}
main()
{ NODE *head;
system("CLS");
head=Creatlink(8,22);
outlink(head);
}
【参考答案】
(1)错误:p->next=NULL; 正确:p=s;
(2)错误:s->date=rand( )%m ;
正确:s->data=rand( )%(m - 1);
(3)错误:return p; 正确:return h;
下列给定程序中,函数fun的功能是:根据整型参数m,计算如下公式的值。
例如,若m=2000,则应输出0.000160。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
#include
#include
/*************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);
}
void main()
{ int n=2000;
system("CLS");
printf("\nThe result is %1f\n",fun(n));
}
【参考答案】
(1)错误:fun(int m)
正确:double fun(int m)
(2)错误:for(i=100,i<=m,i+=100)
正确:for(i=100;i<=m;i+=100)
下列给定程序中,函数fun的功能是:计算并输出下列数的前N项之和SN,直到SN+1大
于q为止,q的值通过形参传入。
例如,若q的值为50.0,则函数值为49.394948。
请改正程序中的错误,使程序能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
#include
double fun(double q)
{ int n; double s,t;
n=2;
s=2.0;
while(s<=q)
{
t=s;
/*************found**************/
s=s+(n+1)/n;
n++;
}
printf("n=%d\n ",n);
/*************found**************/
return s;
}
void main()
{
printf("%f\n ",fun(50));
}
【参考答案】
(1)错误:s=s+(n+1)/n ;
正确:s=s+(double)(n+1)/n;
(2)错误:return s; 正确:return t;
下列给定程序中函数fun的功能是:统计子字符串substr在字符串str中出现的次数
。例如,若字符串为aaas lkaaas,子字符串为as,则应输出2。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
/*************found**************/
int fun(char *str,*substr)
{
int i, j, k,num=0;
/*************found**************/
for(i=0,str[i],i++)
for(j=i,k=0;substr[k]==str[j];k++,j++)
if(substr[k+1]=='\0')
{
num++;
break;
}
return num;
}
void main()
{ char str[80],substr[80];
printf("Input a string: ");
gets(str);
printf("Input a substring: ");
gets(substr);
printf("%d\n ",fun(str,substr));
}
【参考答案】
(1)错误:int fun(char *str, *substr)
正确:int fun(char *str, char *substr)
(2)错误:for (i=0, str[i], i++)
正确:for (i=0;str[i];i++)
下列给定程序中,函数fun实现的功能是:统计一个无符号整数中各位数字值为零的
个数,通过形参传回主函数;并把该整数中各位上最大的数字值作为函数值返回。
例如,若输入无符号整数30800,则数字值为零的个数为3,各位上数字值最大的是8
。
请改正函数fun中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
int fun(unsigned n,int *zero)
{
int count=0,max=0,t;
do
{
t=n%10;
/*************found**************/
if(t=0)
count++;
if(max
#include
#include
/*************found**************/
fun (int n)
{ int a,b,c, k;
double s;
s=0.0;a=2;b=1;
for(k=1;k<=n;k++)
{
/*************found**************/
s=s+(Double)a/b;
c=a;a=a+b;b=c;
}
return s;
}
main()
{ int n=5;
system("CLS");
printf("\nThe value of function is:%lf\n",fun(n));
}
【参考答案】
(1)错误:fun(int n) 正确:double fun(int n)
(2)错误:s=s+(Double)a/b; 正确:s=s+(double)a/b;
下列给定的程序中,函数fun的功能是:为一个偶数寻找两个素数,这两个素数之和
等于该偶数,并将这两个素数通过形参指针传回主函数。
请改正函数fun中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
#include
void fun(int a, int *b, int *c)
{ int I,j,d,y;
for (I=3;I<=a/2;I=I+2)
/*************found**************/
{y=0;
for (j=2;j<=sqrt((double)I );j++)
if (I%j==0) y=0;
if (y==1)
/*************found**************/
{ d=I-a;
for (j=2;j<=sqrt((double)d );j++)
if (d%j==0) y=0;
if (y==1)
{*b=I; *c=d;}
}
}
}
void main()
{ int a,b,c;
do
{ printf("\nInput a: ");
scanf("%d",&a);}
while(a%2);
fun(a,&b,&c);
printf("\n\n%d=%d+%d\n",a,b,c);
}
【参考答案】
(1)错误:y=0; 正确:y=1;
(2)错误:d=i-a; 正确:d=a-i;
下列给定程序中,函数fun的功能是:将s所指字符串的正序和反序进行连接,形成一
个新串放在t所指的数组中。例如,当s所指字符串为ABCD时, t所指字符串中的内
容应为ABCDDCBA。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
#include
#include
#include
/*************found**************/
void fun (char s, char t )
{
int I,s1;
s1=strlen(s);
for (I=0;I
#include
#include
#include
int fun (char *s, char *t)
{int n; char *p, *r;
n=0;
while(*s)
{p=s;
r=t;
while (*r)
/*************found**************/
if(*r==*p) {r++; p++}
else break;
/*************found**************/
if(r=='\0')
n++;
s++;
}
return n;
}
void main( )
{char s[100], t[100]; int m;
system("CLS");
printf("\nPlease enter string s: ");
scanf("%s",s);
printf("\nPlease enter substring t: ");
scanf ("%s",t);
m=fun(s,t);
printf("\nThe result is: m=%d\n", m);
}
【参考答案】
(1)错误:if(*r==*p) {r++;p++}
正确:if(*r==*p) {r++;p++;}
(2)错误:if(r=='\0')
正确:if(*r=='\0')
下列给定程序中,函数fun和funx的功能是:用二分法求方程2x3-4x2+3x-6=0的一个
根,并且绝对误差不超过0.001。例如,若给m输入-100,给n输入90,则函数求得的一
个根值为2.000。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
#include
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);
}
_
【参考答案】
(1)错误:int r; 正确:double r;
(2)错误:while (fabs(n-m) <0.001)
正确:while (fabs(n-m) >0.001)
下列给定程序中,函数fun的功能是:从3个红球、5个白球、6个黑球中任意取出8个
作为一组,进行输出。在每组中,可以没有黑球,但必须要有红球和白球。组合数作
为函数值返回。正确的组合数应该是15。程序中i的值代表红球数,j的值代表白球
数,k的值代表黑球数。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
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:%4dblack:%4d\n ",i,j,k);
}
}
}
return sum;
}
void main()
{ int sum;
sum=fun();
printf("sum=%4d\n\n ",sum);
}
【参考答案】
(1)错误:for(i=0;i<=3;i++)
正确:for(i=1;i<=3;i++)=1&&k<=6)
正确:if((k>=1&&k<=6)&&(i!=0)&&(j!=0)||(k==0))=3;i++),而是for
(i=1;i<=3;i++),另外,在if(k>=1&&k<=6)中,程序旨在说明"在每组中,可以没有黑
球,但必须要有红球和白球"信息,一一满足后,正确的条件应该是:
if((k>=1&&k<=6)&&(i!=0)&&(j!=0)||(k==0))。
下列给定程序中,函数fun的功能是:从字符串s中删除所有小写字母c。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
void fun(char *s)
{int i, j;
for(i=j=0; s[i]!= '\0';i++)
if(s[i]!= 'c')
/*************found**************/
s[j]=s[i];
/*************found**************/
s[i]='\0';
}
void main()
{ char s[80];
printf("\nEnter a string: ");
gets(s);
printf("The original string: ");
puts(s);
fun(s);
printf("The string after deleted: ");
puts(s);
printf("\n\n ");
}
【参考答案】
(1)错误:s[j]=s[i]; 正确:s[j++]=s[i];
(2)错误:s[i]='\0'; 正确:s[j]= '\0'
下列给定程序中,函数fun的功能是:读入一个字符串(长度<20),将该字符串中的所
有字符按ASCII码升序排序后输出。例如,若输入edcba,则应输出abcde。
请改正程序中的错误,使它能统计出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
#include
void fun(char t[])
{
char c;
int I,j;
/*************found**************/
for(I=strlen(t);I;I--)
for(j=0;j0;i--)本题是利用选
择法对数组元素进行比较排序。所谓选择法,是依次用当前取得的元素和它后面的
元素进行比较,在第一个元素和它后面的元素顺次比较时,可以借助中间变量来对
两个数进行交换,保证第一个元素始终存放数组中的最大数,以后依次挑选出次大
数,这样最终的数组就是有序的。Strlen函数在求字符串长度时,会加上结束符,所
以要减1。
(2)这里是一个
分析
定性数据统计分析pdf销售业绩分析模板建筑结构震害分析销售进度分析表京东商城竞争战略分析
逻辑错误,题中要求按升序排序,所以应改为if (t[j] > t
[j+1])。
已知一个数列从0项开始的前3项:0,0,1,以后的各项都是其相邻的前3项之和。下
列给定的程序中,函数fun的功能是:计算并输出该数列前n项的平方根之和sum。n
的值通过形参传入。例如,当n=10时,程序的输出结果应为23.197745。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
#include
#include
#include
/*************found**************/
fun(int n)
{double sum, s0, s1, s2, s; int k;
sum=1.0;
if (n<=2) sum=0.0;
s0=0.0; s1=0.0; s2=1.0;
for (k=4;k<=n;k++)
{ s=s0+s1+s2;
sum+=sqrt(s);
s0=s1;s1=s2;s2=s;
}
/*************found**************/
return sum
}
void main()
{int n;
system("CLS");
printf("Input N=");
scanf("%d",&n);
printf("%f\n",fun(n));
}
【参考答案】
(1)错误:fun(int n) 正确:double fun(int n)
(2)错误:return sum 正确:return sum;
下列给定程序中,函数fun的功能是:计算n的5次方的值(规定n的值大于2且小于8),
通过形参指针传回主函数;并计算该值的个位、十位、百位上数字之和作为函数值
返回。例如,7的5次方是1 6807,其低3位数的和值是15。
请改正函数fun中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
#include
int fun(int n,int *value)
{
int d,s,i;
/*************found**************/
d=0;
/*************found**************/
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;
}
void main()
{
int n,sum,v;
do
{
printf("\n Enter n(2=8);
sum=fun(n,&v);
printf("\n\nThe result: \n value=%d sum=%d\n\n" ,v,sum);
}
【参考答案】
(1)错误:d=0; 正确:d=1;
(2)错误:s=1; 正确:s=0 ;
(3)错误:s=s/10; 正确:d=d/10 ;
下列给定程序中,函数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]="fshADfg",b[10]="sdAEdi",c[80]={'\0'};
fun(a,b,c);
printf("The string a:"); puts(a);
printf("The string b:"); puts(b);
printf("The result :"); puts(c);
}
【参考答案】
(1)错误:int k=1; 正确:int k=0 ;
(2)错误:while(*p != *q) 正确:while(*p || *q)
下列给定程序中,函数fun的功能是:给一维数组a输入任意4个整数,并按如下的规
律输出。例如输入1,2,3,4,程序运行后输出以下方阵:
4 1 2 3
3 4 1 2
2 3 4 1
1 2 3 4
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
#define M 4
/*************found**************/
void fun(int a)
{ int I,j,k,m;
printf("Enter 4 number: ");
for(I=0;I0;I--)
{ k=a[M-1];
for(j=M-1;j>0;j--)
/*************found**************/
a[j]=a[j+1];
a[0]=k;
for(m=0;m
#include
#include
float fun(float num)
{ int s;
float n,t, pi;
t=1;pi=0;n=1;s=1;
/*************found**************/
while(t>=num)
{
pi=pi+t;
n=n+2;
s=-s;
/*************found**************/
t=s%n;
}
pi=pi*4;
return pi;
}
void main()
{ float n1, n2;
system("CLS");
printf("Enter a float number: ");
scanf("%f",&n1);
n2=fun(n1);
printf("%6.4f\n ",n2);
}
【参考答案】
(1)错误:while(t>=num)
正确:while(fabs(t)>=num)
(2)错误:t=s%n; 正确:t=s/n;
假如整数数列中的数不重复,并存放在数组中。下列给定的程序中,函数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
{for (i=p;i
#include
#include
#define M 10
int a[M][M]={0};
/*************found**************/
fun( int **a, int m)
{int j,k;
for(j=0;j
#include
#include
fun(int n,int *a)
{ int I,j,p,t;
for (j=0;ja[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");
}
void 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);
}
【参考答案】
(1)错误:for(i=j+1;i
void fun(char *p,char *b)
{ int i, k=0;
while(*p)
/*************found**************/
{ i=1;
/*************found**************/
while(i<3||*p)
{
b[k]=*p;
k++;p++;i++;
}
if(*p)
/*************found**************/
{ b[k]= ' ';}
}
b[k]= '\0';
}
void main()
{ char a[80],b[80];
printf("Enter a string: ");
gets(a);
printf("The original string: ");
puts(a);
fun(a,b);
printf("\nThe string after insert space: ");
puts(b); printf("\n\n ");
}
【参考答案】
(1)错误:i=1; 正确:i=0;
(2)错误:while(i<3||*p) 正确:while(i<3&&*p)
(3)错误:b[k]=' '; 正确:b[k++]=' ';
下列给定程序中,函数fun的功能是:将 个字符串连接起来,组成一个新串,放入pt
所指字符串中,例如,把3个串abc、CD、EF串联起来,结果是abcCDEF。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
#include
#include
#include
/*************found**************/
int fun(char str[][10],int m,char *pt)
{ int k,q,I,j=0;
for(k=0;k
#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′;
}
}
void main()
{char s[81],c;
system("CLS");
printf("\nPlease enter a string : ");
gets(s);
printf("\n Please enter the character tosearch : ");
c=getchar();
fun(s,c);
printf("\nThe result is %s\n",s);
}
【参考答案】
(1)错误:void fun (char str,char ch)
正确:void fun (char *str,char ch)
(2)错误:if(*str==ch)
正确:if(*str=='\0')
(3)错误:str[1]='0';
正确:str[1]= '\0';
下列给定程序中,函数fun的功能是:依次取出字符串中所有的数字字符,形成新的
字符串,并取代原字符串。
请改正函数fun中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
#include
#include
void fun(char *s)
{int I,j;
for(I=0,j=0; s[I]!= '\0'; I++)
if(s[I]>= '0'&&s[I]<= '9')
/*************found**************/
s[j]=s[I];
/*************found**************/
s[j]="\0";
}
void main()
{char item[80];
system("CLS");
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);
}
【参考答案】
(1)错误:s[j]=s[i]; 正确:s[j++]=s[i];
(2)错误:s[j]="\0"; 正确:s[j]='\0';
下列给定程序中函数fun的功能是:从低位开始取出长整型变量s中偶数位上的数,
依次构成一个新数放在t中。例如,当s中的数为7654321时,t中的数为642。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
#include