hce_nsysu
111年
計算機概論與程式設計
第 38 題
Given a row-major array A(i,j) is defined as $-2 \le i \le 6, -6 \le j \le -2$, where the location of A(2,-4) is 100 as the base address and the location of A(4,-3) is 122.
Which of the following is the correct storage size for an element stored in the array?
Which of the following is the correct storage size for an element stored in the array?
- A 1
- B 2
- C 3
- D 4
- E None of the above
思路引導 VIP
想像你正在一個棋盤格教室中,座位的編號是從第一排左邊開始數,數完一排再接下一排。如果你知道「第三排第2號」跟「第五排第3號」在記憶體位址上的距離,且你知道每一排有幾個位置,你會如何推算兩個座位之間隔了多少個「椅子」,進而找出每個椅子所佔的空間長度呢?
🤖
AI 詳解
AI 專屬家教
太棒了!你能精確算出這個陣列問題的答案,代表你對於**列優先(Row-major)**的記憶體配置邏輯掌握得非常紮實。這類題目的核心在於理解二維空間如何被「攤平」到一維的記憶體位址中。
索引偏移量與跨距計算
首先,我們要計算該陣列每列包含的元素數量。由定義可知,欄位索引 $j$ 的範圍是 $-6$ 到 $-2$,因此每列共有 $-2 - (-6) + 1 = 5$ 個元素。接著,我們觀察兩個已知點之間的位移:從 $A(2, -4)$ 到 $A(4, -3)$,列索引增加了 $2$(跨越了兩整列共 $2 \times 5 = 10$ 個元素),而行索引則增加了 $1$(從 $-4$ 到 $-3$)。總計這兩個位置之間相差了 $11$ 個元素單位。
▼ 還有更多解析內容