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

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

1
2
3
4
5  
6
7
8
9  
10
11
12


  1. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // Decrement Operator (P.59)
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
        int a = 29;
        int b = 3;
        cout << a-- - --b << endl;
        cout << a << b << endl;
        return 0;
    }




  2. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    // Shorthand notation (P.58)
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
        int a = 2020;
        int b =    4;
        int c =    1;
        a /= b + c;
        cout << a << endl;
        return 0;
    }


  3. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    // Type Conversion (P.63)
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
        float R = 2.0;
        float A = 3 / 4;
        cout << A * R * R << endl;
        cout << R * R * A << endl;
        return 0;
    }


  4. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    // Conditional Operator (P.103)
    #include <iostream>

    int main()
    {
        int i;
        for (i=5; i<=9; i++)
            std::cout << ( i % 2 ? "Even" : "Odd" ) << std::endl;
        return 0;

    }


  5. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    // for Loop (P.110)
    #include <iostream>

    int main()
    {
        int i;
        int sum = 41;
        for (i=5; i<=9; i+=2)  
            std::cout << i << std::endl;
            sum += i;
        std::cout << sum << std::endl;
        return 0;
    }


  6. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    // MATH Functions

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

    int main()
    {
        float b = -4.1;
        int n = b;
        int m = abs(b);
        float a = fabs(b);
        cout << n << endl;
        cout << m << endl;
        cout << a << endl;
        cout << ceil(b) << endl;
        cout << floor(b) << endl;
        return 0;
    }


  7. (10%) Determine whether the following code has syntax errors 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[] = "NKGGYWKKI";
    char password[] = "CALIFORNIA";
    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;
    }


  8. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    // Type Conversion (P.63)
    // switch (P.105)

    #include <iostream>
    using std::cout;

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

  9. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

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

    int main()
    {
    const int N = 5;
    int data[N] = { 3 * N };

    int i;
    for (i=1; i<N; i+=2)       
    data[i] = i;
      
    for (i=0; i<N; i++)

          cout << data[i] << std::endl;   
    return 0;

    }



  10. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // continue (P.116) vs. break (P.115)
    #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) break;
    ++count;
    }
    std::cout << count << std::endl;
    return 0;
    }


  11. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // continue (P.116) vs. break (P.115)
    #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;
    }
  12. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // Transposition Cipher - Scytale
    // The input key specifies the diameter of the scytale

    #include <iostream>
    #include <cstring>
    #include <cmath>
    using std::cin;
    using std::cout;
    using std::endl;

    int main()
    {
    const int key = 6;
    const int height = key;
    const int MAX_MESSAGE_LENGTH = 200;
    char plaintext[ MAX_MESSAGE_LENGTH];

    char ciphertext[ MAX_MESSAGE_LENGTH ] =
    "SFAKI IOTUEANUHASGGNEN EADR A PI YTOONLE FRGEWT E E H9 F DE1";
    unsigned i, j, width;

    int length = 0;
    while (ciphertext[length] != '\0')
    length++;
    width = ceil(1.0 * length / height);

    for (j=0; j<width; j++)
    for (i=0; i<height; i++)
    {
    plaintext[i * width + j] = ciphertext[i + height * j] ;
    }
    plaintext[ width * height ] = '\0';
    cout << plaintext << endl;
    return 0;
    }