ARRAYS

nit Scope and Learning Outcomes

Scope and Outline

The study of arrays encompasses two primary structural divisions:

  • Single Dimensional Arrays (1-D Arrays)

  • Multi-Dimensional Arrays (e.g., 2-Dimensional and 3-Dimensional Arrays)

Learning Outcomes

Upon comprehensive mastery of this instructional unit, students will be able to:

  • Construct and analyze programs involving multidimensional data structures.

  • Execute the creation, manipulation, transformation, and traversal of data organized within matrices.


Fundamentals of Arrays

Definitions and Core Characteristics

An array is a fundamental linear data structure governed by three essential architectural properties:

  1. Fixed Length:

    • The storage capacity is strictly established during initial allocation and remains static throughout its lifecycle.

  2. Homogeneous Data:

    • Strictly stores elements sharing the identical data type:

      • Integer Array: Exclusively stores integer values.

      • Character Array: Strictly stores character values.

      • Floating-Point Array: Exclusively stores real floating-point numbers.

  3. Sequential (Contiguous) Memory Allocation:

    • Elements are placed in uninterrupted, sequential physical byte addresses in RAM (Random Access Memory).

Essential Terminology

  • Element: Each individual data item residing at a distinct cell in the array.

  • Index (Subscript): A unique zero-based numerical identifier designating the exact coordinate of an element for direct random access.


Classification tree of arrays into 1-dimensional and multi-dimensional arrays

Structure and Taxonomy of Arrays

Structural Classification

Arrays are categorized by the number of coordinate subscripts needed to retrieve an element:

  • 1-Dimensional (Single Dimensional) Arrays: Linear layout referencing elements using one coordinate subscript: arr[i]arr[i].

  • Multi-Dimensional Arrays: Higher-order arrangements requiring multiple subscripts:

    • 2-Dimensional (2-D) Arrays: Grid-like matrix format consisting of rows and columns: matrix[i][j]matrix[i][j].

    • 3-Dimensional (3-D) Arrays: Multi-layered volumetric structures indexed across depth, rows, and columns: arr[i][j][k]arr[i][j][k].

Visual Anatomy of an Array


Diagram illustrating array structure, indices from 0 to 5, elements, and access notation

Consider an array named arr containing 66 integer elements:

Index (ii)

Access Notation

Stored Element

Position Identity

00

arr[0]

66

Element 11

11

arr[1]

99

Element 22

22

arr[2]

1212

Element 33

33

arr[3]

33

Element 44

44

arr[4]

2727

Element 55

55

arr[5]

4040

Element 66

Core Indexing Rules:

  • Zero-Based Indexing: The first item is consistently situated at index 00.

  • Boundary Bounds: For total capacity NN, legal subscripts span from 00 to N−1N−1.

  • Direct Subscripting: Any element can be directly accessed via array_name[index] in O(1)O(1) time.


One-Dimensional (1-D) Arrays

Definition and Operational Mechanics

A one-dimensional array is a linear collection where:

  • Elements are arranged sequentially along a single dimension.

  • Storage occurs in contiguous memory addresses in RAM.

  • Only a single subscript is required to access any item.

Syntax for Declaration in C

Data_type array_name[size_of_array];
  • Data_type: Defines the type of data stored (e.g., int, float, char).

  • array_name: The identifier variable representing the array.

  • size_of_array: An integer constant indicating total allocated slots.

Concrete Declaration Example

int arr[10] = { 6, 9, 12, 3, 27, 40, 2, 8, 51, 44 };

Index and Element Mapping

Index

00

11

22

33

44

55

66

77

88

99

Element

66

99

1212

33

2727

4040

22

88

5151

4444

  • Capacity N=10N=10, so valid index boundaries range from 00 through 99.

  • Accessing arr[4] directly references value 2727.


Memory Architecture: Base Address and Element Width

Memory Organization of Arrays

Locating an element in physical RAM relies on two essential specifications:

  1. Base Address: The physical memory byte address of the first item (arr[0]).

  2. Width (WW): The physical memory size (in bytes) occupied by each individual element based on its data type.

Element Location Calculation Formula

Address of arr[i]=Base Address+(i×W)Address of arr[i]=Base Address+(i×W)

Where:

  • Base AddressBase Address is the memory location of arr[0].

  • ii is the target element index (0≤i<N0≤i<N).

  • WW represents the memory width consumed per element in bytes.

Architectural Memory Trace Example

For int arr[10] with a hypothetical Base Address of 100100 and width W=4 bytesW=4bytes:

Index (ii)

Element Value

Address Calculation

Physical Memory Address

00

66

100+(0×4)100+(0×4)

100100

11

99

100+(1×4)100+(1×4)

104104

22

1212

100+(2×4)100+(2×4)

108108

33

33

100+(3×4)100+(3×4)

112112

44

2727

100+(4×4)100+(4×4)

116116

55

4040

100+(5×4)100+(5×4)

120120

66

22

100+(6×4)100+(6×4)

124124

77

88

100+(7×4)100+(7×4)

128128

88

5151

100+(8×4)100+(8×4)

132132

99

4444

100+(9×4)100+(9×4)

136136

Standard C Data Type Memory Sizes

Data Type

Memory Width (WW)

char

1 byte1byte

short int

2 bytes2bytes

int

4 bytes4bytes

long int

8 bytes8bytes

float

4 bytes4bytes

double

8 bytes8bytes


Array Declarations and Initializations

Declaration Semantics Across Diverse Types

  • float num[5];
      Allocates 55 contiguous single-precision floats: 5×4 bytes=20 bytes5×4bytes=20bytes.

  • double salary[5];
      Allocates 55 contiguous double-precision numbers: 5×8 bytes=40 bytes5×8bytes=40bytes.

  • char vowels[5];
      Allocates 55 contiguous character slots: 5×1 byte=5 bytes5×1byte=5bytes.

Initialization with Literal Values

float num[5] = { 2.5, 1.8, 20.5, 3.0, 11.5 };
double salary[5] = { 1200.0, 200.0, 300.5, 700.0, 500.0 };
char vowels[5] = { 'a', 'e', 'i', 'o', 'u' };

Implicit Sizing (Omitting Array Dimension)

In C, if an explicit size is omitted inside the brackets, the compiler automatically deduces the dimension from the initializer list:

int marks[] = { 46, 77, 63, 92, 51, 83, 69, 78, 70, 49 }; // Auto-sized to 10 elements

The Four Primary Initialization Methods

Method 1: Sized Initialization List

int arr[5] = { 1, 2, 5, 67, 32 };

Method 2: Unsized Initialization List

int arr[] = { 1, 2, 5, 67, 32 };

Method 3: Post-Declaration Subscript Assignment

int arr[5];
arr[0] = 1;
arr[1] = 2;
arr[2] = 5;
arr[3] = 67;
arr[4] = 32;

Method 4: Dynamic Programmatic Input via Loops

int arr[5];
int i;
for(i = 0; i < 5; i++) {
    scanf("%d", &arr[i]);
}

Fundamental Array Operations
       ┌────────────┐
       │ Operations │
       └──────┬─────┘
  ┌───────┬───┴───┬────────┬────────┬────────┐
  ▼       ▼       ▼        ▼        ▼        ▼
Traverse Insert  Delete  Update   Search    Sort
  1. Traverse: Systematically access and read or print each element sequentially.

  2. Insertion: Add a new element at a specific index, shifting subsequent items rightward.

  3. Deletion: Remove an existing element from an index, shifting subsequent items leftward.

  4. Update (Updation): Directly overwrite the value stored at a designated index.

  5. Search: Locate the position of an element by key value or subscript coordinate.

  6. Sort: Arrange the elements in ascending or descending order.


Detailed Algorithmic Operations

1. Traverse Operation

Visits each memory cell sequentially from lower bound (00) to upper bound (N−1N−1).

  • Algorithm Mechanics:

    • Initialize index variable i=0i=0.

    • Access arr[i], perform the required action (e.g., display value), and increment i=i+1i=i+1.

    • Stop when i=Ni=N.

  • Walkthrough for arr = [11, 3, 9, 17, 2]:

    • Step 1: Counter i=0i=0 → references arr[0] = 1111

    • Step 2: Counter i=1i=1 → references arr[1] = 33

    • Step 3: Counter i=2i=2 → references arr[2] = 99

    • Step 4: Counter i=3i=3 → references arr[3] = 1717

    • Step 5: Counter i=4i=4 → references arr[4] = 22

    • Termination condition met at i=5i=5.

2. Insertion Operation

Inserts a new value into the array at any defined position. To prevent data loss, items residing at and after the insertion coordinate must shift rightward by one slot.

  • Index Translation:
    Target Index=pos−1Target Index=pos−1

  • Walkthrough: Inserting Value 1010 at Position 22:

    • Initial Array: [11, 3, 9, 17, 2] (Size: 55, Indices: 00 to 44)

    • Target Index: pos−1=2−1=1pos−1=2−1=1

    • Execution Steps:

    1. Set pointer i=4i=4 (last element: value 22).

    2. Shift rightward: arr[5] = arr[4] (22 moves to index 55).

    3. Decrement i=3i=3: arr[4] = arr[3] (1717 moves to index 44).

    4. Decrement i=2i=2: arr[3] = arr[2] (99 moves to index 33).

    5. Decrement i=1i=1: arr[2] = arr[1] (33 moves to index 22).

    6. Insert value: Assign arr[1] = 10.

    • Resulting Array: [11, 10, 3, 9, 17, 2] (Size: 66)

  • Exercise: Inserting Value 3333 at Position 33:

    • Target Index = 3−1=23−1=2.

    • Traverse backward from i=4i=4 to i=2i=2, shifting elements right: arr[i + 1] = arr[i].

    • Assign arr[2] = 33 and increment logical array size by 11.

3. Deletion Operation

Deletes an element from a specified position. To maintain memory contiguity without leaving holes, subsequent elements shift leftward by one slot.

  • Index Translation:
    Target Index=pos−1Target Index=pos−1

  • Walkthrough: Deleting Element at Position 22:

    • Initial Array: [11, 3, 9, 17, 2] (Size: 55, Indices: 00 to 44)

    • Target Index: pos−1=2−1=1pos−1=2−1=1 (holding value 33)

    • Execution Steps:

    1. Set pointer i=1i=1.

    2. Shift leftward: arr[1] = arr[2] (Value 99 overwrites 33 at index 11).

    3. Increment pointer to i=2i=2: arr[2] = arr[3] (Value 1717 replaces 99 at index 22).

    4. Increment pointer to i=3i=3: arr[3] = arr[4] (Value 22 replaces 1717 at index 33).

    5. Decrement logical array size from 55 to 44.

    • Resulting Array: [11, 9, 17, 2] (Size: 44, Indices: 00 to 33)

4. Updation Operation

Replaces an existing element value at a specified index in O(1)O(1) time without requiring memory shifts or resizing.

  • Index Translation:
    Target Index=pos−1Target Index=pos−1

  • Walkthrough: Updating Element at Position 33 (Value 99) with New Value 88:

    • Initial Array: [11, 3, 9, 17, 2]

    • Target Index: 3−1=23−1=2

    • Execution: Direct assignment arr[2] = 8

    • Resulting Array: [11, 3, 8, 17, 2]


Academic References
  1. Behrouz A. Forouzan, Richard F. Gilberg, Data Structures – A Pseudocode Approach Using C, Cengage Learning India.

  2. Deepali Srivastava, Data Structures through C in Depth, BPB Publications.

  3. Tremblay J. P., Sorenson P. G., An Introduction to Data Structures with Applications, Tata McGraw-Hill.