if(k!
=i)
{t=a[k];
a[k]=a[i];
a[i]=/**/t/**/;
}
}
}
voidmain( )
{
inta[]={72,25,58,32,2,15,7,64};
inti,m=sizeof(a)/sizeof(int);
sort(a,m);
for(i=0;iprintf("%d",a[i]);
printf("\n");
getch( );
}
5.补充程序Ccon083.C,实现将结构体数组mystudent中存储的各学生信息按其学号的升序排列。
#include
#include
typedefstruct{
intnum;
charname[20],sex[2];
intage,score;
}STU;
STUmystudent[]={
{1111,"Zhangqiang","m",20,80},
{2104,"Liminghong","w",18,82},
{3121,"Wangxingda","m",21,78},
{4118,"Liushaotao","m",20,90},
{1456,"Wuminghong","w",35,86}
};
voidsort(STU*ps,intsize)
{
inti,flag,pass;
/**/STUtemp;/**/
for(pass=1;pass{
flag=0;
for(i=0;iif(/**/ps[i].num>ps[i+1].num/**/)
{
flag+=1;
temp=ps[i];ps[i]=ps[i+1];ps[i+1]=temp;
}
if(/**/flag==0/**/)break;
}
}
voidmain( )
{
inti,size=sizeof(mystudent)/sizeof(STU);
clrscr( );
printf("Students\'informationbeforesort:
\n\n");
printf("NumberNameageSexscore\n\n");
for(i=0;iprintf("%-7d%s%10d\t%s%8d\n",(mystudent+i)->num,
(mystudent+i)->name,(mystudent+i)->age,
(mystudent+i)->sex,(mystudent+i)->score);
sort(mystudent,size);
printf("\nStudents\'informationaftersort:
\n\n");
printf("NumberNameageSexscore\n\n");
for(i=0;iprintf("%-7d%s%5d\t%s%7d\n",(mystudent+i)->num,
(mystudent+i)->name,(mystudent+i)->age,
(mystudent+i)->sex,(mystudent+i)->score);
getch( );
}
6.补充程序Ccon0310.c,使其实现用递归算法求平方根。
求平方根的迭代公式如下:
#include
main( )
{
doublex,y;
/**/doublemysqrt(double,double);/**/
printf("Pleaseinputx:
\n");
scanf("%lf",&x);
y=mysqrt(x,1.0);
printf("Thesqrtof%f=%f\n",x,y);
getch( );
}
doublemysqrt(doublea,doublex0)
{
doublex1,y;
x1=/**/(x0+a/x0)/2.0;/**/
if(fabs(x1-x0)>0.00001)
y=mysqrt(/**/a,x1/**/);
else
y=x1;
return(y);
}
7.将程序Ccon123.c填写完整,函数fun()返回公式
的值,其中m、n
为整数,且
。
#include
doublefun(unsignedm,unsignedn)
{
unsignedi;
doubley;
/**/y=1.0;/**/
for(i=1;i<=m;i++)
y=y*i;
for(i=1;i<=n;i++)
/**/y=y/i;/**/
for(i=1;i<=m-n;i++)
y=y/i;
return/**/y/**/;
}
voidmain()
{
clrscr();
printf("fun(13,8)=%f\n",fun(13,8));
getch();
}
第四部分:
编程题
1.打开考生文件夹中的Cprog012.C,完成其中的函数fun2(inta[],intn,intb[],intc[]),实现:
(1)将数组a中大于-20的元素,依次存放到数组b中;
(2)将数组b中的元素按照从小到大的顺序存放到数组c中;
(3)函数返回数组b中的元素个数。
#include
#include
#include
#include
intfun2(inta[],intn,intb[],intc[])
{
/**/intj=0;
inti,t,k;
for(i=0;iif(a[i]>-20){c[j]=b[j]=a[i];j++;}
for(i=0;ifor(k=i+1;kif(c[i]>c[k])
{t=c[i];c[i]=c[k];c[k]=t;}
returnj;/**/
}
voidmain( )
{intn=10,i,nb;
intaa[10]={12,-10,-31,-18,-15,50,17,15,-20,20};
intbb[10],cc[10];
clrscr( );
printf("Thereare%2delementsinaa.\n",n);
printf("Theyare:
");
for(i=0;iprintf("\n");
nb=fun2(aa,n,bb,cc);
printf("Elementsinbbare:
");
for(i=0;iprintf("\n");
printf("Elementsinccare:
");
for(i=0;iprintf("\n");
printf("Thereare%2delementsinbb.\n",nb);
}
2.打开考生文件夹中的Cprog022.C,完成其中的函数fun2(chara[],charb[],charc[]),实现:
将三个字符串a、b、c从小到大排序后输出。
注意:
字符串比较函数为strcmp(str1,str2),字符串赋值函数为strcpy(str1,str2)。
#include
#include
#include
#include
voidfun2(chara[],charb[],charc[])
{
/**/charstr[100];
if(strcmp(a,b)>0)
{strcpy(str,a);strcpy(a,b);strcpy(b,str);}
if(strcmp(b,c)>0)
{strcpy(str,b);strcpy(b,c);strcpy(c,str);}
if(strcmp(a,b)>0)
{strcpy(str,a);strcpy(a,b);strcpy(b,str);}/**/
}
voidmain( )
{charstr1[15]="Fuzhou",str2[15]="Fujian",str3[15]="China";
clrscr( );
fun2(str1,str2,str3);
printf("Theorderedstringsis:
%s,%s,%s\n",str1,str2,str3);
getch( );
}
3.打开程序Cprog072.C,完成其中的fun( )函数,该函数将4阶矩阵A的各行中0之前的所有正数依次存放到数组b中,并返回这些正数之和。
如矩阵A为
则调用函数fun( )后,b[0]为1,b[1]为2,b[2]为23,b[3]为32,函数返回58。
#include
#defineROW4
#defineCOL4
intfun(inta[][COL],introw,intb[])
{
/**/intx=0,i,j,k=0;
for(i=0;ifor(j=0;j
if(a[i][j]>0)b[k++]=a[i][j];
elseif(a[i][j]==0)break;
for(i=0;ix=x+b[i];
returnx;/**/
}
voidmain( )
{
intsss=0,b[16]={0};
inta[ROW][COL]={{1,2,-3,-4},{0,-12,-13,14},{-21,23,0,-24},{-31,32,-33,0}};
clrscr( );
sss=fun(a,ROW,b);
printf("Sumofpositiveelementsis%d\n",sss);
getch( );
}
4.打开程序Cprog082.C,完成其中的fun( )函数,该函数将以指针数组的形式存放的n个串升序排序。
(提示:
字符串复制函数是strcpy(char*,char*),字符串比较函数是strcmp(char*,char*))
#include
#include
voidf(charp[][20],intn);
voidmain( )
{
inti;
charp[][20]={"abc","xabdfg","abbd","dcdbe","cd"};
f(p,5);
clrscr( );
for(i=0;i<5;i++)
printf("%s\n",p[i]);
getch( );
}
voidf(charp[][20],intn)
{
/**/chart[20];
inti,j;
for(i=1;ifor(j=0;jif(strcmp(p[j],p[j+1])>0)
{strcpy(t,p[j]);strcpy(p[j],p[j+1]);strcpy(p[j+1],t);}
/**/
}
5.打开程序Cprog092.C,完成其中的strcmp1( )函数,该函数实现判别两字符串str1和str2的大小。
#include
intstrcmp1(const
|