Address issue of matrices in C [closed]


A matrix X [-15 . . . 10, 15 . . . 40] is given.Each memory address holds 1 byte of memoryFind the address of X[10][40].

Note: The notation of the matrix is rather orthodox and confusing. It says that these are the indices of the matrix. I have come across comments that this does not look like a matrix. But I came across this and I can assure hundreds of other people also do, so it will be helpful to clear this for once and for all. The simple question here is if the X[10][40] will be accessible or not as from the indices we get that there are 26 rows and 26 columns. The calculation for the number of rows in done below. My confusion is if said in one line is that as there are 26 rows and 26 columns, then [1][26] will occupy one byte of memory.In the lieu of that will [1][27] also hold 1 byte of memory. Because I haven’t declared it to have 1 byte of memory. And following this will [10][40] also hold 1 byte of memory.I hope my question is now clear. However, if any doubt is still there,I would be more than happy to address it.:).

Lr = Lower limit of row/start row index of a matrix, if not given assume 0 (zero)

Lc = Lower limit of column/start column index of a matrix, if not given assume 0 (zero)

If the beginning location is 1500 determine the location of X[10][40] for both row-major and column-major order.

Every element of the matrix requires 1 byte of storage.

The question is solved by this ugly looking formula.

Number or rows say M = (Ur – Lr) + 1

Number or columns say N = (Uc – Lc) + 1

Row Major Wise Calculation:

Address of A [ I ][ J ] = B + W * [ N * ( I – Lr ) + ( J – Lc ) ]

Column Major Wise Calculation :

Address of A [ I ][ J ] = B + W * [ ( I – Lr ) + M * ( J – Lc ) ]

B = Base address

I = Row subscript of the element whose address is to be found

J = Column subscript of the element whose address is to be found

W = Storage Size of one element stored in the array (in byte)

Lr = Lower limit of row/start row index of a matrix, if not given assume 0 (zero)

Lc = Lower limit of column/start column index of a matrix, if not given assume 0 (zero)

M = Number of rows of the given matrix

N = Number of columns of the given matrix

The question is [10][40] valid or out of bounds here.Can we calculate its address?

Here i proceeded like this:

For X[10][40]:

Row-major:1500+1* [26 * (10 – (-15))) + (40 – 15)]=2175

Column-major:1500 + 1 * [(10– (-15)) + 26 * (40 – 15)]=2175