科目名稱: 程式設計
第一次期中考 開課系所:資工系 考試日期 2018.4.19
系所別:
年級:
學號:
姓名:
考試時間 08:30-10:30
Open book; turn off computer & mobile phone
1
2



3



4
5
6
7
8


9




10



11

12


  1. (10%) After Mr. Carter started coaching Richmond High School, what is his strategy to help them to win the first game with Hercules?

  2. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    // for-loop (P.110)
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
        const int N = 6;
        int i, j;
        for (i=0; i<N; i++)
        {
            for (j=0; j<i; j++)
                cout << ' ';
            for (j=0; j<N-i; j++)
                cout << i;
            cout << endl;
        }
        return 0;
    }

  3. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    // Overflow (P.125)
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
        unsigned char n = 11;
        unsigned char a = 1;
        for (unsigned char i = 1; i<n; i++)
        {
            a = a * i;
            if (i >= 5) cout << static_cast<int>(a) << endl;
        }
        return 0;
    }

  4. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    // End-of-String character '\0' (P.140)
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
        char s[] = "A Clash of Kings.";
        char* tail;
        char* p;
        for (p=s; *p != '\0'; p++)
            ;
        tail = p;
        for (int i=0; i<2; i++)
        {
            while (--tail > s)
            {
                if (*tail == ' ')
                {
                    *tail = '\0';
                    break;
                }
            }
            cout << s << endl;
        }
        return 0;
    }

  5. (10%) What will be the output of the following program?

    // Character Data Type (P.42)
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
        char s[] = {70, 82, 65, 78, 67, 69}; // 'A' == 65
        for (int i=0; i<6; i++)
            if (s[i] % 2) cout << s[i];
        cout << endl;
        return 0;
    }

  6. (10%) What will be the output of the following program?

    // Bitwise Operators (P.69)
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
        char a[] = "CALIFORNIA";
        char mask = 32;
        const int N = sizeof(a)/sizeof(a[0]);
        for (int i=0; i<N-1; i++)
        {
            switch (a[i])
            {
                case 'A':
                case 'E':
                case 'I':
                case 'O':
                case 'U':
                    a[i] = a[i] ^ mask;
                    break;
            }
        }
        cout << a << endl;
        return 0;
    }



  7. (10%) What will be the output of the following program?

    // Binary to Decimal Conversion
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
        unsigned short i;
        char b[9] = "01100101";
        for (i=0; i<8; i++)
            b[i] -= 48;    // '0' = 48
        for (i=0; i<=6; i++)
            b[i+1] = b[i] * 2 + b[i+1];

        cout << b[7] << endl;
        return 0;
    }


  8. (10%) The following program does not run correctly.  Please point out the cause and correct it.

    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;

    int main()
    {
        int p[6] = { 0 };
        int i;
        // srand((unsigned) time(NULL));
        cout << "Input an integer as the random seed -- ";
        cin >> i;
        cout << endl;
        srand(i);

        for (int k = 0; k < 20; k++ )
        {
            i = rand() % 6 + 1; // rand() % 6 is 0 ~ 5
            ++p[i];
        }

        // Statistics of pips
        for (i=0; i<6; i++)
        {
            cout << i + 1 << ": " << p[i];
            cout << ' ';
           
    for (int i=0; i<p[i]; i++)
                cout << '*';

            cout << endl;
        }
        return 0;
    }

  9. (10%) Determine whether the following code has syntax erros or
        not.  If it is correct, predict its output.  If it is
        incorrect, point out the
        mistake(s).

    // Exchange Sort
    #include <iostream>
    #include <cstring>
    using std::cout;
    using std::endl;

    int main()
    {
    char a[][8] = { "Brazil", "Spain", "Gerard",
    "Miami", "Denmark", "Lisbon",
    "Venice", "Hawaii"};
    int N = sizeof(a)/sizeof(a[0]);
    char temp[10];
    for (int i=0; i<N-1; i++)
    {
    for (int j=i+1; j<N; j++)
    if (a[i][0] > a[j][0])
    {
    strcpy(temp, a[i]);
    strcpy(a[i], a[j]);
    strcpy(a[j], temp);
    }
    for (int i=0; i<N; i++)
    cout << a[i] << '\t';
    cout << endl;
    }
    return 0;
    }

  10. (10%) Determine whether the following code has syntax erros or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // Arrays of Pointers to char (P.152)
    #include <iostream>
    #include <cstring>
    using std::cout;
    using std::endl;

    int main()
    {
    const char* a[] = { "Brazil", "Spain", "Gerard",
    "Miami", "Denmark", "Lisbon",
    "Venice", "Hawaii"};
    cout << sizeof(a) << sizeof(a[0]) << endl;
    int N = sizeof(a)/sizeof(a[0]);
    const char* p[N];
    int i;
    for (i=0; i<N; i++)
    {
    cout << a[i] << '\t';
    p[i] = a[i];
    }
    cout << endl;

    const char* temp;
    for (int i=0; i<N-1; i++)
    {
    for (int j=i+1; j<N; j++)
    if (*p[i] < *p[j])
    {
    temp = p[i];
    p[i] = p[j];
    p[j] = temp;
    }
    for (int i=0; i<N; i++)
    cout << p[i] << '\t';
    cout << endl;
    }

    for (int i=0; i<N; i++)
    cout << a[i] << '\t';
    cout << endl;

    return 0;
    }


  11. (10%) Determine whether the following code has syntax erros or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // Pointer Operation (P.149)
    #include <iostream>

    int main()
    {
    int n[] = {2, 9, 1, 0, 9, 6, 0};
    int* p = &n[0];
    int* p1;

    std::cout << *++p;
    std::cout << ++*p;
    std::cout << *p++;
    std::cout << *(++p) << std::endl;;
    p1 = p;
    std::cout << *(p++);
    std::cout << (*p)++;
    std::cout << ++(*p);
    std::cout << *(p++) << std::endl;
    std::cout << ++(*p1);
    std::cout << *(p1++);
    std::cout << *p1 << std::endl;
    return 0;
    }
  12. (10%) When Principal Garrison told Coach Carter, "Your job is to win basketball games, Mr. Carter.  I suggest you start doing your job."  How did Mr. Carter respond?