國立暨南國際大學 97 學年度第一學期小考試卷

科目名稱:資訊系統 與網路導論 開課系所:資訊工程 學系 任課教師
吳坤熹
系所別:
年級:
學號:
姓名:
考試日期
2009.1.7

考試時間: 14:10-14:30
Open book; turn off computer & mobile phone

  1. (10%) What value will the following code display?
    // Pass-by-value
    #include <iostream>
    using std::cout;
    using std::endl;

    void test(char p[])
    { cout << --p << endl;
    }

    int main()
    {
    char *name = "CSIE";
    char *p = name;
    test(++name);
    cout << ++name << endl;
    cout << name - p << endl;
    }





  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).
    // pointer to function
    #include <iostream>
    using std::cout;
    using std::endl;

    int sum(int a, int b)
    { return a+b;
    }

    int product(int a, int b)
    { return a*b;
    }

    int main()
    {
    int *pfun(int, int) = sum;
    cout << pfun(3, 7) << endl;
    pfun = product;
    cout << pfun(3, 7) << endl;
    }






  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).
    // Pass function pointer as an argument
    #include <iostream>
    using std::cout;
    using std::endl;

    int compare0(int a, int b)
    { return 0;
    }

    int compare1(int a, int b)
    { return 1;
    }

    int compare2(int a, int b)
    { return a>b?1:0;
    }

    int compare3(int a, int b)
    { return a>b?0:1;
    }

    void sort(int a[], int size, int (*pfun)(int, int))
    {
    int temp;
    for (int i=1; i<size; i++)
    for (int j=size-1; j>=i; j--)
    if (pfun(a[j-1], a[j]))
    {
    temp = a[j-1]; a[j-1] = a[j]; a[j] = temp;
    }
    }

    void print(int a[], int size)
    {
    for (int i=0; i<size; i++)
    cout << "\t" << a[i] ;
    cout << endl;
    }

    int main()
    {
    int a[] = { 1, 8, 9, 5 };
    const int n = sizeof a / sizeof a[0];
    sort(a, n, compare0); print(a, n);
    sort(a, n, compare1); print(a, n);
    sort(a, n, compare2); print(a, n);
    sort(a, n, compare3); print(a, n);
    }