3D Array Address Formula
i will give you questions of my assignment from DSA, you have to write the answer properly answer
i am a phd student so be carefull with answer the questions are really important
Q1, What shall be address of an element A[p,q,r] of 3D array stored in dimension order.
A[0,0,0], A[0,0,1], A[0,0,2] … A[0,0,r-1],
A[0,1,0], A[0,1,1], A[0,1,2] … A[0,1,r-1],
…
A[0,q-1,0], A[0,q-1,1], A[0,q-1,2] … A[0,q-1,r-1],
A[1,0,0], A[0,0,1], A[0,0,2] … A[0,0,r-1],
A[1,1,0], A[0,1,1], A[0,1,2] … A[0,1,r-1],
…
A[1,q-1,0], A[0,q-1,1], A[0,q-1,2] … A[0,q-1,r-1],
A[2,0,0], A[2,0,1], A[2,0,2] … A[2,0,r-1],
A[2,1,0], A[2,1,1], A[2,1,2] … A[2,1,r-1],
…
A[2,q-1,0], A[2,q-1,1], A[2,q-1,2] … A[2,q-1,r-1],
….
….
A[p-1,0,0], A[p-1,0,1], A[p-1,0,2] … A[p-1,0,r-1],
A[p-1,1,0], A[p-1,1,1], A[p-1,1,2] … A[p-1,1,r-1],
…
A[p-1,q-1,0], A[p-1,q-1,1], A[p-1,q-1,2] … A[p-1,q-1,r-1],
Solution Explanation
We have a 3D array A with dimensions [0..p − 1], [0..q − 1], [0..r − 1]. The array is stored in row-
major (dimension) order, meaning the last dimension varies the fastest, then the second, and finally
the first dimension varies the slowest.
Concretely, the sequence of stored elements given in the question is:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/5
A[0, 0, 0], A[0, 0, 1], … , A[0, 0, r − 1],
A[0, 1, 0], A[0, 1, 1], … , A[0, 1, r − 1],
…
A[0, q − 1, 0], A[0, q − 1, 1], … , A[0, q − 1, r − 1],
A[1, 0, 0], A[1, 0, 1], … , A[1, 0, r − 1],
A[1, 1, 0], A[1, 1, 1], … , A[1, 1, r − 1],
…
A[1, q − 1, 0], A[1, q − 1, 1], … , A[1, q − 1, r − 1],
A[2, 0, 0], … , A[2, q − 1, r − 1],
…
A[p − 1, q − 1, r − 1].
Here is how the linear (one-dimensional) index of an element A[i, j, k] (where 0 ≤ i < p, 0 ≤ j <
q, 0 ≤ k < r) is computed in row-major storage:
1. The last dimension (k ) varies first,
2. then the second dimension (j ),
3. and the first dimension (i) varies last.
Hence, the one-dimensional offset Index(i, j, k) from the start (A[0, 0, 0]) is:
Index(i, j, k) = i × (q × r) + j × (r) + k.
If each array element takes w bytes of memory, and if we let
BaseAddress = Address(A[0, 0, 0])
be the address of the very first element in memory, then the address of the element A[i, j, k] is given
by:
Address(A[i, j, k]) = BaseAddress + [ i ⋅ (q ⋅ r) + j ⋅ r + k] × w.
This formula directly reflects that we are using row-major storage with the ordering:
A[0, 0, 0], A[0, 0, 1], … , A[0, 0, r − 1], A[0, 1, 0], A[0, 1, 1], …
Final Answer
Address(A[i, j, k]) = BaseAddress + (i × q × r + j × r + k) × w
where:
0 ≤ i < p, 0 ≤ j < q , 0 ≤ k < r .
w = size (in bytes) of each element.
BaseAddress = Address(A[0, 0, 0]).
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/5
Q2 Generalize this to an N-dimensional array of size d1 x d2 x d3x … x dN. What shall be address of
element [r1, r2, r3, …, rN]
Solution Explanation
For an N -dimensional array A of size d1× d2 × d3 × ⋯ × dN (with each index starting at 0), stored in
row-major order, the last dimension (dN ) varies the fastest, followed by dN −1 , and so on, up to d1 which
varies the slowest.
Let:
BaseAddress = address of the first element A[0, 0, … , 0].
w = size (in bytes) of each element.
Valid indices: 0 ≤ rk < dk for k = 1, 2, … , N .
Then the address of an element A[r1 , r2 , … , rN ] in row-major order is given by:
Address(A[r1 , r2 , … , rN ]) = BaseAddress + [r1 (d2 × d3 × ⋯ × dN ) + r2 (d3 × d4 × ⋯ ×
This formula generalizes the concept from 1D, 2D, or 3D arrays to any N -dimensional array.
Assignment: Complete the table
Below is a commonly‐used summary of time‐complexities for the basic operations on singly and doubly
linked lists. The best‐case times typically assume you already have a direct pointer to the node of
interest (or you are operating at the head/tail which is readily accessible). The worst‐case times assume
you must first traverse the list to locate the position/node in question.
Operation Singly‐Linked List Singly‐Linked List Doubly‐Linked List Doubly‐Linked List
Best Case Worst Case Best Case Worst Case
Insertion O(1) O(n) O(1) O(n)
Deletion O(1) O(n) O(1) O(n)
Updation O(1) O(n) O(1) O(n)
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/5
Operation Singly‐Linked List Singly‐Linked List Doubly‐Linked List Doubly‐Linked List
Search O(1) O(n) O(1) O(n)
Best‐case O(1) generally occurs when you already know exactly which node to operate
on (e.g., you have a pointer/reference to it), or if you operate at the head/tail (which is
stored and accessible in constant time).
Worst‐case O(n) arises whenever you must traverse the list to find the node or
position first.
Assignment: Complete the table
Question: Is stack FILO (first in last out) structure too? If not, provide a counterexample.
Below is a commonly‐cited summary of the time complexities (best‐ and worst‐case) for the four listed
operations on stacks and queues. In both cases we assume a standard (fixed‐ or linked‐)
implementation, where we can enqueue at the back of the queue and dequeue from the front in O(1).
Likewise, pushes and pops on a stack take O(1). Searching or “updating” an arbitrary element, however,
typically requires traversing the entire structure, hence O(n) in the worst case.
Stack <br> Best Stack <br> Worst Queue <br> Best Queue <br> Worst
Operation
Case Case Case Case
Insertion O(1) O(1) O(1) O(1)
Deletion O(1) O(1) O(1) O(1)
O(1) O(n) O(1) O(n)
Updation
(if top/front) (if must traverse) (if front & known) (if must traverse)
O(1) O(1)
Search O(n) O(n)
(if top/front) (if front)
Is a Stack Also a “FILO” Structure?
A stack is most commonly described as a LIFO (Last In, First Out) structure. However, the term FILO
(First In, Last Out) is simply the same principle stated from the other end:
LIFO: “The item you push last will be popped first.”
FILO: “The item you pushed first will be popped last (if you pop everything).”
These two descriptions are equivalent views of exactly the same behavior. So yes, a standard stack can
correctly be called FILO as well as LIFO.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/5
There is no “counterexample” to show that a proper stack would not be FILO, because LIFO = FILO in this
context. If, on the other hand, you wanted a data structure that is not FILO, a simple example is the
queue (which is FIFO, or First In, First Out).
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 5/5