C
1-D Arrays & Applications in C
🏠 Home Lecture 13: 1-D Arrays
Fundamental Data Structures

1-D Arrays in C

An array is a fixed-size, contiguous sequence of memory locations that stores multiple items of the exact same data type under a single name.

To store marks of 60 students in a class, what is the best approach?
array_intro.cDeclaration & Access
#include <stdio.h> int main(void) { // 1. Declaration with direct initialization int marks[5] = {85, 92, 78, 90, 88}; // 2. Direct Index Access in O(1) time printf("First student: %d\n", marks[0]); // 85 printf("Last student: %d\n", marks[4]); // 88 return 0; }
1

Contiguous Memory

Stored back-to-back in consecutive RAM memory addresses.

2

0-Based Indexing

Indices run from 0 to N - 1 (offset from start).

3

Instant Access O(1)

Address = Base + (i × sizeof(type)) calculated in hardware.

4

Homogeneous Type

Every element must strictly share the identical data type.

Hardware Architecture

Array Memory Layout & Address Calculation

In C, array name is a constant pointer to the base address. Explore how byte offsets are calculated based on data types!

Interactive Address Calculator

Formula: Address(arr[i]) = Base + (i × sizeof(type))
Address(arr[2]) = 2000 + (2 × 4) = 2008
Calculated Memory Map:

Contiguous RAM Cells Visualization

🎯 Why 0-indexed? Index i denotes the exact number of element blocks to skip from the base address! arr[0] skips 0 × sizeof bytes.
Standard Iteration

Array Traversal with for Loop

Step through visiting every element in the array sequentially to read or display its value.

Interactive Traversal Tracer

Active Cell Highlight:
Loop Counter: i = 0 | Action: Ready
Standard Output (printf):
Click "Next Step ▶" to start iteration...
array_traversal.cActive Line Tracer
int arr[5] = {15, 42, 8, 99, 63}; for (int i = 0; i < 5; i++) { printf("arr[%d] = %d\n", i, arr[i]); } printf("Traversal Complete!\n");
Algorithm 1

Finding Maximum & Minimum Element

Initialize max = arr[0] and min = arr[0], then compare every subsequent element i = 1 to N-1.

Interactive Min/Max Scanner

Visual Scan (🟢 MAX | 🔴 MIN | 🔵 Current i):
Index i = 0 | Val: 34 | MAX: 34 | MIN: 34
Comparison Log:
Ready. Click "Next Step ▶".
min_max.cLogic Tracer
int max = arr[0], min = arr[0]; for (int i = 1; i < n; i++) { if (arr[i] > max) { max = arr[i]; } if (arr[i] < min) { min = arr[i]; } }
Algorithm 2 (Two-Pointer Technique)

In-Place Array Reversal

Reverse an array in-place with O(N / 2) swaps by having two pointers converge from the ends to the center.

Two-Pointer Swap Simulator

Two-Pointers (Left 🔵 vs Right 🔴):
Left: left = 0 | Right: right = 5 | Status: Ready
Swap Log:
reverse_array.cTwo-Pointer Swap
int left = 0, right = n - 1; while (left < right) { int temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; left++; right--; }
Searching Algorithm

Linear Search (O(N))

Sequential search checks each element one by one from index 0 until the target key is found or the array ends.

Interactive Search Scanner

Visual Scan (🟡 Checking | 🟢 Found):
Checking: arr[0] == 12 | Status: Ready
Search Log:
Click "Next Step ▶" to begin linear search.
linear_search.cActive Search Line
int foundIndex = -1; for (int i = 0; i < n; i++) { if (arr[i] == target) { foundIndex = i; break; // Found! } }
Sorting Algorithm (O(N²))

Bubble Sort Visualizer

Repeatedly compares adjacent pairs, and swaps them if out of order. Largest element "bubbles" to end each pass!

Interactive Bubble Sorter

Array Visual State:
Pass i = 0 | Pair: j = 0 vs j+1 = 1 | Ready
Sort Step Log:
Click "Next Step ▶" to start sorting.
bubble_sort.cPass & Swap Tracer
for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { int tmp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = tmp; } } }
Critical Debugging

Common Array Pitfalls & Bugs

C provides no automatic array bounds checking for performance reasons. Understand how to avoid critical security and memory bugs.

1. Out-of-Bounds (Buffer Overflow)

Accessing arr[5] in int arr[5]; reads/writes adjacent memory causing random bugs or crashes!

int arr[5]; arr[5] = 100; // ⚠️ BUG: Valid indices are only 0..4

2. Uninitialized Garbage Values

Local arrays are NOT initialized to zero automatically. They contain unpredictable memory junk.

int arr[5]; // Garbage values! int arr[5] = {0}; // ✅ Safe: All 0

3. Array Decay to Pointer

When passed to functions, arrays decay to pointers. sizeof(arr) inside a function returns pointer size (8B/4B)!

void printArray(int arr[], int size); // Always pass size!

4. Off-By-One in Loop Limits

Using i <= N instead of i < N will cause the loop to run N + 1 times, accessing past the array boundary.

for (int i = 0; i < N; i++) // ✅ Correct
Question 1 of 5
Loading quiz...