國立暨南國際大學 104 學年度第二學期 期末考試卷

科目名稱:基礎程式 設計(二) 開課系所:資訊工程 學系 考試日期 2016.6.21
系所別:
年級:
學號:
姓名:
考試時間 18:30-20:30

1
2
3  
4
5  

  1. (20%) 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).

    // Recursive Function Call
    #include <iostream>
    #include <iomanip>
    using std::cin;
    using std::cout;
    using std::setw;

    int p(int,int);
    void space(int,int);

    int main()
    {
        int row = 7;
        for(int i=1;i<=row;i++)
        {
            space(row-i,3);
            for(int j=1;j<=i;j++)
            {
                cout << setw(3) << p(i,j);
                space(1,3);
            }
            cout << '\n';
        }
        return 0;
    }

    int p(int row, int column)
    {
        if(column == 0) // The 0th column element is assumed to 0
            return 0;
        else if(row == 1 && column == 1)
            return 1;
        else if(column > row) // assuming the element is zero (no of columns> no of rows)
            return 0;
        else
            return (p(row - 1, column - 1) + p(row - 1, column));
    }

    void space(int num, int mul) // for spaces in between elements
    {
        int i;
        num *= mul;
        for(i=0; i<num; i++)
            cout << ' ';
    }


  2. (20%) Consider the following code.  If we want the function to handle lowercase hexadecimals in addition to uppercase hexadecimals, what integers should be filled into the blank at   (2)  ?

    // hex2dec
    #include <iostream>
    #include <cstring>
    using std::cout;
    using std::endl;

    int hex2dec(const char* s)
    {
        unsigned int sum = 0;
        unsigned int n;
        for (int i=0; i<strlen(s); i++)
        {
            n = static_cast<unsigned int>(*(s+i))-48;
            if (n>9)
                n -= 7;
            if (n>=42)
                n -=    (2)   ;
            sum = sum * 16 + n;
        }
        return sum;
    }

    int main()
    {
        cout << hex2dec("9") << endl
             << hex2dec("F") << endl
             << hex2dec("20") << endl
             << hex2dec("FF") << endl
             << hex2dec("a") << endl
             << hex2dec("fe") << endl;
        return 0;
    }


  3. (20%) 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).

    // Static object in a function
    #include <iostream>
    using std::cout;
    using std::endl;

    class CData
    {
        public:
            int data;

        CData(int n=0)
        {
            data = n;
        }

        ~CData()
        {
            cout << data << endl;
        }
    };

    int add(CData n) // This parameter is passed by value
    {
        static CData sum(0); // When will a static object be destroyed?
        sum.data += n.data;
        return sum.data;
    } // The object n will be destroyed at the end of this function

    int main()
    {
        CData a(3);
        CData b(5);
        add(a);
        int k = add(b);
        cout << k << endl;
        return 0;
    }


  4. (20%) 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).

    // Vector
    #include <iostream>
    #include <vector>
    using std::vector;

    int main()
    {
        const int N = 10;
        vector<int> a(N, 0);
        for (int i=0; i<N; i+=2)
            a.at[i] = i;
        for (int i=0; i<N; i++)
            std::cout << a.at[i] << std::endl;
        return 0;
    }


  5. (20%) If we replace the OnDraw() function in P.669 as below, what result will be shown on the screen after you run the program? Please specify the coordinates of endpoints of each segment, or the coordinates of centers and boundaries.

    // CView::OnDraw()  (P.669)
    void CSketcherView::OnDraw(CDC* pDC)
    {
        CSketcherDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);
        if (!pDoc)
            return;

        pDC->Rectangle(50, 100, 200, 150);
        pDC->Rectangle(100, 50, 150, 200);
    }