2

ARR [15][20] is a two - dimensional array, which is stored in the memory along the row with each of its elements occupying 4 bytes. Find the address of the element ARR [5][15], if the element ARR [10][5] is stored at the memory location 35000.

Comments
  • 5
    It's 42069, obviously.
  • 8
    This sounds like a homework problem to me. What are you looking for?
  • 3
    35000 - 4*20*10-4*6 for 0 offset. Calculate from there.
  • 4
    The correct answer is actually negative pi.
  • 1
    When you declare an array as `ARR[15][20]` it just means there's 15*20*4 = 1200 bytes reserved for ARR in memory so if [10][5] is at 35000 memory offset then [0][0] is at 35000 - 10*5*4 = 35000 - 200 = 34800 offset and [5][15] is at 34800 + 5*15*4 = 34800 + 300 = 35100.

    By the way `ARR[5][15]` can also be accessed as `ARR[75]` and `ARR[10][5]` can be accessed as `ARR[50]` - there's no difference in C-like languages. Array dimensions are solely contractual, offsets are the same.
  • 4
    @cprn your index calculations are off. You cannot simply multiply the two indices together. Otherwise [5][10] and [10][5] would be the same element.
  • 1
    @korrat he'll probably just provide wrong solutions so people will provide the correct one.
    But curious to see that his 0 offset is wildly different from mine
  • 1
    the correct answer is:

    #include <stdio.h>

    int main()
    {
    long long arr[15][20];
    fprintf(stderr, "delta [5][15] : %llu\n", (unsigned long long)(&(arr[5][15]) - &(arr[0][0])));
    fprintf(stderr, "delta [10][5] : %llu\n", (unsigned long long)(&(arr[10][5]) - &(arr[0][0])));
    }

    -> you look under the skirt
  • 1
    @cprn
    its (index_x * size_y) + index_y = index

    you extract coordinates by doing
    index / size_y = index_x
    index % size_y = index_y
  • 1
    @bad-frog Well, obviously, cynide108 did not know that and how is he going to learn if not by public humiliation? 🤷‍♂️ But now you've gave him the answer on a silver platter depriving him of this perfectly good opportunity for proper education.

    The real answer is: it depends. There are two ways of storing arrays in memory - row major and column major. For row major it is like you said but for column major it's the other way around.
  • 1
    @cprn if were talking about c theres only one.
  • 1
    @bad-frog Unless I missed it, the language isn't specified.
  • 2
    @cprn took it as granted since c like are the languages where the question makes most sense.
    afaik in all that goes higher level your arrays become more and more wilder.
    and i dont know of any where you would specify in what order you input the indexes.
  • 2
    If byte addressed,

    10,5 is [(9*20)+5]*4 = 740 bytes from the start.
    5,15 is [(4*20)+15]*4 = 380 bytes from the start.

    So 5,15 will have address 34,640.

    If 4 bytes (word) addressed:

    10,5 is [(9*20)+5] = 185 words from the start.
    5,15 is [(4*20)+15] = 95 words from the start.

    So 5,15 will have address 34,910.
Add Comment