2013計算機二級C語言上機練習題及答案(5)

字號:

為大家收集整理了《2013計算機二級C語言上機練習題及答案(5)》供大家參考,希望對大家有所幫助?。。?BR>    填空題
    請補充main函數(shù),該函數(shù)的功能是:輸出一個N*N矩陣,要求非周邊元素賦值0,周邊元素賦值1。
    僅在橫線上填入所編寫的若干表達式或語句,勿改動函數(shù)中的其他內(nèi)容。
    #include
    #define N 10
    main()
    {
    int bb[N][N];
    int i, j, n;
    printf(" \nInput n:\n");
    scanf("%d", &n);
    for (i=0; i
    for (j=0; j
    {
    if (i==0||i==n-1||j==0||j==n-1)
    ___1___;
    else
    ___2___;
    }
    printf("\n ***** the result ******* \n");
    for (i=0; i
    {
    printf("\n\n");
    for (j=0; j
    printf("%4d", bb[i][j]);
    }
    }
    參考答案:
    第1處填空:bb[i][j]=1
    第2處填空:bb[i][j]=0
    改錯題
    下列給定程序中函數(shù)fun的功能是:從低位開始取出長整型變量s中奇數(shù)位上的數(shù),依次構(gòu)成一個新數(shù)放在t中,例如,當s中的數(shù)為7654321時,t中的數(shù)為7531。
    請改正程序中的錯誤,使它能得出正確的結(jié)果。
    注意:不要改動main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!
    #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;
    }
    }
    main()
    {
    long s, t;
    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;
    編程題
    請編寫函數(shù)fun,其功能是:將兩個兩位數(shù)的正整數(shù)a、b合并形成一個整數(shù)放在c中。合并的方式是:將a數(shù)的十位和個位數(shù)依次放在c數(shù)的百位和個位上,b數(shù)的十位和個位數(shù)依次放在c數(shù)的十位和千位上。
    例如,當a=45,b=12,調(diào)用該函數(shù)后,c=2415。
    請勿改動主函數(shù)main和其他函數(shù)中的任何內(nèi)容,僅在函數(shù)fun的花括號中填入所編寫的若干語句。
    #include
    #include
    void fun (int a, int b, long *c )
    {
    }
    main ()
    {
    int a, b;  考試大論壇
    long c;
    FILE *out ;
    printf ("Input a, b;");
    scanf ("%d%d", &a, &b);
    fun (a, b, &c);
    printf ("The result is : %ld\n", c);
    out=fopen ("out.dat","w");
    for (a = 20; a < 50; a+=3)
    {
    fun(a, 109-a, &c);
    fprintf(out, "%ld\n", c);
    }
    fclose (out );
    }
    參考答案:
    void fun (int a, int b, long *c)
    {
    *c=(b%10)1000+(a/10)*100+(b/10)*10+a%10;
    }