请补充main函数,该函数的功能是:如果数组aa的前一个元素比后一个元素小,则把它保存在数组比中并输出。
例如,输入“40,51,62,33,35,52,48,95,66, 73”,则结果输出“40,51,33,35,48,66”。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在 main函数的横线上填入所编写的若干表达式或语句。
试题程序:
#include <stdio. h>
#define N 10
main ( )

int i, n=0;
int aa [N]=40, 51, 62,33, 35, 52, 48, 95,
66,73;
int bb[N];
clrscr ( );
for (i=0; i< 【1】 ; i++)
if (aa [i] <aa [i+l] )
【2】 ;
printf("/n*** display bb ***In");
for (i=0; i<n; i++)
printf("bb[%d]=%2d ", 【3】 );

[1]N-1 (2)bb[n++]=aa[i] (3)i,bb[i]

下列给定程序中,函数fun( )的功能是:求出以下分数序列的前n项之和。
2/1,3/2,5/3,8/5,13/8,21/13,……
和值通过函数值返回main( )函数。例如,若输入n=5,则应输出8.391667。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include <conio.h>
#include <stdio.h>
/*************found**************/
fun (int n)
int a=2,b=l,c, k;
double s=0.0;
for(k=l;k<=n;k++)
s=s+l.0*a/b;
/*************found**************/
c=a; a+=b; b+=c;

return (s);

main ( )
int n=5;
clrscr ( );
printf("/nThe value of function is:
%1f/n", fun (n));

错误:fun(int n) 正确:double fun(int n)
(2)错误:c=a;a+=b;;b+=c; 正确:c=a;a+=b;b=c;

学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s中,请编写函数fun( ),它的功能是:把指定分数范围内的学生数据放在b所指的数组中,分数范围内的学生人数由函数值返回。
例如,输入的分数是60和69,则应当把分数在60到69的学生数据进行输出,包含60分和69分的学生数据。主函数中把60放在low中,把69放在heigh中。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
#include <stdio.h>
#define N 16
typedef struct
char num[10];
int s ;
STREC;
int fun (STREC *a, STREC *b, int 1, int h )

main ( )

STREC s [N] = "GA005", 85 , "GA003", 76 ,
"GA002", 69, "GA004", 85, "GA001", 96 ,
"GA007", 72, "GA008", 64, "GA006", 87 ,
"GA015", 85, "GA013", 94, "GA012", 64 ,
"GA014", 91, "GA011", 90, "GA017", 64 ,
"GA018", 64 , "GA016", 72 ;
STREC h [N], tt;
FILE *out;
int i, j, n, low, heigh, t;
printf("Enter 2 integer number low &
heigh: ");
scanf ("%d%d ", &low, &heigh);
if (heigh<low)
t=heigh;heigh=low; low=t;
n=fun (s,h, low, heigh);
printf ("The student’s data between
%d--%d: /n ", low, heigh);
for (i=0; i<n; i++)
printf("%s %4d/n ",h[i] .num, h[i].s);
/*输出指定分数范围内的学生记录*/
printf("/n ");
out=fopen ("out74.dat ", "w")
fprintf(out, "%d/n ",n)-
n=fun (s,h, 80, 98);
for (i=0; i<n-1; i++)
/* 分数在80~98之间的学生记录按他数从低到高排序*/
for (j=i+l; j<n; j++)
if (h[i] .s>h [j] .s)
tt=h [i] ;h [ii =h [j] ;h [j] =tt;
for (i=0; i<n; i++)
fprintf(out, "%4d/n ",h[i] .s);
fprintf(out, "/n ");
fclose (out);

int fun (STREC *a, STREC *b, int A, int h )
{
int i, j=0;
for (i=0; i<N; i++)
if (a [i]. s>-l&&a [i]. s<=h) /*将分数高于A,低于h的学生记录存在于结构体数组b中* /
b[j++]=a[i];
return j; /*返回分数范围内的学生人数*/
}

请补充函数fun( ),该函数的功能是:把字符串str中的字符按字符的ASCⅡ码降序排列,处理后的字符串仍然保存在原串中,字符串及其长度作为函数参数传入。
例如,如果输入“cdefgh”,则输出为“hgfedc”。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun( )的横线上填入所编写的若干表达式或语句。
试题程序:
#include <stdio.h>
#define N 80
void fun (char s [], int n)

int i, j;
char ch;
for (i=0; i<n; i++)
for(j= 【1】 ;j<n;j++)
if (s[i]<s [j])

ch=s [j];
【2】 ;
s [i] =ch;

main ( )

int i=0, strlen=0;
char str [N];
clrscr ( );
printf ("/nInput a string: /n");
gets (str);
while (str [i] !=’ /0’)

strlen++;
i++;

fun (str, strlen);
printf ("/n***display string ***/n");
puts (str);

[1]i [2] s[j]=s[i]

下列给定程序中,函数fun( )的功能是:应用递归算法求某数a的平方根。求平方根的迭代公式如下: 例如,2的平方根为1.414214。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。 试题程序: #include <math. h> #include <stdio. h> /*************found**************/ fun(double a,double x0) double xl,y; xl= (x0+a/x0)/2.0; /*************found**************/ if (fabs (xl-x0) >0.00001) y=fun (a, xl); else y=x1; return y; main ( ) double x; printf("Enter x: "); scanf("%1f",&x); printf ("The square root of %1f is %1f/n", x, fun(x,l.O));

错误:fun(double a,double x0) 正确:double fun(double a,double x0)
(B)错误:if(fabs(xA-x0)>0.0000A) 正确:if(fabs(xA-x0)>=0.0000A)

请补充main函数,该函数的功能是:求1+21+3!+…+N!的和。
例如, 1+2!+3!+...+5!+6!的和为873。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在 main函数的横线上填入所编写的若干表达式或语句。
试题程序:
#include <stdio.h>
main ( )

int i, n;
long s=0, t=l;
clrscr ( );
printf ("/nInput n: /n");
scanf ("%d", 【1】 );
for (i=l; i<=n; i++)

t= 【2】 ;
s= 【3】 ;

printf ("1 ! +2 ! +3 !... +%d! =%ld/n", n, s);

&n (2)t*I , (3)s+t

下列给定程序中,函数fun( )的功能是:根据输入的3个边长(整型值),判断能否构成三角形:若能构成等边三角形,则返回3,若是等腰三角形,则返回2,若能构成三角形则返回1,若不能,则返回0。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include <math. h>
int fun(int a, int b, int c)
if (a+b>c&&b+c>a&&a+c>b)
if (a==b&&b==c)
/*************found**************/
return 1;
else if(a==b|| b==c||a==c)
return 2;
/*************found**************/
else return 3;

else return 0;

main ( )
int a,b, c, shape;
printf("/nInput a,b,c: ");
scanf ("%d%d%d", &a, &b, &c);
printf ("/na=%d, b=%d, c=%d/n",a,b,c);
shape=fun (a,b, c);
printf ("/n/nThe shape : %d/n", shape);

错误:return 1; 正确:return 3;
(2)错误:return 3; 正确:return 1;

请编写函数fun( ),该函数的功能是:删去一维数组中所有相同的数,使之只剩一个。数组中的数已按由小到大的顺序排列,函数返回删除后数组中数据的个数。
例如,若一维数组中的数据是:
2 2 2 3 4 4 5 6 6 6 6 7 7 8 9 9 10 10 10
删除后,数组中的内容应该是:
2 3 4 5 6 7 8 9 10。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
#include <stdio. h>
#define N 80
int fun(int a[], int n)

main ( )

int a[N]= 2,2,2,3,4,4,5,6,6,6,6,7,7,
8,9,9,10,10,10,10, i, n=20;
printf ("The original data : /n");
for(i=0; i<n; i++)
printf ("%3d", a [i] );
n=fun (a, n);
printf("/n/nThe data after deleted
: /n");
for(i=0; i<n; i++)
printf ("%3d", a [i] );
printf ("/n/n");

int fun(int a[], int n)
{
int i, j=l;
for (i=A; i<n; i++)
if (a[j-A] !=a[i]) /*若该数与前一个数不相同,则要保留*/
a[j++]=a [i];
return j; /*返回不同数的个数*/
}

请补充main函数,该函数的功能是:输入两个正整数m和n,求这两个数的最大公约和最小公倍数。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在 main函数的横线上填入所编写的若干表达式或语句。
试题程序:
#include <stdio.h>
main ( )

int a, b, n, m, t;
clrscr ( );
printf ("/nInput two numbers: /n");
scanf ("%d, %d", &n, &m);
if (n<m)

a=m;
b=n;

else

a=n;
b=m;

while( 【1】 )

t= 【2】
a=b;
b=t;

printf ("greatest con. non divisor:
%d/n", a);
printf ("least common multiple:
%d/n", 【3】 );

[1]b!=0 [2]a%b; (3)n*m/a

下列给定程序中函数fun( )的功能是:计算n!。例如,给 n输入5,则输出120.000000。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include <stdio.h>
#include <conio.h>
double fun (int n)
double result=l.0;
/*************found**************/
if n==0
return 1.0;
while (n>l&&n<170)
/*************found**************/
result *=n--
return result;

main ( )
int n;
printf ("Input N: ");
scanf ( "%d" &n);
printf ("/n/n%d!=%lf/n/n",n, fun(n));

错误:if n==0 正确:if(n==0)
(2)错误:result*=n-- 正确:result*=n--;

微信扫码获取答案解析
下载APP查看答案解析