Data Structures: Derivation of Index Formula for 1-D, 2-D, and Multi-D Arrays

Course Overview and Learning Objectives

  • Course Information:

    • Course Name: Data Structures

    • Course Code: R1UC302B

    • Session Number: 5

    • Instructor: Mriganka Shekhar Das

    • Class Duration: 50 mins

    • Date of Conduction: 16 Aug 2026

    • Instructional Framework: GSCALE (Galgotias Student Centered Active Learning Ecosystem), which incorporates Critical Thinking, Collaboration, Learning Engagement, and Experiential Learning.

  • Session Learning Outcomes:

    • Calculate the memory address of any given element in 1D, 2D, and 3D arrays under various storage orders using formal address formulas.

    • Explain the step-by-step derivation process of index formulas for 1-D, 2-D, and multi-D arrays in both row-major and column-major order.

Array Classification and Storage Orders

  • Array Types Based on Size:

    • Fixed Size Array: Memory size is predetermined during declaration and cannot be resized during runtime.

    • Dynamic Size Array: Memory size can grow or shrink dynamically during program execution.

  • Array Types Based on Dimensions:

    • One-Dimensional (1D) Array: A linear sequence of elements stored in contiguous memory locations.

    • Multi-Dimensional Array: Higher-dimensional structures, including Two-Dimensional (2D) Arrays, Three-Dimensional (3D) Arrays, and higher dimensions.

  • Two-Dimensional Storage Orders:

    • Row-Major Order: Matrix elements are stored in memory row by row sequentially. For a matrix containing elements a11,a12,a13,a21,a22,a23,a31,a32,a33a_{11}, a_{12}, a_{13}, a_{21}, a_{22}, a_{23}, a_{31}, a_{32}, a_{33}, the row-major memory sequence is:

    a11,a12,a13,a21,a22,a23,a31,a32,a33a_{11}, a_{12}, a_{13}, a_{21}, a_{22}, a_{23}, a_{31}, a_{32}, a_{33}

  • Column-Major Order: Matrix elements are stored in memory column by column sequentially. For the same matrix, the column-major memory sequence is:

    a11,a21,a31,a12,a22,a32,a13,a23,a33a_{11}, a_{21}, a_{31}, a_{12}, a_{22}, a_{32}, a_{13}, a_{23}, a_{33}

Warm-Up Problem: Theater Ticket Sales Calculation

  • Problem Scenario:

    • A theater auditorium contains 20 rows, with 30 seats in each row.

    • Each ticket costs ₹150\text{₹}150.

    • Tickets are sold seat by seat in row-major order (from left to right in a row, then proceeding to the first seat of the next row).

    • The last ticket sold was for Row 7, Seat 12.

  • Step-by-Step Calculation:

    • Fully sold rows: Row 1 through Row 6 = 6 rows6\,rows.

    • Total seats sold in full rows = 6×30=180 seats6 \times 30 = 180\,seats.

    • Seats sold in Row 7 = Seats 1 through 12 = 12 seats12\,seats.

    • Total tickets sold = 180+12=192 tickets180 + 12 = 192\,tickets.

    • Total money collected = 192×150=28800 rupees192 \times 150 = 28800\,rupees (₹28800\text{₹}28800).

Address Calculation in One-Dimensional (1D) Arrays

  • Parameters and Definitions:

    • Base Address (BB): The actual memory address of the first element in the array.

    • Element Width (WW): The memory space acquired by each individual element in bytes.

    • Lower Bound (LBLB): The starting index or lower limit of the subscript (assumed to be 00 if not explicitly provided).

    • Index (Index\text{Index}): The specific subscript location of the element whose address is being calculated.

  • Address Calculation Formula:

  Address of A[Index]=B+W×(Index−LB)\text{Address of } A[\text{Index}] = B + W \times (\text{Index} - LB)

  • Concrete Memory Example:

    • Given Base Address B=1100B = 1100.

    • Given Element Width W=4 bytesW = 4\,bytes.

    • Given Lower Bound LB=0LB = 0.

    • Array Elements: [15,7,11,44,93,20][15, 7, 11, 44, 93, 20] stored at indices 0,1,2,3,4,50, 1, 2, 3, 4, 5.

    • Memory addresses calculated for each index:

    • Address of A[0]=1100+4×(0−0)=1100A[0] = 1100 + 4 \times (0 - 0) = 1100

    • Address of A[1]=1100+4×(1−0)=1104A[1] = 1100 + 4 \times (1 - 0) = 1104

    • Address of A[2]=1100+4×(2−0)=1108A[2] = 1100 + 4 \times (2 - 0) = 1108

    • Address of A[3]=1100+4×(3−0)=1112A[3] = 1100 + 4 \times (3 - 0) = 1112

    • Address of A[4]=1100+4×(4−0)=1116A[4] = 1100 + 4 \times (4 - 0) = 1116

    • Address of A[5]=1100+4×(5−0)=1120A[5] = 1100 + 4 \times (5 - 0) = 1120

Address Calculation in Two-Dimensional (2D) Arrays

  • Row-Major Order Calculation:

    • Formula:

    Address of A[I][J]=B+W×(M×(I−LR)+(J−LC))\text{Address of } A[I][J] = B + W \times (M \times (I - LR) + (J - LC))

  • Parameter Definitions:

    • II: Row index of the target element.

    • JJ: Column index of the target element.

    • BB: Base address of the array.

    • WW: Memory size of one array element in bytes.

    • LRLR: Lower limit of row index / starting row index (default is 00 if omitted).

    • LCLC: Lower limit of column index / starting column index (default is 00 if omitted).

    • MM: Total number of columns in the matrix.

    • Column-Major Order Calculation:

  • Formula:

    Address of A[I][J]=B+W×((I−LR)+N×(J−LC))\text{Address of } A[I][J] = B + W \times ((I - LR) + N \times (J - LC))

  • Parameter Definitions:

    • NN: Total number of rows in the matrix.

    • All other parameters (II, JJ, BB, WW, LRLR, LCLC) carry identical definitions to the Row-Major Order formula.

Address Calculation in Multi-Dimensional (3D) Arrays

  • Row-Major Order Formula for 3D Array A[I][J][K]A[I][J][K]:

  Address of A[I][J][K]=B+W×(N×L×(I−x)+L×(J−y)+(K−z))\text{Address of } A[I][J][K] = B + W \times (N \times L \times (I - x) + L \times (J - y) + (K - z))

  • Column-Major Order Formula for 3D Array A[I][J][K]A[I][J][K]:

  Address of A[I][J][K]=B+W×(N×L×(I−x)+(J−y)+(K−z)×N)\text{Address of } A[I][J][K] = B + W \times (N \times L \times (I - x) + (J - y) + (K - z) \times N)

  • Parameter Definitions for 3D Array Formulas:

    • BB: Base Address (start address of the array).

    • WW: Weight / storage size of one element in bytes.

    • NN: Height / Layer (total number of cells depth-wise).

    • MM: Total number of rows.

    • LL: Total number of columns.

    • xx: Lower Bound of Row index (LRLR).

    • yy: Lower Bound of Column index (LCLC).

    • zz: Lower Bound of Height/Depth index (LHLH).

Peer Learning Activities and Problem Solutions

  • Peer Learning Activity Design:

    • Group Structure: 6 members split into 3 pairs.

    • Task Distribution:

    • Pair 1: 1D Array Problem

    • Pair 2: 2D Array Row-Major Order Problem

    • Pair 3: 2D Array Column-Major Order Problem

    • Timing Protocol: Total 5 minutes (2 minutes individual problem-solving, followed by 3 minutes pair discussion and consensus).

    • Comparison Phase: Pairs present solutions to the entire group and compare formula structures across array types.

  • Problem 1 (1D Array Address Calculation):

    • Problem Statement: An array A[0…9]A[0 \dots 9] is stored in memory with base address 10001000. Each element takes 4 bytes4\,bytes. Find the address of A[6]A[6].

    • Given Data: B=1000B = 1000, W=4 bytesW = 4\,bytes, LB=0LB = 0, Index=6\text{Index} = 6

    • Derivation:

    Address(A[6])=B+W×(Index−LB)\text{Address}(A[6]) = B + W \times (\text{Index} - LB)

    Address(A[6])=1000+4×(6−0)\text{Address}(A[6]) = 1000 + 4 \times (6 - 0)

    Address(A[6])=1000+24=1024\text{Address}(A[6]) = 1000 + 24 = 1024

  • Problem 2 (2D Array Row-Major Order Calculation):

    • Problem Statement: A matrix B[0…3][0…4]B[0 \dots 3][0 \dots 4] is stored in row-major order. Base address = 20002000, element size = 2 bytes2\,bytes. Find the address of B[2][3]B[2][3].

    • Given Data: B=2000B = 2000, W=2 bytesW = 2\,bytes, LR=0LR = 0, LC=0LC = 0, I=2I = 2, J=3J = 3

    • Matrix Dimensions: Rows 0…3  ⟹  N=40 \dots 3 \implies N = 4, Columns 0…4  ⟹  M=50 \dots 4 \implies M = 5

    • Derivation:

    Address(B[2][3])=B+W×(M×(I−LR)+(J−LC))\text{Address}(B[2][3]) = B + W \times (M \times (I - LR) + (J - LC))

    Address(B[2][3])=2000+2×(5×(2−0)+(3−0))\text{Address}(B[2][3]) = 2000 + 2 \times (5 \times (2 - 0) + (3 - 0))

    Address(B[2][3])=2000+2×(5×2+3)\text{Address}(B[2][3]) = 2000 + 2 \times (5 \times 2 + 3)

    Address(B[2][3])=2000+2×(10+3)=2000+26=2026\text{Address}(B[2][3]) = 2000 + 2 \times (10 + 3) = 2000 + 26 = 2026

  • Problem 3 (2D Array Column-Major Order Calculation):

    • Problem Statement: A matrix C[1…4][1…5]C[1 \dots 4][1 \dots 5] is stored in column-major order. Base address = 50005000, element size = 2 bytes2\,bytes. Find the address of C[3][4]C[3][4].

    • Given Data: B=5000B = 5000, W=2 bytesW = 2\,bytes, LR=1LR = 1, LC=1LC = 1, I=3I = 3, J=4J = 4

    • Matrix Dimensions: Rows 1…4  ⟹  N=4−1+1=41 \dots 4 \implies N = 4 - 1 + 1 = 4, Columns 1…5  ⟹  M=5−1+1=51 \dots 5 \implies M = 5 - 1 + 1 = 5

    • Derivation:

    Address(C[3][4])=B+W×((I−LR)+N×(J−LC))\text{Address}(C[3][4]) = B + W \times ((I - LR) + N \times (J - LC))

    Address(C[3][4])=5000+2×((3−1)+4×(4−1))\text{Address}(C[3][4]) = 5000 + 2 \times ((3 - 1) + 4 \times (4 - 1))

    Address(C[3][4])=5000+2×(2+4×3)\text{Address}(C[3][4]) = 5000 + 2 \times (2 + 4 \times 3)

    Address(C[3][4])=5000+2×(2+12)=5000+28=5028\text{Address}(C[3][4]) = 5000 + 2 \times (2 + 12) = 5000 + 28 = 5028

Summary of Array Address Formulas

  • 1D Array:

    • Formula: Addr(A[Index])=B+W×(Index−LB)\text{Addr}(A[\text{Index}]) = B + W \times (\text{Index} - LB)

    • Parameters: BB = Base address, WW = Size of one element (bytes), LBLB = Lower bound of index.

  • 2D Array (Row-Major Order):

    • Formula: Addr(A[I][J])=B+W×(M×(I−LR)+(J−LC))\text{Addr}(A[I][J]) = B + W \times (M \times (I - LR) + (J - LC))

    • Parameters: BB = Base address, WW = Element size, LRLR = Row lower bound, LCLC = Column lower bound, MM = Number of columns.

  • 2D Array (Column-Major Order):

    • Formula: Addr(A[I][J])=B+W×((I−LR)+N×(J−LC))\text{Addr}(A[I][J]) = B + W \times ((I - LR) + N \times (J - LC))

    • Parameters: NN = Number of rows, all other symbols identical to Row-Major Order.

  • 3D Array (Row-Major Order):

    • Formula: Addr(A[I][J][K])=B+W×(N×L×(I−x)+L×(J−y)+(K−z))\text{Addr}(A[I][J][K]) = B + W \times (N \times L \times (I - x) + L \times (J - y) + (K - z))

    • Parameters: BB = Base address, WW = Element size, NN = Depth/Height, MM = Rows, LL = Columns, x,y,zx, y, z = Lower bounds for row, column, and depth.

  • 3D Array (Column-Major Order):

    • Formula: Addr(A[I][J][K])=B+W×(N×L×(I−x)+(J−y)+(K−z)×N)\text{Addr}(A[I][J][K]) = B + W \times (N \times L \times (I - x) + (J - y) + (K - z) \times N)

    • Parameters: Identical parameter definitions to 3D Row-Major Order.

Assessment and Next Session Preview

  • Learning Outcome Verification:

    • Confirmation of formula application for memory addresses across 1D, 2D, and 3D arrays.

    • Mastery of formula derivation steps for Row-Major and Column-Major memory layouts.

  • Interactive Assessment Tools and Review:

    • Pre-Assessment Lecture 05 - DS

    • Post-Assessment Lecture 05 - DS

    • Interactive reinforcement tools: WooClap and WooFlash.

  • Next Session Topics:

    • Practical Applications of Arrays

    • Sparse Matrices

    • Memory Representations of Sparse Matrices