一、程序填空题
在此程序中,函数fun的功能是:利用指针数组对形参ss所指字符串数组中的字符串按由长到短的顺序排序,并输出排序结果。ss所指字符串数组中共有N个字符串,且串长小于M。
#include <stdio.h>
#include <string.h>
#define N 5
#define M 8
void fun(char (*ss)[M])
{ char *ps[N],*tp; int i,j,k;
for(i=0; i<N; i++) ps[i]=ss[i];
for(i=0; i<N-1; i++) {
/**********found**********/
k= __1__ ;
for(j=i+1; j<N; j++)
/**********found**********/
if(strlen(ps[k]) < strlen(__2__) ) k=j;
/**********found**********/
tp=ps[i]; ps[i]=ps[k]; ps[k]= __3__ ;
}
printf("\nThe string after sorting by length:\n\n");
for(i=0; i<N; i++) puts(ps[i]);
}
void main()
{ char ch[N][M]={"red","green","blue","yellow","black"};
int i;
printf("\nThe original string\n\n");
for(i=0;i<N;i++)puts(ch[i]); printf("\n");
fun(ch);
}
答案:(1) i (2) ps[j] (3) tp
二、程序修改题
在此程序中,已知一个数列从0项开始前3项:0、0、1,以后的各项都是其相邻的前3项之和。下列给定的程序中,函数fun的功能是:计算并输出结果应为23.197745。
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>
/*************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) double fun(int n) (2) return sum ;
三、程序设计题
在此程序中,编写函数fun,它的功能是计算下列级数和,和值由函数值返回。
例如,当n=10,x=0.3时,函数值为1.349859。
#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
double fun(double x, int n)
{
}
void main()
{
FILE *wf;
system("CLS");
printf("%f ",fun(0.3,10));
/******************************/
wf=fopen("out.dat","w");
fprintf(wf,"%f",fun(0.3,10));
fclose(wf);
/*****************************/
}
答案:
int i;
double s=1.0,s1=1.0;
for(i=1;i<=n;i++)
{s1=s1*i; /*各项中的阶乘*/
s=s+pow(x,i)/s1; /*按公式求出*/
}
return s;