國立暨南國際大學 102 學年度第二學期 第一次期中考試卷

科目名稱:程式設計 開課系所:資訊工程 學系 考試日期 2014.3.26
系所別:
年級:
學號:
姓名:
考試時間 14:10-16:00
1

2

3
 
4

5
 
6
 
7
 
8
 
9
 
10

  1. (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).

    // Shorthand notation (P.73)
    // Pointers and Arrays (P.194)
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
        int a[] = { 12, 4, 8 };
        (*a /= *(a+1) ) + *(a+2);
        cout << a[0] << endl;
        return 0;
    }


  2. (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).

    // Comparing Values (P.122)
    // Conditional Operator (P.133)

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

    int main()
    {
        int i;
        for (i=0; i<10; i++)
            cout << i << (i % 5 == 0 ? '\n' : '\t');
        return 0;
    }


  3. (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).

    // for Loop (P.140)
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
        unsigned short points[] = { 82, 75, 39, 93, 2, 64, 28, 74, 9, 46 };
        unsigned short K = sizeof points / sizeof( points[0] );
        unsigned short i=0, max, max2, p, p2;
        for (max=points[i++]; i < K ; i++)
        {
            if (max < points[i])
            {
                max = points[i];
                p = i;
            }
        }

        for (max2 = 0, i=0; i < K; i++)
        {
            if (max2 < points[i] && points[i] != max)
            {
               max2 = points[i];
               p2 = i;
            }
        }

        cout << p  << '\t' << max  << '\n';
        cout << p2 << '\t' << max2 << '\n';
        return 0;
    }


  4. (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).

    // strlen vs. sizeof (P.190)
    #include <iostream>
    #include <cstring>
    using std::cout;
    using std::endl;

    int main()
    {
        char name[][10] = { "Alice", "Bob", "Charlie", "Dennis", "Emily" };

        cout << sizeof(name) << endl;
        cout << sizeof(name[1]) << endl;
        cout << strlen(name[1]) << endl;
        cout << sizeof(name[1][2]) << endl;
        return 0;
    }

  5. (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).

    // Type Conversion (P.78)
    // switch (P.135)

    #include <iostream>
    using std::cout;

    int main()
    {
        int even = 0, odd = 1;
        char str[] = "NETHERLANDS"; // 'A' == 65
        for (int i=0; i<12; i++)
            switch ( str[i] % 2 )
            {
                case 0:
                    even++;
                    break;
                case 1:
                    odd++;
                    break;
            }
        cout << "odd = " << odd << '\n'
             << "even = " << even << '\n';
        return 0;
    }


  6. (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).

    // Initialize an Array (P.172)
    #include <iostream>
    using std::cout;

    int main()
    {
        int data[5] = { 2 * 5 };
        int i;
        for (i=0; i<5; i+=2)
            data[i] = i;
        for (i=0; i<5; i++)
            cout << data[i];
        return 0;
    }

  7. (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).

    // continue (P.146) vs. break (P.136)
    #include <iostream>

    int main()
    {
        const int K = 5;
        int count = 0;
        int i, j;
        for (i=0; i<K; i++)
            for (j=0; j<K; j++)
            {
                ++count;
                if (i == j) break;
            }
        std::cout << count << std::endl;
        return 0;
    }
  8. (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).

    // Increment Operator (P.74)
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
    const int N = 3;
    int a[N][N] = { 0 };
    int i = 0, j = N / 2;
    for (int k=1; k <= N * N; k++)
    {
    a[i][j] = k;
    if ( ( i>0 && j<N-1 && a[i-1][j+1] > 0) || (i==0 && j==N-1) )
    ++i;
    else
    {
    if (++j >= N) j = 0;
    if (--i < 0) i = N-1;
    }
    }

    for (i=0; i<N; i++)
    {
    for (j=0; j<N; j++)
    cout << a[i][j];
    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).
    // Substitution Cipher
    #include <iostream>

    int main()
    {
    char ciphertext[] = "NOGPQROHNG";
    char password[] = "NETHERLANDS";
    char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char mapping[26];
    char reverse[26];
    char c;
    unsigned short i, j, k;
        // Preparing the mapping table
    for (k=0, i=0; (c=password[i]) != '\0'; i++)
    {
    if (alphabet[ c - 'A' ] != 0)
    {
    mapping[k++] = c;
    alphabet[ c - 'A' ] = 0;
    }
    }
    for (j=0; k<26; )
    {
    while (alphabet[j] == 0) j++;
    mapping[k++] = alphabet[j++];
    }
    // mapping[k] = '\0';
    // std::cout << "DEBUG - " << mapping << std::endl;

    for (i=0; i<26; i++)
    reverse[ mapping[i] - 'A' ] = 'A' + i;

    for (i=0; (c=ciphertext[i]) != '\0'; i++)
    std::cout << reverse[ c - 'A' ];
    std::cout << std::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).
    // Transposition Cipher
    #include <iostream>
    #include <cstring>
    using std::cout;
    using std::endl;

    int main()
    {
    const unsigned short N = 4;
    char matrix[N * N + 1];
    char plaintext[] = "Uiorknrsred a eCraopi oumtpler loA.tuoA.tuoA.tuo";

    char ciphertext[ sizeof(plaintext) ];
    char *p = plaintext;
    unsigned i, j;

    while (p < plaintext + strlen(plaintext) )
    {
    for (i=0; i<N; i++)
    for (j=0; j<N; j++)
    {
    matrix[i*N + j] = *p ? *p : 'A';
    ++p;
    }

    for (j=0; j<N; j++)
    for (i=0; i<N; i++)
    cout << matrix[i*N + j];
    }
    cout << endl;

    return 0;
    }