111 年 國立中正大學資訊工程學系碩士班乙組《計算機概論》
第 1 題10 分
- (10%) Carefully examine the following sentences and give your answer, true (T) or false (F), for each sentence. Two points are given for each correct answer.
(a) The worst-case time complexity of any sorting algorithm is .
(b) If and , then .
(c) The maximum number of nodes in a binary tree of depth is .
(d) is the postfix expression of .
(e) We cannot overload a function using the same number of parameters in C++.
登入後即可作答並保存紀錄。
核心觀念
本題考查:
- 漸進符號 與 的定義
- 比較式排序的時間下限
- 二元樹的最大節點數
- 中序運算式與後序運算式的轉換
- C++ 函式多載(function overloading)
解題方法與選項分析
(a) The worst-case time complexity of any sorting algorithm is .
答案:F
表示漸進下界。對於「基於比較」的排序演算法,最壞情況時間複雜度確實具有下界:
原因是比較排序可表示成決策樹。要排列 個不同元素,至少要區分 種排列,因此決策樹高度至少為:
但題目說的是 任何排序演算法,範圍包含非比較排序,例如 counting sort、radix sort。當鍵值範圍適當時,counting sort 的時間複雜度可為:
其中 是鍵值範圍;當 時,整體為 ,低於 。
因此, 只適用於比較排序,不能套用到所有排序演算法。
(b) If and , then .
答案:F
只表示函數的漸進上界,不代表兩個函數完全相等。
例如:
可得:
且:
但顯然:
所以兩個函數同屬於 ,不代表兩者相等。
(c) The maximum number of nodes in a binary tree of depth is .
答案:T
本題採用二元樹深度以「層數」計算的常見定義,根節點位於第 層。
二元樹每一個節點最多有兩個子節點,因此:
- 第 層最多有 個節點
- 第 層最多有 個節點
- 第 層最多有 個節點
- 第 層最多有 個節點
總節點數為等比級數:
其和為:
因此,深度為 的二元樹最多有:
個節點。
(d) is the postfix expression of .
答案:T
中序運算式為:
乘法優先於加法,因此運算順序是:
再將結果與 相加:
轉為後序表示法時,每個運算子放在其兩個運算元之後:
- 輸出
- 輸出
- 輸出
- 輸出乘法運算子
- 輸出加法運算子
所以後序運算式為:
第 2 題10 分
- (10%) Please write down the result of the following C++ code.
#include <iostream>
using namespace std;
class B {
public:
B() { cout << "hi B" << endl; }
virtual ~B() { cout << "bye B" << endl; }
};
class D : public B {
public:
D() { cout << "hi D" << endl; }
virtual ~D() { cout << "bye D" << endl; }
};
class DD : public D {
public:
DD() { cout << "hi DD" << endl; }
virtual ~DD() { cout << "bye DD" << endl; }
};
int main() {
B* b_ptr = new D;
D* d_ptr = dynamic_cast<D*>(b_ptr);
if (d_ptr == nullptr) cout << "no d_ptr" << endl;
DD* dd_ptr = dynamic_cast<DD*>(b_ptr);
if (dd_ptr == nullptr) cout << "no dd_ptr" << endl;
delete b_ptr;
return 0;
}
登入後即可作答並保存紀錄。
這題考察 C++ 的繼承、多型、虛函數、建構子與解構子的調用順序,以及 dynamic_cast 的使用。
核心觀念:
- 繼承與建構子調用:當創建一個派生類別物件時,會先調用基底類別的建構子,然後再調用派生類別自身的建構子。
- 解構子調用順序:當物件被銷毀時,會先調用派生類別的解構子,然後再依序調用基底類別的解構子。
- 虛函數與多型:
virtual關鍵字確保在運行時調用正確的函數版本(特別是解構子)。 dynamic_cast:用於在繼承體系中進行運行時的類型轉換。它會檢查轉換是否安全。如果轉換失敗(目標類型不是來源物件的實際類型或其基底類型),則返回nullptr。
解題過程:
-
B* b_ptr = new D;- 這行程式碼創建了一個
D類別的物件,並將其指標賦值給一個B*型別的指標b_ptr。 - 創建
D物件時,調用順序如下:B的建構子被調用:輸出 "hi B"。D的建構子被調用:輸出 "hi D"。
- 目前為止的輸出:
hi B換行,hi D換行。
- 這行程式碼創建了一個
-
D* d_ptr = dynamic_cast<D*>(b_ptr);b_ptr指向一個實際為D型別的物件。dynamic_cast<D*>(b_ptr)嘗試將b_ptr(類型為B*)轉換為D*。- 由於
b_ptr實際指向的物件確實是D類別(或者D的派生類別),這個轉換是安全的。 d_ptr將會成功指向該D物件。
-
if (d_ptr == nullptr) cout << "no d_ptr" << endl;d_ptr不是nullptr,所以這段if語句不會執行。
-
DD* dd_ptr = dynamic_cast<DD*>(b_ptr);b_ptr指向一個實際為D型別的物件。dynamic_cast<DD*>(b_ptr)嘗試將b_ptr(類型為B*)轉換為DD*。- 由於
b_ptr實際指向的物件是D類別,而D類別並非DD類別(DD是D的派生類別,但D不等於DD),這個轉換是不安全的。
第 3 題8 分
- (8%) Please write down the result after we compile the C++ code. Then, please give the reason for the result.
#include <iostream>
class Money {
int dollar;
public:
Money() : dollar(0) {}
Money(int _dollar) : dollar(_dollar) {}
const Money operator+(const Money& amount) const {
return Money(dollar + amount.dollar);
}
};
int main() {
Money baseAmount(100), fullAmount;
fullAmount = baseAmount + 25;
fullAmount = 25 + baseAmount;
return 0;
}
登入後即可作答並保存紀錄。
核心觀念
本題考查 C++ 運算子重載(Operator Overloading)中,成員運算子函數(Member operator function)與轉換建構子(Converting constructor)的交互限制:
- 成員運算子的左運算元限制:若二元運算子
operator+宣告為類別成員函數,編譯器會將a + b轉譯為a.operator+(b)。因此左運算元a必須是該類別的物件,不會對左運算元進行隱含型別轉換。 - 非
explicit單參數建構子的隱含轉換:Money(int _dollar)未加上explicit關鍵字,可將int隱含轉換為暫時的Money物件,但此轉換僅能套用在函數的參數列(即成員運算子的右運算元)。
解題方法
1. 編譯結果(Result after compilation)
編譯錯誤(Compile Error),錯誤發生在 fullAmount = 25 + baseAmount; 這一行。
2. 逐行分析(Reason for the result)
Money baseAmount(100), fullAmount;baseAmount呼叫Money(int)建構子初始化,dollar為100;fullAmount呼叫預設建構子Money()初始化,dollar為0。
fullAmount = baseAmount + 25;(編譯成功)- 編譯器將其解析為
baseAmount.operator+(25)。 - 左運算元
baseAmount本身即為Money物件,可合法呼叫成員函數operator+。 - 右運算元
25(型別為int)會透過非explicit建構子Money(int)隱含轉換為暫時物件Money(25),傳入const Money& amount參數,回傳Money(125)並指派給fullAmount。
- 編譯器將其解析為
fullAmount = 25 + baseAmount;(編譯失敗)- 左運算元為基本型別常數
25(型別為int),右運算元為Money物件baseAmount。 - 由於
operator+是Money的成員函數,編譯器無法寫成25.operator+(baseAmount)(內建型別沒有成員函數,且 C++ 不會將左運算元隱含轉換為Money後再呼叫成員函數)。 - 編譯器接著嘗試尋找全域非成員運算子重載(例如
operator+(const Money&, const Money&)或operator+(int, const Money&)),但程式中並未定義,因此無法解析此運算式,直接回報編譯錯誤(如no match for 'operator+' (operand types are 'int' and 'Money'))。
- 左運算元為基本型別常數
第 4 題12 分
-
(12%) A rational number can be represented by the fixed-point representation. The fixed-point representation uses an implied binary point to separate integer part and fraction part. Assume that we have 8-bits fixed-point format to represent a number, where 4 bits for integer part and 4 bits for fraction part. Two's complement is used for signed values.
integer fraction 4 bits 4 bits
(a) (4%) A singed number represented by the fixed-point format is 00100110 (Binary). What is its decimal value?
(b) (4%) Represent the number -6.3125 (Decimal) in the fixed-point representation.
(c) (4%) IEEE-754 format and fixed-point representation are usually used to represent rational numbers. What are the advantage and disadvantage of using fixed-point representation?
登入後即可作答並保存紀錄。
核心觀念
本題評量定點數表示法(Fixed-Point Representation)、二補數(Two's Complement)數值轉換以及定點數與 IEEE-754 浮點數表示法之優缺點比較。
題目給定格式為 8 位元定點數,其中高 4 位元表示整數部分,低 4 位元表示小數部分:
- 整數部分 4 位元,權重分別為 (最高位為符號位,採用二補數)。
- 小數部分 4 位元,權重分別為 。
(a) 將 轉換為十進位
給定位元字串為 :
- 最高位為 0,表示此數為正數。
- 整數部分為 :
- 小數部分為 :
- 結合整數與小數部分:
(b) 將 轉換為 8 位元二補數定點數表示法
- 先求絕對值 的二進位表示:
- 整數部分 。
- 小數部分 :
- (取整數 0)
- (取整數 1)
- (取整數 0)
- (取整數 1)
故小數部分為 。
- 正數 之 8 位元定點數表示為 (即二進位字串 )。
- 取二補數以求得負數表示:
- 取反碼(各個位元反轉):。
- 加 1:。
- 數值驗證:
第 5 題16 分
- (16%) Explain the following terms:
(a) (4%) Compiler
(b) (4%) Interpreter
(c) (4%) Assembler
(d) (4%) Linker
登入後即可作答並保存紀錄。
本題考驗系統軟體中程式語言翻譯與建置工具鏈(Toolchain)的四個核心元件:編譯器(Compiler)、直譯器(Interpreter)、組譯器(Assembler) 與 連結器(Linker) 的定義與職責。
核心觀念
高階語言通常需透過翻譯程式轉換為低階語言或直接執行;在典型的編譯式語言建置流程中,依序會經過:
各小題解析
(a) Compiler(編譯器)
- 定義:將整個以高階程式語言(如 C、C++)撰寫的原始碼(Source Code),在程式執行前一次性翻譯為低階語言(如組合語言或機器碼),或翻譯為中間碼/位元組碼(Bytecode,如 Java)的系統軟體。
- 主要階段:包含詞法分析(Lexical Analysis)、語法分析(Syntax Analysis)、語意分析(Semantic Analysis)、最佳化(Optimization)及目標碼產生(Code Generation)。
- 特點:執行前已完成翻譯與最佳化,因此執行效能通常較高;但編譯需花費時間,且每次修改原始碼後皆須重新編譯。
(b) Interpreter(直譯器)
- 定義:無須事先將整份高階語言原始碼翻譯成機器碼或產生獨立可執行檔,而是逐行(或逐敘述)讀取、解析原始碼並直接執行對應操作的系統軟體(例如 Python、早期 BASIC)。
- 特點:省去編譯時間,開發與除錯快速,且通常具較佳的跨平台性(只要目標平台有對應的直譯器即可執行);但因每次執行時都必須重新解析與執行,執行速度通常較編譯器慢。
(c) Assembler(組譯器)
- 定義:將以組合語言(Assembly Language)撰寫的低階程式翻譯成對應處理器架構之二進位機器碼(Machine Code)的系統軟體。
第 6 題10 分
- (10%) Assume that we have eight registers (i.e., d0, d1, ..., d7) and the following instructions. Each register has 16 bits, bit 0 ~ bit 15. The operands dst and src1 are registers. The operand src2 is a register or an immediate value. The operand n is an immediate value. The immediate value is represented by a hexadecimal value with a leading "0x".
| Instruction | Description |
|---|---|
| AND dst, src1, src2 | Perform bit-wise AND operation on the value in src1 and src2, store the result in dst. |
| OR dst, src1, src2 | Perform bit-wise OR operation on the value in src1 and src2, store the result in dst. |
| ADD dst, src1, src2 | Perform addition operation on the value in src1 and src2, store the result in dst. |
| SHR dst, src1, n | Move the bits in src1 right by n bits. The left-hand n bits of src1 are set to 0. The result is written to dst. |
| SHL dst, src1, n | Move the bits in src1 left by n bits. The right-hand n bits of src1 are set to 0. The result is written to dst. |
(a) (4%) Write the instructions to clear all the bits in register d1 to zero.
(b) (6%) Write the instructions to extract the bit 5 ~ bit 10 of register d1 and to place it in the bit 0 to bit 5 of register d2. The bit 6 to bit 15 of register d2 are unchanged. d1 is unchanged.
登入後即可作答並保存紀錄。
核心觀念
本題考查組合語言中 16 位元暫存器的位元邏輯運算(AND、OR)與邏輯位移(SHR、SHL),重點在於:
- 位元歸零(Clear):利用
AND遮罩或將所有位元移出。 - 位元欄位擷取與寫入(Bit-field Extraction & Insertion):透過位移對齊、遮罩隔離目標位元、清除目的端對應欄位後再以
OR合併,同時確保來源暫存器與其餘位元不受破壞。 - 立即值格式規範:題目規定所有立即值(包含
src2與位移量n)皆須以0x開頭的十六進位表示(例如 、)。
解題方法
(a) 將暫存器 d1 的所有位元清為 0 (4%)
可任選下列其中一種單一指令完成:
- 方法一(使用
AND遮罩):將d1與0x0000做位元 AND 運算,所有位元皆歸零:AND d1, d1, 0x0000 - 方法二(使用邏輯位移
SHR或SHL):將 16 位元暫存器右移或左移 16 位(),補入 16 個 0:
(或SHR d1, d1, 0x10SHL d1, d1, 0x10)
(b) 擷取 d1 之 bit 5 ~ bit 10 並寫入 d2 之 bit 0 ~ bit 5 (6%)
- 需求分析:
- 擷取範圍:
d1的 bit 5 ~ bit 10(共 個位元)。 - 寫入位置:
d2的 bit 0 ~ bit 5。 - 不變條件:
d2的 bit 6 ~ bit 15 維持原值,d1維持原值。 - 選用
d0(或d3~d7任一未使用的暫存器)作為暫存器。
- 擷取範圍:
- 步驟推導:
- 右移對齊:將
d1邏輯右移 5 位(0x5),使 bit 5 ~ bit 10 對齊至 bit 0 ~ bit 5,結果存入d0(d1保持不變):SHR d0, d1, 0x5 - 遮罩隔離:保留
d0的最低 6 個位元(bit 0 ~ bit 5),清除 bit 6 ~ bit 15。遮罩為 :AND d0, d0, 0x003F - 清除
d2目標位元:將d2的 bit 0 ~ bit 5 歸零,同時保留 bit 6 ~ bit 15。
- 右移對齊:將
第 7 題6 分
- (6%) The following C program is executed on a machine. The output at S1 is 18.
#include <stdio.h>
int main(void) {
int a = 0x12345678;
unsigned char *c = (unsigned char*)(&a);
printf("%d\n", *c); // S1
printf("%d\n", *(c+3)); // S2
return 0;
}
(a) (3%) Is the byte order of the machine little-endian or big-endian?
(b) (3%) What is the output at S2?
登入後即可作答並保存紀錄。
核心觀念
本題考查計算機組織與 C 語言指標底層記憶體佈局中,位元組順序(Byte Order: Little-Endian vs Big-Endian)的定義與十六進位/十進位輸出運算:
- 位元組順序定義:
- 考慮 32-bit 整數
a = 0x12345678(共 4 個位元組):- 最高有效位元組 (Most Significant Byte, MSB):
0x12 - 次高有效位元組:
0x34 - 次低有效位元組:
0x56 - 最低有效位元組 (Least Significant Byte, LSB):
0x78
- 最高有效位元組 (Most Significant Byte, MSB):
- Big-Endian(大端序):MSB 儲存在最低記憶體位址(即位址順序由低至高依序存放:
0x12, 0x34, 0x56, 0x78)。 - Little-Endian(小端序):LSB 儲存在最低記憶體位址(即位址順序由低至高依序存放:
0x78, 0x56, 0x34, 0x12)。
- 考慮 32-bit 整數
- 指標位移與格式化輸出:
- 指標
c指向&a的最低位址(Base address)。 - 語句 S1 輸出
*c(即最低位址之位元組值)。 - 語句 S2 輸出
*(c+3)(即位移 3 個位元組、最高記憶體位址之位元組值)。 %d格式化字串將印出該位元組的十進位數值。
- 指標
解題方法
(a) 判定該機器是 Little-endian 還是 Big-endian (3%)
- 分析已知條件:
- 整數變數
a = 0x12345678。 - 指標
c的型別為unsigned char*,指向變數a的起始記憶體位址(即最低位址&a + 0)。 - 題幹明確指出:在 S1 處的輸出結果為
18。
- 整數變數
- 進位制轉換與位元組對應:
- 注意:
printf("%d ", *c)輸出的是十進位整數。 - 將十進位數值 18 轉換為十六進位:
- 這表示最低位址
*c所儲存的位元組正是十六進位的0x12。
- 注意:
- 字節序判定:
- 在數值
0x12345678中,0x12是最高有效位元組 (MSB)。 - 由於 MSB (
0x12) 被存放在最低的記憶體位址,這完全符合 Big-endian(大端序) 的標準定義! - 結論:該機器的字節序為 Big-endian。
- 在數值
(b) 計算在 S2 處的輸出值 (3%)
- Big-endian 下的記憶體佈局:
- 假設
&a的基底記憶體位址為 ,則連續 4 個位元組在記憶體中的排列如下:- 位址 (即
*c):0x12(十進位 18,S1 輸出) - 位址 (即
*(c+1)):0x34 - 位址 (即
*(c+2)):0x56
- 位址 (即
- 假設
第 8 題22 分
- (22%) Given the following numbers: 37, 5, 26, 1, 61, 11, 59, 15, 48, 19, please answer the following questions.
(a) (8%) Please sort these numbers in ascending order with the following quicksort function in C, where the swap function is used to swap two numbers. Note that the first number in each sublist is always selected as the pivot, and you must show necessary steps such that the use of the quicksort function can be recognized.
(b) (7%) Assume that the numbers are input sequentially. Please construct a binary search tree without rotations.
(c) (7%) Please construct a max heap by adding the numbers one by one. Please do not directly heapify it.
void quicksort(int a[], int left, int right) {
int pivot, i, j;
while (left < right) {
i = left;
j = right + 1;
pivot = a[left];
do {
do i++; while (a[i] < pivot);
do j--; while (a[j] > pivot);
if (i < j) swap(&a[i], &a[j]);
} while (i < j);
swap(&a[left], &a[j]);
if (j - left < right - j) {
quicksort(a, left, j - 1);
left = j + 1;
} else {
quicksort(a, j + 1, right);
right = j - 1;
}
}
}
登入後即可作答並保存紀錄。
核心觀念
本題考查三種基本資料結構與排序方法:
-
Quicksort(依題目 C 程式實作)
- 每次以子陣列第一個元素
a[left]作為pivot。 - 指標
i從left往右遞增,停在第一個滿足a[i] >= pivot的位置;指標j從right + 1往左遞減,停在第一個滿足a[j] <= pivot的位置。 - 若
i < j,交換a[i]與a[j]並繼續掃描;當i >= j時結束迴圈,交換a[left]與a[j],使pivot歸位至索引j。 - 接著比較左右兩半長度:較短的子陣列交由遞迴呼叫
quicksort處理,較長的子陣列則更新left或right並由while迴圈繼續處理(尾遞迴優化,使遞迴深度控制在 )。
- 每次以子陣列第一個元素
-
Binary Search Tree (BST)
- 小於目前節點的值往左子樹插入,大於目前節點的值往右子樹插入。
- 依輸入順序逐一插入,且不進行平衡旋轉。
-
Max Heap(逐一插入)
- 每個父節點皆須滿足 。
- 逐一插入時,新元素一律先放到陣列末端(最下層最左側空位),若以 0-based 索引表示,索引 的父節點索引為 (1-based 則為 )。
- 若新元素大於父節點,則向上交換(sift-up / percolate-up),直到小於等於父節點或抵達根節點為止。
(a) 依指定 Quicksort 排序
將數列存入 0-based 陣列 a[0..9]:
1. 呼叫 quicksort(a, 0, 9)
left = 0,right = 9,pivot = a[0] = 37i停在索引4(a[4] = 61 >= 37),j停在索引9(a[9] = 19 <= 37)。因i < j,交換a[4]與a[9]:
- 繼續掃描:
i停在索引6(a[6] = 59 >= 37),j停在索引7(a[7] = 15 <= 37)。因i < j,交換a[6]與a[7]:
- 繼續掃描:
i停在索引7(59 >= 37),j停在索引6(15 <= 37)。此時i >= j,結束do-while,交換a[0]與a[6]:
j = 6。因j - left = 6不小於right - j = 3,先遞迴處理右半部quicksort(a, 7, 9),再設right = 5繼續while迴圈。
2. 遞迴 quicksort(a, 7, 9):子陣列 [59, 48, 61]
left = 7,right = 9,pivot = a[7] = 59i停在索引9(61 >= 59),j停在索引8(48 <= 59)。因i >= j,交換a[7]與a[8]:
j = 8。因j - left = 1不小於right - j = 1,遞迴quicksort(a, 9, 9)(立即返回),並設right = 7後結束while迴圈。
3. while 迴圈繼續處理 left = 0, right = 5:子陣列 [15, 5, 26, 1, 19, 11]
pivot = a[0] = 15i停在索引2(26 >= 15),j停在索引5(11 <= 15)。交換a[2]與a[5]:
- 繼續掃描:
i停在索引4(19 >= 15),j停在索引3(1 <= 15)。因i >= j,交換a[0]與a[3]:
j = 3。因j - left = 3不小於right - j = 2,先遞迴處理右半部quicksort(a, 4, 5),再設right = 2繼續while迴圈。
4. 遞迴 quicksort(a, 4, 5):子陣列 [19, 26]
left = 4,right = 5,pivot = a[4] = 19i停在索引5(26 >= 19),j停在索引4(19 <= 19)。因i >= j,交換a[4]與a[4](不變):
j = 4。因j - left = 0 < right - j = 1,遞迴quicksort(a, 4, 3)(立即返回),並設left = 5後結束while迴圈。
5. while 迴圈繼續處理 left = 0, right = 2:子陣列 [1, 5, 11]
pivot = a[0] = 1i停在索引1(5 >= 1),j停在索引0(1 <= 1)。因i >= j,交換a[0]與a[0](不變):
j = 0。因j - left = 0 < right - j = 2,遞迴quicksort(a, 0, -1)(立即返回),並設left = 1繼續while迴圈。
6. while 迴圈繼續處理 left = 1, right = 2:子陣列 [5, 11]
pivot = a[1] = 5i停在索引2(11 >= 5),j停在索引1(5 <= 5)。因i >= j,交換a[1]與a[1](不變):
j = 1。因j - left = 0 < right - j = 1,遞迴quicksort(a, 1, 0)(立即返回),並設left = 2後結束while迴圈。
排序完成後結果為:
(b) 依序建立 Binary Search Tree
依序插入 37, 5, 26, 1, 61, 11, 59, 15, 48, 19(不旋轉):
- 插入
37:成為根節點。 - 插入
5:,置於37的左子節點。 - 插入
26:、,置於5的右子節點。 - 插入
1:、,置於5的左子節點。 - 插入
61:,置於37的右子節點。 - 插入
11:、、,置於26的左子節點。 - 插入
59:、,置於61的左子節點。 - 插入
15:、、、,置於11的右子節點。 - 插入
48:、、,置於59的左子節點。
第 9 題6 分
- (6%) The function func has two input parameters and exchanges their values. The output of the following program is "200 100". Complete the following program in (1), (2), and (3).
#include <stdio.h>
void func((1)) {
(2)
}
int main(void) {
int a = 100, b = 200;
func( (3));
printf("%d %d\n", a, b);
return 0;
}
登入後即可作答並保存紀錄。
這題考察 C 語言的函數參數傳遞機制,特別是「傳址呼叫」(Call by Reference)的實現方式,以及函數原型(Function Prototype)和函數定義的語法。
核心觀念:
- 函數參數傳遞:C 語言預設是「傳值呼叫」(Call by Value),即函數接收的是參數的副本。若要實現「傳址呼叫」(相當於 C++ 的 Call by Reference),需要傳遞參數的指標。
- 指標 (Pointer):變數的記憶體位址。
- 解引用 (Dereference):通過指標訪問其指向的值(使用
*操作符)。 - 函數原型與定義:函數定義提供了函數的實現,而函數原型(或函數頭)則聲明了函數的名稱、返回類型和參數類型。
解題過程:
題目要求函數 func 能夠交換兩個整數變數的值,並且程式執行的輸出結果是 "200 100"。
在 main 函數中,a 的初始值是 100,b 的初始值是 200。
printf("%d %d\n", a, b); 輸出 a 和 b 的值。
如果輸出是 "200 100",這意味著在 func 函數被呼叫後,a 的值變成了 200,而 b 的值變成了 100。
這表明 func 函數成功地交換了 a 和 b 的值。
由於 C 語言是傳值呼叫,直接傳遞 a 和 b 的值給 func 無法修改 main 中的 a 和 b。
為了讓 func 能夠修改 main 中的變數,我們必須傳遞 a 和 b 的 位址(指標)。
(1) 函數參數列表:void func((1))
* func 函數需要接收兩個整數的指標,以便能夠修改它們指向的變數。
* 因此,參數類型應該是 int *。
* 為了能夠在函數內部訪問和修改指標指向的值,我們需要為這兩個指標命名,例如 ptr_a 和 ptr_b。
* 所以,(1) 應該是 int *ptr_a, int *ptr_b。
(2) 函數體:(2)
* 在函數體內部,我們需要交換 ptr_a 和 ptr_b 所指向的值。
* 假設 ptr_a 指向 a,ptr_b 指向 b。
* 交換兩個變數的值通常需要一個臨時變數。
* 步驟:
1. 創建一個臨時整數變數,例如 temp。
2. 將 ptr_a 指向的值(即 *ptr_a)存入 temp。
3. 將 ptr_b 指向的值(即 *ptr_b)賦值給 ptr_a 指向的位置(即 *ptr_a)。
4. 將 temp 中的值賦值給 ptr_b 指向的位置(即 *ptr_b)。
* 所以,(2) 應該是: