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

科目名稱:程式設計 開課系所:資訊工程 學系 考試日期 2015.3.25
系所別:
年級:
學號:
姓名:
考試時間 8:15-8:25
  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).
    // sizeof()
    #include <iostream>

    int main()
    {
        char msg[20] = "CAESAR CIPHER";
        std::cout << sizeof(msg) << '\n';
        std::cout << static_cast<unsigned short>(msg[0]) << '\n';
        std::cout << sizeof(msg[0]) << '\n';
        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).
    // Caesar Cipher
    #include <iostream>

    int main()
    {
        char msg[20] = "CAESAR CIPHER";
        char new_msg[20] = { 0 };
        int key = 3;
        int i, len;
        for (i=0; i<20; i++)
        {
            new_msg[i] = msg[i] + key;
            if (msg[i] == '\0') break;
        }
        for (len=0; len<20; len++)
            if (new_msg[len] == '\0') break;
        std::cout << len << 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).
    // Matrix
    #include <iostream>
    #include <iomanip>

    int main()
    {
        int a[3][3] = { {2,4,6}, {3,5,7}, {1,6,8} };
        int b[3][3] = { {1,4,7}, {2,5,8}, {3,6,9} };
        int c[3][3] = { 0 };
        int i, j, k;

        for (i=0; i<3; i++)
            for (j=0; j<3; j++)
                for (k=0; k<3; k++)
                    c[i][j] += a[i][k] * b[j][k];

        for (i=0; i<3; i++)
        {
            for (j=0; j<3; j++)
                std::cout << std::setw(4) << c[i][j];
            std::cout << std::endl;
        }

        return 0;
    }