Single-Dimensional Arrays (1D) & Core Algorithms
Unit 3: Arrays, Pointers & String Manipulation • Problem 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
1. Definition, Declaration & Contiguous Memory Allocation
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!
$\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:
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!
3. Core Fundamental Array Algorithms
| Array Index | Stored Value | Memory Address (Hex) | Offset from Base | Address Calculation Formula |
|---|---|---|---|---|
| arr[0] | 12 | 0x2000 | 0 Bytes | 0x2000 + (0 × 4) = 0x2000 |
| arr[1] | 45 | 0x2004 | +4 Bytes | 0x2000 + (1 × 4) = 0x2004 |
| arr[2] | 78 | 0x2008 | +8 Bytes | 0x2000 + (2 × 4) = 0x2008 |
| arr[3] | 23 | 0x200C | +12 Bytes | 0x2000 + (3 × 4) = 0x200C |
| arr[4] | 90 | 0x2010 | +16 Bytes | 0x2000 + (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;
}🎯 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.