Arrays & Strings

1D Arrays & 2D Matrices Explained

Declare, initialize, index 0-based arrays, iterate over elements, and compute sum & average.

1. The Hook Imagine a apartment building with 5 numbered mailboxes in a row on the ground floor: Box 0, Box 1, Box 2, Box 3, Box 4.

Instead of 5 separate mailboxes scattered across the city, they are all built right next to each other in one contiguous block.

In C programming, an Array is a row of contiguous memory boxes holding items of the SAME data type!


2. Array Key Rules 1. 0-Based Indexing: First element is arr[0], last is arr[size - 1]. 2. Contiguous RAM: Array items live side-by-side in RAM.

#include <stdio.h>

int main() { int arr[5] = {10, 20, 30, 40, 50}; int sum = 0;

for (int i = 0; i < 5; i++) { sum += arr[i]; }

printf("Sum = %d\n", sum); printf("Average = %.2f\n", (float)sum / 5); return 0; } ```


3. Summary Today you learned how to group multiple elements together in 1D and 2D arrays!

C Runner