Trending News
91清大資工所考古題
因為是在家自修,在解題時發現跟書上的不一樣..想問一下大家這題的答案怎麼算才對?
題目如下:
Let A[I:j,1:k] be a two-dimensional array.Each element of A requires
two bytes.Let A[2,2] be stored at 898 and A[1,3] be stored at 910.
Assume that the memory is addressed by byte and the array is stored
sequenially in memory.
What is the address of A[5,7]??
書上的解答寫:
[(7-1)*(7-3)+5]*2=x-910,=>X=978
按照解答的式子算出來X應該是968才對..
但書上解答的式子我不理解
我用我的方式解:
Loc(A[5,7])=Loc(A[1,3])+[(7-3)*7-1+5]*2
=910+64
=974
To Jacob Lee ~
j的值我是這樣算出來的(跟書上的解法式子不同):
因為Loc(A[2,2]) < Loc(A[1,3])==>array A是column-major的儲存方式
Loc(A[1,3]) = Loc(A[2,2]) + [ ( 3 - 2 ) * j - 2 + 1 ] * 2 = 910
==>898 + ( j - 1 ) * 2 = 910
==>j - 1 = 6
所以 j = 7
1 Answer
- Jacob LeeLv 71 decade agoFavorite Answer
We know that the second column dominates the RAM place from A[1, 3] occupies higher address than A[2, 2].
From A[1:j, 1:k] we know that the subscript begins from 1 not 0.
(910 - 898) / 2 = 6 tells us that there are 6 cells between A[1, 3] and A[2, 2].
From this we know that
j - 2 + 1 = 6 // because (3-2) = 1
j = 7
The location of A[5, 7] is
910 + [ 7*(7-3) + (5-1) ] * 2 = 974
Therefore, I am sure that your answer is correct.
Your expression, Loc(..) , is better than mine!
I think you know how to calculate the brown 7.
That is the only thing you do not show in you calculation.
Good luck on your exam. ^_^
2009-04-12 22:32:57 補充:
I do not look very carefully about the content you 補問。
But it looks correct and the answer is correct.
This means that your concept and calculation is correct.
A Chinese saying is 盡信書不如無書.
2009-04-12 22:33:15 補充:
I worked in a public press for more than 8 years.
It is impossible that there is no error in a book!
That will definitely a miracle!
So, The solution you got from the solution book is definitely wrong on this question.
Once again: your answer is correct.