1 / 15

πŸ“¦ Session 11: Arrays

Storing Many Values

Store 100 numbers in one variable? Yes, with arrays!

Marks of 50 students? A list of scores? One name, many boxes.

One array β†’ Many values ✨

Today's Array Mission

πŸ“¦

Declare an array
Type, name, size

πŸ”’

Index (0-based)
First element is 0

πŸ”„

Loops + arrays
Fill, print, sum

πŸ“₯

Read & write
scanf/printf with arr[i]

πŸ“Š

Simple programs
Sum, max, marks

🚨

Common traps
Bounds, off-by-one

πŸ€” Why Arrays? Without vs With

❌ Without array (5 marks)
int m1, m2, m3, m4, m5; scanf("%d", &m1); scanf("%d", &m2); // ... 50 variables for 50 students? 😫

Too many variables!

βœ… With array
int marks[50]; for (i=0; i<50; i++) scanf("%d", &marks[i]);

One name, 50 boxes. πŸŽ‰

Same type β€’ One name β€’ Use a loop

πŸ“¦ Declaring an Array

Type, name, and size in square brackets. Size is fixed.

// Syntax: type name[size]; int marks[5]; // 5 integers float scores[10]; // 10 floats int arr[100]; // 100 integers

πŸ“Œ Fixed size

You choose size when you write the program (e.g. 5, 10, 50).

πŸ“Œ Same type

All elements are the same type (all int or all float).

πŸ”’ Index: Start from 0!

First element is index 0, not 1. Last element is size - 1.

int arr[5];

[0]
10
[1]
20
[2]
30
[3]
40
[4]
50

Valid indices: 0, 1, 2, 3, 4. Never use 5 (out of bounds!).

πŸ“₯ Reading & Writing Array Elements

// Write: arr[index] = value; arr[0] = 10; arr[1] = 20; // Read: use arr[index] in expressions printf("%d", arr[0]); // print first scanf("%d", &arr[2]); // read into third

Use & when reading into arr[i] with scanf: &arr[i]

πŸ”„ Loop: Fill and Print Array

int arr[5], i; // Fill from user for (i = 0; i < 5; i++) { printf("Enter value %d: ", i + 1); scanf("%d", &arr[i]); } // Print all for (i = 0; i < 5; i++) { printf("%d ", arr[i]); }

Always use i < 5 (not i <= 5). Last index is 4.

βž• Sum of All Elements

int arr[5] = {10, 20, 30, 40, 50}; int sum = 0, i; for (i = 0; i < 5; i++) { sum = sum + arr[i]; } printf("Sum = %d\n", sum); // 150

Initializer: int arr[5] = {10, 20, 30, 40, 50};

πŸ“Š Find the Maximum

int arr[5], i, max; // assume first is max max = arr[0]; for (i = 1; i < 5; i++) { if (arr[i] > max) max = arr[i]; } printf("Max = %d\n", max);

Start from index 1; compare each with max.

πŸ“š Marks of 50 Students (as in Session 10!)

int marks[50], i, sum = 0; for (i = 0; i < 50; i++) { printf("Mark for student %d: ", i + 1); scanf("%d", &marks[i]); sum = sum + marks[i]; } printf("Average = %f\n", sum / 50.0);

One array, one loop: store and add. Then average!

🚨 Array Traps

❌ Out of bounds

arr[5] when size is 5 β€” valid indices are 0 to 4 only!

❌ Wrong loop condition

i <= 5 for arr[5] β€” last index is 4. Use i < 5.

❌ Forgetting & in scanf

scanf("%d", arr[i]); β€” must be &arr[i] for integers.

❌ Index 1 to N

Use 0 to N-1. First element is arr[0], not arr[1].

🧠 Array Quiz!

In int arr[10], valid indices are 0 to 9.

First element of an array is at index 1.

We use a loop to fill or print many elements easily.

arr[10] for int arr[10] is valid (last element).

πŸ† Array Mastery Unlocked!

You've earned these badges:

πŸ“¦ Array Declarer
πŸ”’ Index Master (0-based)
πŸ”„ Loop + Array Pro
πŸ“Š Sum & Max
πŸ“š 50 Students Ready

You can store many values in one variableβ€”like a pro! πŸš€

🏠 Your Coding Mission!

πŸ“₯ Read 5 numbers

Store in array, then print them

βž• Sum & average

Array of 10, find sum and average

πŸ“Š Max and min

Find largest and smallest in array

πŸ“š Marks

Store marks of N students, print average

πŸš€ What's Coming Next!

Strings: Working with Text

Store names and sentences? Strings are arrays of characters!

πŸ€” Think About This Week:

How would you store a person's name in C?