2
$\begingroup$

I am reading about the address calculation formulas for one and two-dimensional arrays. I have two related doubts concerning it.

In one of the problems, we are asked to calculate the location of B [1700] for an array B [1300....1900]. Now, I understand that B [1700] is actually the 401th element in the array (correct?), so we can calculate its address.

But in another problem, we are given an array X [-15....10,15.....40] and are asked to determine the location of X [15][20] but I don't see how it is even a part of the array since the "row" subscript(i.e. 15) is not contained in (-15....10). (According to me the last element of this array is X [10][40]).

My second doubt is: In another question, we are given the array M[-3......18,-8......7] (stored column-wise where width of element = 8 bytes) and are asked to calculate the base address given that the address of element M [5][10]=4000.

The solution proceeds as -

M [5][10] = BA + W*(M*(j-Lc)+(i-Lr)) therefore, M [5][10]=4000 + 8*(22*(10-0)+(5-0))

i.e. my book answer takes Lc = 0 & Lr = 0 but I think it should be Lc = -8 and Lr = -3? So, which is it? Also can I write a program in which I can verify this for myself (specifically, how can I declare an array in c++ whose subscript doesn't start at 0?)? Thank you so much.

$\endgroup$
3
  • $\begingroup$ @D.W. so am I right when I say that X[15][10] is Not a part of the given array and so we cannot find its location? (BTW, I've seen the same question on some websites too so I wasn't sure whether it was a typo or not). $\endgroup$ – HeWhoMustBeNamed Aug 23 '17 at 1:49
  • $\begingroup$ @D.W. So, is c++ such a language? Also, can you help me with the second doubt? $\endgroup$ – HeWhoMustBeNamed Aug 23 '17 at 11:50
  • $\begingroup$ BTW, thank you to whoever has upvoted this question ( My first upvote!). $\endgroup$ – HeWhoMustBeNamed Aug 24 '17 at 12:44
1
$\begingroup$

For the first question, you can calculate an address for it -- languages that don't do array bounds checking will gladly calculate an address and read/write there, even if it is out of bounds. Of course, if the program is accessing an out-of-bounds element, that's typically a bug of some sort.

For your second question, you are right.

You probably won't run into arrays that start at an index other than 0 in practice very often (if ever), so I wouldn't worry too much about it. Your understanding seems solid.

$\endgroup$
2
  • $\begingroup$ Ok, I've been trying to find similar questions to practise these formulas but have been unsuccessful. I know this is not related to the original question but for further understanding, can you suggest any source having similar problems(i.e. where the index does not start at 0)? $\endgroup$ – HeWhoMustBeNamed Aug 26 '17 at 12:43
  • $\begingroup$ Can you please expand on your answer? (Especially for the first doubt?) And is it possible to check this by writing some program and running it? $\endgroup$ – HeWhoMustBeNamed Aug 26 '17 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.