1
🎲 Your 5 Favourite Numbers
1D int array
What this teaches: A 1D array is like a row of labelled boxes. You put one value in each box. To get a value back, you say which box number you want — that's the
index. Index always starts at 0, not 1.
int nums[5] — 5 boxes, each holding one number
nums[ ]
7
[0]
42
[1]
13
[2]
99
[3]
5
[4]
← nums[3] = 99 is biggest
5 elements · indices 0 to 4 · nums[0] is first · nums[4] is last
#include <stdio.h> int main() { /* Store 5 favourite numbers in an array */ int nums[5] = {7, 42, 13, 99, 5}; int i, biggest, smallest; /* Print all numbers */ printf("Your 5 numbers: "); for (i = 0; i < 5; i++) printf("%d ", nums[i]); printf("\n"); /* Find biggest — start assuming nums[0] is biggest */ biggest = nums[0]; for (i = 1; i < 5; i++) { if (nums[i] > biggest) biggest = nums[i]; /* update if we find bigger */ } /* Find smallest — same idea */ smallest = nums[0]; for (i = 1; i < 5; i++) { if (nums[i] < smallest) smallest = nums[i]; } /* Print result */ printf("Biggest : %d\n", biggest); printf("Smallest : %d\n", smallest); printf("Total : "); int total = 0; for (i = 0; i < 5; i++) total += nums[i]; printf("%d\n", total); return 0; }
Your 5 numbers: 7 42 13 99 5 Biggest : 99 Smallest : 5 Total : 166
Key idea: To find the biggest, you start by assuming the first element
nums[0] is the biggest. Then you walk through the rest. Every time you find something bigger, you update your answer. This single-pass technique works for any array of any size.example 2
2
🌡️ Weekly Temperature Diary
1D float array
What this teaches: Arrays are perfect for repeating measurements — one entry per day, per student, per product. A float array stores decimal numbers.
float temp[7] gives you exactly 7 slots, one for each day of the week.
float temp[7] — one slot per day of the week
temp[ ]
28.5
Mon[0]
31.2
Tue[1]
38.0
Wed[2]
35.5
Thu[3]
29.0
Fri[4]
26.3
Sat[5]
27.8
Sun[6]
Wednesday (index 2) is the hottest — 38.0°C highlighted
#include <stdio.h> int main() { char days[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }; float temp[7] = {28.5, 31.2, 38.0, 35.5, 29.0, 26.3, 27.8}; int i, hotDay = 0, hotCount = 0; float total = 0; /* Print diary */ printf("Day Temp Comment\n"); printf("---- ----- ----------\n"); for (i = 0; i < 7; i++) { printf("%-4s %5.1f ", days[i], temp[i]); if (temp[i] >= 35) printf("Very hot!\n"); else if (temp[i] >= 30) printf("Hot\n"); else printf("Pleasant\n"); total += temp[i]; if (temp[i] > temp[hotDay]) hotDay = i; if (temp[i] > 30) hotCount++; } printf("\nWeek average : %.1f C\n", total / 7); printf("Hottest day : %s (%.1f C)\n", days[hotDay], temp[hotDay]); printf("Days over 30C: %d\n", hotCount); return 0; }
Day Temp Comment ---- ----- ---------- Mon 28.5 Pleasant Tue 31.2 Hot Wed 38.0 Very hot! Thu 35.5 Very hot! Fri 29.0 Pleasant Sat 26.3 Pleasant Sun 27.8 Pleasant Week average : 30.9 C Hottest day : Wed (38.0 C) Days over 30C: 3
Two arrays, same index:
days[i] and temp[i] always refer to the same day. Index 2 gives "Wed" AND 38.0 — they are linked by the shared index. This is called a parallel array — the most natural way to store related data in C.example 3
3
🪑 Classroom Seating Chart
2D int array
What this teaches: A 2D array is like a grid.
int seat[3][4] means 3 rows and 4 columns — like a classroom. The first index picks the row, the second picks the seat in that row. 1 = occupied, 0 = free.
int seat[3][4] — 3 rows × 4 seats per row = 12 seats total
—
Seat 1
Seat 2
Seat 3
Seat 4
Row 1
1
1
0
1
Row 2
0
1
1
0
Row 3
1
0
0
1
1 = Occupied
0 = Free
5 free seats in green
#include <stdio.h> int main() { /* 1 = occupied, 0 = free */ int seat[3][4] = { {1, 1, 0, 1}, /* row 0: seat 3 is free */ {0, 1, 1, 0}, /* row 1: seats 1 and 4 are free */ {1, 0, 0, 1} /* row 2: seats 2 and 3 are free */ }; int i, j, freeCount = 0; /* Print the seating map */ printf("CLASSROOM SEATING MAP\n"); printf(" S1 S2 S3 S4\n"); printf(" ---- ---- ---- ----\n"); for (i = 0; i < 3; i++) { printf("Row %d: ", i + 1); for (j = 0; j < 4; j++) { if (seat[i][j] == 1) printf(" [X] "); /* occupied */ else { printf(" [ ] "); /* free */ freeCount++; } } printf("\n"); } printf("\n[X] = Taken [ ] = Free\n"); printf("Free seats : %d\n", freeCount); printf("Taken seats: %d\n", 12 - freeCount); /* Ask which seat they want */ int r, s; printf("\nEnter row (1-3) and seat (1-4): "); scanf("%d %d", &r, &s); r--; s--; /* convert to 0-based */ if (seat[r][s] == 0) { seat[r][s] = 1; printf("Seat booked! Enjoy class.\n"); } else { printf("Sorry, that seat is taken!\n"); } return 0; }
CLASSROOM SEATING MAP
S1 S2 S3 S4
---- ---- ---- ----
Row 1: [X] [X] [ ] [X]
Row 2: [ ] [X] [X] [ ]
Row 3: [X] [ ] [ ] [X]
[X] = Taken [ ] = Free
Free seats : 5
Taken seats: 7
Enter row (1-3) and seat (1-4): 2 1
Seat booked! Enjoy class.
Two indices, two directions:
seat[i][j] — i picks the row, j picks the column. The nested loop visits every single cell. Outer loop walks rows, inner loop walks across each row. This is the core 2D array pattern.example 4
4
✏️ Your Name — Character by Character
1D char array (string)
What this teaches: In C, a string is just a
char array that ends with a special invisible character '\0' (called the null terminator). You can access any letter the same way you access any array element — name[0] is the first letter, name[1] is the second, and so on.
char name[] = "Ananta" — stored as individual characters + '\0' at end
name[ ]
'A'
[0]
'n'
[1]
'a'
[2]
'n'
[3]
't'
[4]
'a'
[5]
'\0'
[6]
null end
Yellow = vowels · '\0' at index 6 marks end of string · strlen = 6
#include <stdio.h> #include <string.h> int main() { char name[20]; int i, len, vowels = 0, consonants = 0; printf("Enter your name: "); scanf("%s", name); len = strlen(name); /* count characters */ /* Print name letter by letter with position */ printf("\nYour name has %d letters:\n", len); for (i = 0; i < len; i++) printf(" Letter %d : %c\n", i + 1, name[i]); /* Count vowels and consonants */ for (i = 0; i < len; i++) { char c = name[i]; if (c=='a'||c=='e'||c=='i'||c=='o'||c=='u'|| c=='A'||c=='E'||c=='I'||c=='O'||c=='U') vowels++; else consonants++; } /* Print name reversed */ printf("\nReversed : "); for (i = len - 1; i >= 0; i--) printf("%c", name[i]); printf("\nVowels : %d\n", vowels); printf("Consonants : %d\n", consonants); /* First and last letter */ printf("First letter: %c\n", name[0]); printf("Last letter: %c\n", name[len - 1]); return 0; }
Enter your name: Ananta Your name has 6 letters: Letter 1 : A Letter 2 : n Letter 3 : a Letter 4 : n Letter 5 : t Letter 6 : a Reversed : atnanA Vowels : 4 Consonants : 2 First letter: A Last letter: a
To reverse a string you start from the last index (
len-1) and count down to 0. That's it — just loop backwards. No extra array needed. strlen() counts the characters and does NOT count the invisible '\0' at the end.example 5
5
📋 Class Name List — Roll Call
2D char array (strings)
What this teaches: A
char names[5][20] is an array of strings — 5 rows, each holding a name up to 19 characters. Each row is one string. names[0] is the first name, names[4] is the last. Use strcmp() to compare strings, never ==.
char names[5][20] — each row is one student name
names[0]
'A'
'n'
't'
'a'
'\0'
← "Anta"
names[1]
'P'
'r'
'i'
'y'
'a'
'\0'
← "Priya"
names[2]
'R'
'a'
'h'
'u'
'l'
'\0'
← "Rahul"
Each row = one name · Red '\0' marks end of each string · Max 19 chars per name
#include <stdio.h> #include <string.h> int main() { char names[5][20] = { "Priya", "Ananta", "Sneha", "Rahul", "Vikram" }; int present[5] = {1, 1, 0, 1, 1}; /* 1=here 0=absent */ int i, j, found; char search[20], temp[20]; /* ── Roll call ──────────────────────────────── */ printf("=== ROLL CALL ===\n"); for (i = 0; i < 5; i++) { printf("%d. %-10s %s\n", i+1, names[i], present[i] ? "Present" : "ABSENT"); } /* ── Count present and absent ───────────────── */ int presentCount = 0; for (i = 0; i < 5; i++) if (present[i]) presentCount++; printf("\nPresent: %d Absent: %d\n", presentCount, 5 - presentCount); /* ── Search for a name ──────────────────────── */ printf("\nSearch name: "); scanf("%s", search); found = -1; for (i = 0; i < 5; i++) { if (strcmp(names[i], search) == 0) { found = i; break; } } if (found != -1) printf("%s is roll number %d — %s\n", search, found+1, present[found] ? "Present" : "Absent"); else printf("%s not found in class.\n", search); /* ── Sort names alphabetically (bubble sort) ── */ for (i = 0; i < 4; i++) for (j = 0; j < 4 - i; j++) if (strcmp(names[j], names[j+1]) > 0) { strcpy(temp, names[j]); strcpy(names[j], names[j+1]); strcpy(names[j+1], temp); } printf("\nAlphabetical order:\n"); for (i = 0; i < 5; i++) printf("%d. %s\n", i+1, names[i]); return 0; }
=== ROLL CALL === 1. Priya Present 2. Ananta Present 3. Sneha ABSENT 4. Rahul Present 5. Vikram Present Present: 4 Absent: 1 Search name: Sneha Sneha is roll number 3 — Absent Alphabetical order: 1. Ananta 2. Priya 3. Rahul 4. Sneha 5. Vikram
Three things to remember about string arrays:
1.
2. Always use
3. Use
1.
char names[5][20] — first number is how many strings, second is max length of each2. Always use
strcmp(a, b) == 0 to check if two strings are equal — never ==3. Use
strcpy(dest, src) to copy a string — never dest = src
checklist
- Ex 1 — A 1D array stores values in numbered boxes starting at index 0
- Ex 1 — To find biggest: start with nums[0], replace whenever you find something larger
- Ex 2 — Parallel arrays: same index links two related arrays (days[i] + temp[i])
- Ex 3 — 2D array: first index = row, second index = column, nested loops visit all cells
- Ex 4 — A string is a char array ending in '\0' — access each letter with name[i]
- Ex 4 — strlen() counts characters but does NOT count '\0'
- Ex 5 — char names[5][20] stores 5 strings, each up to 19 characters long
- Ex 5 — Always use strcmp() to compare strings, strcpy() to copy strings