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

科目名稱:程式設計 開課系所:資訊工程 學系 考試日期 2014.3.18
系所別:
年級:
學號:
姓名:
考試時間 18:10-18:20
  1. (10%) What would the following code show on the screen?
    // Initialize a Matrix
    #include <iostream>

    int main()
    {
        unsigned short data[2][4] = { { 1, 2, 3   }, {5, 6     } };
        std::cout << data[0][2] << std::endl;
        return 0;
    }


  2. (10%) Determine whether the following code is correct or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // Matrix Multiplication
    #include <iostream>

    int main()
    {
        const unsigned short N = 3;
        unsigned short i, j;
        unsigned short A[N][N] = { { 1, 0, 1 },
                                   { 1, 1, 0 },
                                   { 0, 1, 1}
                                 };
        unsigned short B[N][N] = { { 1, 1, 0 },
                                   { 0, 1, 1 },
                                   { 1, 0, 1 }
                                 };
        unsigned short C[N][N] = { 0 };

        for (i=0; i<N; i++)        // Multiply
            for (j=0; j<N; j++)           
                    C[i][j] += A[i][j] * B[i][j];

        for (i=0; i<N; i++)        // Print out
        {
            for (j=0; j<N; j++)
                std::cout << C[i][j] << ' ';
            std::cout << std::endl;
        }
        return 0;
    }

  3. (10%) Determine whether the following code is correct 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 plaintext[] = "ZULU";
        char password[] = "UNIVERSITY";
        char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        char mapping[27];
        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; (c=plaintext[i]) != '\0'; i++)
            std::cout << mapping[ c - 'A' ];
        std::cout << std::endl;

        return 0;
    }