寫出下列C++程式之執行結果:(20 分)
#include <iostream.h>
main() {
int i,j;
for (i=1; i<5; i++) {
for (j=1; j<5; j++) {
cout << '=' << 3*i*j-i-j << ' ';
if (i+j==5) cout << "** ";
}
cout << endl;
}
}
試算出下列含程式片段之時間複雜度。
(10 分)
for i = 0 to n do
begin
j = i;
while j ≠ 0 do j = j / 2;
end
(10 分)
function Euclid (m, n)
while m > 0 do
begin
t = n mod m;
n = m;
m = t;
end
return n
試寫出以下片段程式(用C/C++/Java 都可),使得兩變數x 及y 的數值互換。(20 分)
int x,y,temp;
... /* x 及y 數值已在此設定好了 */
/* 請在此處寫出片段程式,使得兩變數x 及y 的數值互換 */
... /* x 及y 數值已交換 */
考慮下列程式片段:
void main( ){
int value = 4, list = 8;
swap(value, list);
}
void swap(int a, int b){
int temp;
temp = a;
b = temp;
}
若參數傳遞為以值傳遞(passed by value),則在main()執行完swap(value, list)後,
變數value 及list 的值各為多少?(10 分)
若參數傳遞為以址傳遞(passed by address),則在main()執行完swap(value, list)後,
變數value 及list 的值各為多少?(10 分)
對上題,試寫出不用temp 變數的程式,來使得兩變數x 及y 的數值互換。(20 分)
考慮下列程式片段:
int fab (int n){
if (n=1)
return 1;
else return (fab(n-1) + fab(n-2));
}
void main(){
int value;
value = fab(3);
}
請說明當執行到fab( )函數時,activation record(包含function value, parameter,
dynamic link, static link,及return address)的內容。(20 分)