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

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

    struct Circle
    {
        int x;
        int y;
        int radius;
    }

    int main()
    {
        Circle a, b;
        a.x = 5; a.y = 10; a.radius = 15;
        b = a;
        cout << b.x << 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).
    // Initialize a struct (P.355)
    #include <iostream>
    using std::cout;
    using std::endl;

    struct Rational
    {
        int numerator;
        int denominator;
    };

    int gcd(int a, int b)
    {
        if (a == 0)
        {
            return (b == 0 ? 0 : b);
        }
        else
            return gcd(b % a, a);
    }

    int main()
    {
        Rational a = { 4, 29 };
        Rational b;
        b = a;
        cout << ( b.denominator /= gcd(b.numerator, b.denominator) ) << 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 the Members of a struct (P.358)
    #include <iostream>
    using std::cout;
    using std::endl;

    struct Circle
    {
        int x;
        int y;
        int radius;
    };

    void MoveCircle(Circle aCircle, int x, int y);

    int main()
    {
        Circle a = { 10, 10, 5};
        MoveCircle(a, 20, 30);
        cout << a.x << endl;
        return 0;
    }

    void MoveCircle(Circle aCircle, int x, int y)
    {
        aCircle.x = x;
        aCircle.y = y;
        return;
    }