Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) Give the accessing formula for addr(A[ I ][ J ][ K ]) if A is declared in C++

ID: 3593762 • Letter: 1

Question

1) Give the accessing formula for

addr(A[ I ][ J ][ K ]) if A is declared in C++ by:

typex A[ m ][ n ][ p ];

2) Assume C++ uses column-major storage instead of row-major. Give the accessing formula for

addr(B[ I ][ J ]) if B is declared by:

typex B[ m ][ n ];

Note: In problems 1 and 2, assume typex values require "e" memory cells, and treat e, m, n, and p as positive integer constants in your formulas.

ANSWER:

1) Addr(A[I][J][K] = Addr(A[0][0][0]) + I*n*p*e + J*p*e + K*e

2) Addr(B[I][J]) = Addr(B[0][0]) + J*m*e + I*e

Explanation / Answer

1. It is a 3D matrix whose first index represents a 2D matrix of size n X p. First index of this 2D matrix represents an array of size p
Accessing formula for address of A[I][J][K] = A + e*I*n*p + e*J*p + e*K
where

A is the base address of the Array.

e is the number of cells occupied by a typex value.

We need to find the address of the element at Kth column of the Jth row of the Ith matrix of size n X p. Start with the base address A. skip I number of n X p matrices to reach the Ith n X p matrix. In this matrix, skip J*p rows to reach the jth row. In this row, skip K columns to reach the kth column. Each number is multiplied by e so that actual numbers of cells are skipped.

2.

Accessing formula for addres of B[I][J] = B + e*m*J + e*I
where

B is the base address of Array B,

e is the number of cells occupied by a typex vlue and a column-major representation is being used.

Since this is column major representation, m*J columns are skipped first so as to reach the Jth column, then the I rows are skipped to reach the Ith row. Again each term is multiplied by e so that actual number of cells are skipped.