现在的位置: 首页 > 14 二级C语言 > 正文

2011年9月全国二级C上机真题第3~4套

2013年02月25日 14 二级C语言 ⁄ 共 195字 ⁄ 字号 暂无评论

上机真题包括:填空、改错和选择。

【程序填空题_3】给定程序中,函数fun的功能是:利用指针数组形参ss所指字符串数组中的字符串按长到短的顺序排列,并输出排列结果。ss所指字符串数组中共有N个字符串,且串长小于M。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#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=  _________;
        for(j=i+1; j<N; j++)
            /**********found**********/
            if(strlen(ps[k]) < strlen(______)) k=j;
        /**********found**********/
        tp=ps[i];
        ps[i]=ps[k];
        ps[k]= ___________ ;
    }
    printf("\nThe string after sorting by length:\n\n");
    for(i=0; i<N; i++)  puts(ps[i]);
}
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);
}

【程序修改题_3】已知一个数列从第0项开始的前三项分别为0,0,1以后的各项都是其相邻的前三项之和。给定程序MODI1.C中函数fun的功能是:计算并输出该数列前n项的平方根之和。N的值通过形参传入。
例如,当n=10时,程序的输出结果应为:23.197745。
请改正程序中的错误,使它能计算出正确的结果。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#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
}
 
int main ( )
{
    int n;
    printf("Input N=");
    scanf("%d", &n);
    printf("%f\n", fun(n) );
    return 0;
}

【程序设计题_3】编写函数fun,它的功能是计算下列级数和,和值由函数值返回。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#include <math.h>
double fun(double x , int  n)
{
}
 
main()
{
    void NONO();
    printf("%f\n", fun(0.3,10));
    NONO();
}
void NONO ()
{
    /* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
    FILE *fp, *wf ;
    int i, n ;
    double s, x ;
    fp = fopen("K:\\k51\\24000403\\in.dat","r") ;
    wf = fopen("K:\\k51\\24000403\\out.dat","w") ;
    for(i = 0 ; i < 10 ; i++)
    {
        fscanf(fp, "%lf,%d", &x, &n) ;
        s = fun(x, n) ;
        fprintf(wf, "%f\n", s) ;
    }
    fclose(fp) ;
    fclose(wf) ;
}

抱歉!评论已关闭.