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

科目名稱:程式設計 開課系所:資訊工程 學系 考試日期 2015.4.29
系所別:
年級:
學號:
姓名:
考試時間 08:20-08:30
  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).
    // Defining a struct (P.268)
    #include <iostream>
    using std::cout;
    using std::endl;

    struct Rectangle
    {
        short left;
        short top;
        short right;
        short bottom;
    };

    int main()
    {
        Rectangle aRect;
        cout << sizeof( Rectangle ) << endl;
        cout << sizeof( aRect ) << 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).
    // Accessing the Members of a struct (P.269)
    #include <iostream>
    using std::cout;
    using std::endl;

    struct Rectangle
    {
        short left;
        short top;
        short right;
        short bottom;
    };

    int area(Rectangle aRect)
    {
        return (aRect.right - aRect.left) * (aRect.bottom - aRect.top);
    }

    void changeRect(Rectangle aRect)
    {
        aRect.left  += 50;
        aRect.right -= 50;
        cout << area(aRect) << endl;
    }

    int main()
    {
        Rectangle yard = { 100, 100, 300, 300 };
        changeRect( yard );
        cout << area( yard ) << 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).
    // Accessing Structure Members through a Pointer (P.275)
    #include <iostream>
    using std::cout;
    using std::endl;

    struct Rectangle
    {
        short left;
        short top;
        short right;
        short bottom;
    };

    int area(Rectangle aRect)
    {
        return (aRect.right - aRect.left) * (aRect.bottom - aRect.top);
    }

    void changeRect(Rectangle* pRect)
    {
        *pRect.left  += 50;
        *pRect.right -= 50;
        cout << area( *pRect ) << endl;
    }

    int main()
    {
        Rectangle yard = { 100, 100, 300, 300 };
        changeRect( &yard );
        cout << area( yard ) << endl;
        return 0;
    }