NBC 103 • Unit 316 min readHigh Exam Frequency

Single-Dimensional Arrays (1D) & Core Algorithms

Unit 3: Arrays, Pointers & String ManipulationProblem Solving Using C

👨‍🏫 Professor's Mental Model: Numbered Lockers in a Gymnasium Corridor

1D Array ek lambe corridor me lagi numbered lockers ki line jaisa hai! Har locker ka size bilkul barabar hai (Homogeneous - sabme sirf integer ya float hi aayega), aur saare lockers ek ke baad ek chipke hue hain (Contiguous Memory). Agar pehle locker ka number pata hai, toh 5ve locker tak pahunchne ke liye seedha formula lagao, beech ke lockers kholne ki zaroorat nahi padti (Direct Random Access)! Lekin C me koi security guard nahi hai jo roke agar aap aakhri locker ke paar chale jao (No Bounds Checking)!

Academic Lecture Notes & Solved Study Pages

Unit 3 • Core Concepts, Step-by-Step Proofs & Notebook Solutions

3 Notebook Pages
NOTEBOOK PAGE 1 OF 3

1. Definition, Declaration & Contiguous Memory Allocation

Definition: An array is a fixed-size, sequenced collection of homogeneous (identical data type) elements stored in contiguous (adjacent) memory locations.
Declaration Syntax: `data_type array_name[array_size];` (e.g. `int marks[5];`).
Compile-Time Initialization:
- Complete: `int arr[5] = {10, 20, 30, 40, 50};`
- Partial: `int arr[5] = {10, 20};` (Remaining elements automatically initialized to 0!)
- Implicit Sizing: `int arr[] = {1, 2, 3};` (Compiler automatically allocates 3 elements).
NOTEBOOK PAGE 2 OF 3

2. Address Calculation Formula & Zero-Based Indexing

Why does C use 0-based indexing? The index represents the OFFSET (distance) from the array's starting memory cell. The first element has an offset of 0 bytes from the base address!

Mathematical Address Formula for 1D Array:

$\text{Address of } arr[i] = \text{Base Address} + i \times \text{sizeof(element)}$

Example:

If `int arr[5]` has Base Address = 1000 and `sizeof(int)` = 4 bytes:

Address of arr[0] = 1000 + 0 * 4 = 1000
Address of arr[3] = 1000 + 3 * 4 = 1012
Lack of Bounds Checking:

C does NOT verify whether index `i < size`. Accessing `arr[10]` in an array of size 5 compiles without error, but reads/writes into random RAM, causing silent data corruption or a runtime Segmentation Fault!

NOTEBOOK PAGE 3 OF 3

3. Core Fundamental Array Algorithms

Traversal: Visiting and processing every element from index 0 to $N-1$ in $O(N)$ time.
Insertion at Index k: Must shift existing elements from index $N-1$ down to $k$ one position to the right to make room, then insert the new item. Time: $O(N)$.
Deletion from Index k: Must shift subsequent elements from index $k+1$ up to $N-1$ one position to the left, decreasing array size. Time: $O(N)$.
1D Array Contiguous Memory Map: int arr[5] = {12, 45, 78, 23, 90}
Array IndexStored ValueMemory Address (Hex)Offset from BaseAddress Calculation Formula
arr[0]120x20000 Bytes0x2000 + (0 × 4) = 0x2000
arr[1]450x2004+4 Bytes0x2000 + (1 × 4) = 0x2004
arr[2]780x2008+8 Bytes0x2000 + (2 × 4) = 0x2008
arr[3]230x200C+12 Bytes0x2000 + (3 × 4) = 0x200C
arr[4]900x2010+16 Bytes0x2000 + (4 × 4) = 0x2010

Interactive Tested Code Example

#include <stdio.h>

int main(void) {
    int arr[10] = {10, 20, 30, 40, 50};
    int size = 5;
    int insert_val = 99, insert_pos = 2;

    printf("Initial Array (%d elements): ", size);
    for (int i = 0; i < size; i++) printf("%d ", arr[i]);
    printf("\n");

    // 1. Insertion Algorithm at index 'insert_pos'
    for (int i = size - 1; i >= insert_pos; i--) {
        arr[i + 1] = arr[i]; // Shift right
    }
    arr[insert_pos] = insert_val;
    size++;

    printf("After Inserting %d at index %d: ", insert_val, insert_pos);
    for (int i = 0; i < size; i++) printf("%d ", arr[i]);
    printf("\n");

    // 2. Deletion Algorithm at index 'insert_pos'
    for (int i = insert_pos; i < size - 1; i++) {
        arr[i] = arr[i + 1]; // Shift left
    }
    size--;

    printf("After Deleting index %d: ", insert_pos);
    for (int i = 0; i < size; i++) printf("%d ", arr[i]);
    printf("\n");
    return 0;
}
💡 Note:Demonstrates 1D array manipulation: shifting elements right for in-place insertion and shifting elements left for deletion.

🎯 University Exam Scoring Blueprint

  • Always write down the 1D address formula: Address(arr[i]) = Base + i * Size in university exam answers.
  • Highlight that the array name 'arr' by itself represents the base address (&arr[0]).
  • State clearly that partially initialized arrays zero-fill the remaining elements.

Top Viva Questions on Single-Dimensional Arrays (1D) & Core Algorithms

2 Questions
1

Why does C not check array bounds during runtime?

2

What is the difference between int arr[5] and arr[5]?