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

科目名稱:程式設計 開課系所:資訊工程 學系 考試日期 2014.3.18
系所別:
年級:
學號:
姓名:
考試時間 14:10-14:20
  1. (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).
    // Initialize a Matrix
    #include <iostream>

    int main()
    {
        unsigned short data[2][ ] = { { 1, 2, 3, 4}, {5, 6, 7, 8} };
        std::cout << data[1][1] << 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, k;
        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];

        for (i=0; i<N; i++)        // Multiply
            for (j=0; j<N; j++)
                for (k=0; k<N; k++)
                    C[i][j] = A[i][k] * B[k][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[] = "NCNU";
        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;
    }