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

科目名稱:C++ Programming
開課系所: Dept. of CSIE
考試日期 2014.3.26
系所別:
年級:
學號:
姓名:
考試時間 18:10-20: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 << 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 == 4 ? '\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, 93, 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[2]) << '\t' << name[2] << endl;
        cout << strlen(name[2]) << '\t' << name[2] << endl;
        cout << sizeof(name[2][2]) << '\t' << name[2][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).

    // switch (P.135)

    #include <iostream>
    using std::cout;

    int main()
    {
        int even = 0, odd = 0;
        char ascii[] = {  78, 69, 84, 72, 69, 82, 76, 65, 78, 68, 83 };
        for (int i=0; i<11; i++)
            switch ( ascii[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 };
        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++)
            {
                if (i == j) continue;
                ++count;
            }
        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).

    // Magic Square
    // Increment Operator (P.74)

    #include <iostream>
    using std::cout;

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

    for (i=0; i<N; i++)
    {
    for (j=0; j<N; j++)
    cout << a[i][j] << '\t';
    cout << std::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[] = "DNAURUISVROPSQY";
    char password[] = "NETHERLANDS";
    char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char mapping[26];
    char reverse[26];
    char c;
    unsigned short i, j, k;

    for (k=0, i=0; (c=password[i]) != '\0'; i++) // Preparing the mapping table
    {
    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 = 3;
    char matrix[N * N + 1];
    char plaintext[] = "Wtoh eads amVdilir t Piwunanntte? xA";

    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;
    }