Three parallel arrays hold a supermarket bill — names[][] stores item names, price[] stores unit prices, qty[] stores quantities. The program computes subtotal per item, applies a discount if total exceeds ₹500, adds 18% GST, and prints a formatted receipt.
This is exactly how real POS billing software works — parallel arrays are the simplest database structure in C.
#include <stdio.h> #include <string.h> int main() { /* ── Parallel arrays — each index = one product ─── */ char names[6][20] = { "Rice (1kg)", "Milk (1L)", "Bread", "Eggs (12)", "Butter", "Coffee" }; float price[6] = {60.0, 28.0, 45.0, 72.0, 55.0, 180.0}; int qty[6] = {2, 3, 1, 2, 1, 1}; float subtotal[6], total = 0, discount = 0, gst, grandTotal; int i; /* ── Calculate subtotals ──────────────────────────── */ for (i = 0; i < 6; i++) { subtotal[i] = price[i] * qty[i]; total += subtotal[i]; } /* ── Apply discount: 10% off if total > ₹500 ─────── */ if (total > 500) discount = total * 0.10; gst = (total - discount) * 0.18; grandTotal = total - discount + gst; /* ── Print receipt ────────────────────────────────── */ printf("\n========================================\n"); printf(" ANANTA SUPERMARKET RECEIPT\n"); printf("========================================\n"); printf("%-18s %5s %6s %8s\n","Item","Qty","Price","Amount"); printf("----------------------------------------\n"); for (i = 0; i < 6; i++) printf("%-18s %5d %6.2f %8.2f\n", names[i], qty[i], price[i], subtotal[i]); printf("----------------------------------------\n"); printf("%-30s %8.2f\n", "Subtotal", total); if (discount > 0) printf("%-30s %8.2f\n", "Discount (10%)", -discount); printf("%-30s %8.2f\n", "GST (18%)", gst); printf("========================================\n"); printf("%-30s %8.2f\n", "GRAND TOTAL", grandTotal); printf("========================================\n"); printf("You saved: Rs %.2f!\n", discount); return 0; }
========================================
ANANTA SUPERMARKET RECEIPT
========================================
Item Qty Price Amount
----------------------------------------
Rice (1kg) 2 60.00 120.00
Milk (1L) 3 28.00 84.00
Bread 1 45.00 45.00
Eggs (12) 2 72.00 144.00
Butter 1 55.00 55.00
Coffee 1 180.00 180.00
----------------------------------------
Subtotal 628.00
Discount (10%) -62.80
GST (18%) 101.56
========================================
GRAND TOTAL 666.76
========================================
You saved: Rs 62.80!
i links all arrays: names[i], price[i], qty[i] always refer to the same product.A 5×6 marks grid (5 students, 6 subjects) — the most common 2D array problem in real academia. Computes every student's total and average (across the row), every subject's class average (down the column), finds the class topper, and assigns letter grades. This is exactly what school management software does.
#include <stdio.h> #include <string.h> int main() { #define STU 5 #define SUB 6 char students[STU][15] = { "Ananta", "Priya", "Rahul", "Vikram", "Sneha" }; char subjects[SUB][10] = { "Maths", "Physics", "Chem", "English", "C Prog", "PE" }; int marks[STU][SUB] = { {92, 85, 78, 88, 95, 80}, {78, 82, 90, 75, 88, 92}, {65, 70, 60, 72, 68, 75}, {88, 92, 85, 90, 82, 88}, {95, 98, 92, 96, 99, 94} }; int i, j, total, topIdx = 0; float avg, topAvg = 0; char grade; /* ── Header ───────────────────────────────────────── */ printf("\n%-10s", "Name"); for (j = 0; j < SUB; j++) printf("%-8s", subjects[j]); printf("%-7s %-6s %s\n", "Total", "Avg", "Grade"); printf("%s\n", "------------------------------------------------------------------"); /* ── Per-student row ──────────────────────────────── */ for (i = 0; i < STU; i++) { total = 0; for (j = 0; j < SUB; j++) total += marks[i][j]; avg = (float)total / SUB; if (avg >= 90) grade = 'A'; else if (avg >= 75) grade = 'B'; else if (avg >= 55) grade = 'C'; else grade = 'F'; printf("%-10s", students[i]); for (j = 0; j < SUB; j++) printf("%-8d", marks[i][j]); printf("%-7d %-6.1f %c\n", total, avg, grade); if (avg > topAvg) { topAvg = avg; topIdx = i; } } /* ── Subject averages (column-wise) ──────────────── */ printf("%s\n", "------------------------------------------------------------------"); printf("%-10s", "SubjAvg"); for (j = 0; j < SUB; j++) { int colSum = 0; for (i = 0; i < STU; i++) colSum += marks[i][j]; printf("%-8.1f", (float)colSum / STU); } printf("\n\nCLASS TOPPER: %s (%.1f avg)\n", students[topIdx], topAvg); return 0; }
Name Maths Physics Chem English C Prog PE Total Avg Grade ------------------------------------------------------------------ Ananta 92 85 78 88 95 80 518 86.3 B Priya 78 82 90 75 88 92 505 84.2 B Rahul 65 70 60 72 68 75 410 68.3 C Vikram 88 92 85 90 82 88 525 87.5 B Sneha 95 98 92 96 99 94 574 95.7 A ------------------------------------------------------------------ SubjAvg 83.6 85.4 81.0 84.2 86.4 85.8 CLASS TOPPER: Sneha (95.7 avg)
marks[i][j] array, traversed in two directions, gives two completely different insights.Three parallel 2D string arrays store names, phone numbers, and email addresses — the simplest contact database. User types a name, the program searches using strcmp(), and either displays the contact card or reports not found. This is exactly how phonebook lookup works at its core.
#include <stdio.h> #include <string.h> int main() { #define N 5 /* ── Three parallel string arrays = contact database ─ */ char name[N][20] = { "Ananta", "Priya", "Rahul", "Sneha", "Vikram" }; char phone[N][15] = { "9876543210", "8765432109", "7654321098", "6543210987", "5432109876" }; char email[N][30] = { "ananta@gmail.com", "priya@yahoo.com", "rahul@hotmail.com", "sneha@gmail.com", "vikram@outlook.com" }; char query[20]; int i, found = -1; /* ── Display all contacts ─────────────────────────── */ printf("===== CONTACTS (%d saved) =====\n", N); for (i = 0; i < N; i++) printf("%d. %-12s %s\n", i+1, name[i], phone[i]); /* ── Search ───────────────────────────────────────── */ printf("\nSearch contact: "); scanf("%s", query); for (i = 0; i < N; i++) { if (strcmp(name[i], query) == 0) { /* exact match */ found = i; break; } } /* ── Result ───────────────────────────────────────── */ if (found != -1) { printf("\n┌─── Contact Found ────────────────┐\n"); printf("│ Name : %-28s│\n", name[found]); printf("│ Phone : %-28s│\n", phone[found]); printf("│ Email : %-28s│\n", email[found]); printf("└──────────────────────────────────┘\n"); } else { printf("\n Contact '%s' not found.\n", query); printf(" Tip: Names are case-sensitive.\n"); } return 0; }
===== CONTACTS (5 saved) ===== 1. Ananta 9876543210 2. Priya 8765432109 3. Rahul 7654321098 4. Sneha 6543210987 5. Vikram 5432109876 Search contact: Rahul ┌─── Contact Found ────────────────┐ │ Name : Rahul │ │ Phone : 7654321098 │ │ Email : rahul@hotmail.com │ └──────────────────────────────────┘
strcmp not ==? name[i] == query compares memory addresses — always false even if the text is identical. strcmp(name[i], query) == 0 compares the actual characters. This is one of C's most important rules for strings.An 11×5 2D array stores runs scored by each batsman in each of 5 overs they played. The program computes each player's total, balls faced, strike rate, and determines Man of the Match (highest total). This is the same logic behind Cricinfo's scoring engine.
#include <stdio.h> #include <string.h> int main() { #define PLAYERS 6 #define OVERS 5 char player[PLAYERS][15] = { "Rohit", "Kohli", "Pant", "Hardik", "Jadeja", "Dhoni" }; /* runs[i][j] = runs by player i in over j */ int runs[PLAYERS][OVERS] = { {12, 8, 15, 6, 10}, /* Rohit */ {6, 18, 12, 20, 14}, /* Kohli */ {20, 14, 8, 22, 16}, /* Pant */ {4, 10, 18, 12, 6 }, /* Hardik */ {8, 6, 10, 4, 12}, /* Jadeja */ {18, 22, 16, 8, 24} /* Dhoni */ }; int balls[PLAYERS] = {30, 28, 25, 24, 22, 20}; int i, j, total, teamTotal = 0, momIdx = 0, momScore = 0; float sr; /* ── Scorecard table ──────────────────────────────── */ printf("\n%-10s", "Batsman"); for (j=0;j<OVERS;j++) printf(" Ov%d", j+1); printf(" Total Balls SR\n"); printf("%-s\n","----------------------------------------------------"); for (i = 0; i < PLAYERS; i++) { total = 0; for (j = 0; j < OVERS; j++) total += runs[i][j]; sr = (((float)total / balls[i]) * 100); teamTotal += total; printf("%-10s", player[i]); for (j = 0; j < OVERS; j++) printf("%5d", runs[i][j]); printf("%7d %6d %6.1f\n", total, balls[i], sr); if (total > momScore) { momScore = total; momIdx = i; } } /* ── Over totals (column sums) ────────────────────── */ printf("----------------------------------------------------\n"); printf("%-10s", "OverTotal"); for (j = 0; j < OVERS; j++) { int overSum = 0; for (i = 0; i < PLAYERS; i++) overSum += runs[i][j]; printf("%5d", overSum); } printf(" %5d\n", teamTotal); printf("\nMAN OF THE MATCH: %s (%d runs)\n", player[momIdx], momScore); return 0; }
Batsman Ov1 Ov2 Ov3 Ov4 Ov5 Total Balls SR ---------------------------------------------------- Rohit 12 8 15 6 10 51 30 170.0 Kohli 6 18 12 20 14 70 28 250.0 Pant 20 14 8 22 16 80 25 320.0 Hardik 4 10 18 12 6 50 24 208.3 Jadeja 8 6 10 4 12 40 22 181.8 Dhoni 18 22 16 8 24 88 20 440.0 ---------------------------------------------------- OverTotal 68 78 79 72 82 379 MAN OF THE MATCH: Dhoni (88 runs)
Three parallel 1D float arrays store 12 months of income, expenses, and savings. The program computes savings rate, finds best and worst months, calculates yearly totals, and renders an ASCII bar chart of monthly savings. This is personal finance software in ~60 lines of C.
#include <stdio.h> int main() { char months[12][4] = { "Jan","Feb","Mar","Apr","May","Jun", "Jul","Aug","Sep","Oct","Nov","Dec" }; float income[12] = { 45000,45000,48000,45000,50000,52000, 45000,45000,55000,60000,45000,70000 }; float expenses[12] = { 32000,28000,35000,30000,42000,38000, 29000,31000,40000,45000,50000,55000 }; float savings[12], totalInc=0, totalExp=0, totalSav=0; float bestSav, worstSav; int i, bestMon, worstMon, bars; /* ── Compute savings ──────────────────────────────── */ for (i = 0; i < 12; i++) { savings[i] = income[i] - expenses[i]; totalInc += income[i]; totalExp += expenses[i]; totalSav += savings[i]; } /* ── Find best/worst months ──────────────────────── */ bestSav = worstSav = savings[0]; bestMon = worstMon = 0; for (i = 1; i < 12; i++) { if (savings[i] > bestSav) { bestSav = savings[i]; bestMon = i; } if (savings[i] < worstSav) { worstSav = savings[i]; worstMon = i; } } /* ── Monthly table ────────────────────────────────── */ printf("%-5s %8s %9s %9s %5s\n", "Month","Income","Expenses","Savings","Rate"); printf("%s\n","--------------------------------------------"); for (i = 0; i < 12; i++) { float rate = (savings[i] / income[i]) * 100; printf("%-5s %8.0f %9.0f %9.0f %4.0f%%", months[i], income[i], expenses[i], savings[i], rate); if (i == bestMon) printf(" ← BEST"); if (i == worstMon) printf(" ← WORST"); printf("\n"); } /* ── Yearly summary ──────────────────────────────── */ printf("%s\n","--------------------------------------------"); printf("%-5s %8.0f %9.0f %9.0f %4.0f%%\n", "YEAR", totalInc, totalExp, totalSav, (totalSav/totalInc)*100); /* ── ASCII savings bar chart ─────────────────────── */ printf("\nSavings Bar Chart (each █ = ₹1000):\n"); for (i = 0; i < 12; i++) { printf("%s |", months[i]); bars = (int)(savings[i] / 1000); for (int b = 0; b < bars; b++) printf("█"); printf(" %.0f\n", savings[i]); } return 0; }
Month Income Expenses Savings Rate -------------------------------------------- Jan 45000 32000 13000 29% Feb 45000 28000 17000 38% Mar 48000 35000 13000 27% Apr 45000 30000 15000 33% May 50000 42000 8000 16% ← WORST Jun 52000 38000 14000 27% Jul 45000 29000 16000 36% Aug 45000 31000 14000 31% Sep 55000 40000 15000 27% Oct 60000 45000 15000 25% Nov 45000 50000 -5000 -11% Dec 70000 55000 15000 21% -------------------------------------------- YEAR 605000 455000 150000 25% Savings Bar Chart (each █ = ₹1000): Jan |█████████████ 13000 Feb |█████████████████ 17000 ← BEST Mar |█████████████ 13000 May |████████ 8000 Nov |(-5000)
A 10×4 char array represents the bus — 'A' = Available, 'B' = Booked. The program displays a visual seat map, allows a passenger to book a seat by entering row and column, validates the input, and shows updated availability. This is how IRCTC and RedBus seat maps work internally.
#include <stdio.h> int main() { #define ROWS 10 #define COLS 4 /* 'A' = available, 'B' = booked */ char seat[ROWS][COLS]; int i, j, row, col, avail; /* ── Initialise: some pre-booked ─────────────────── */ for (i = 0; i < ROWS; i++) for (j = 0; j < COLS; j++) seat[i][j] = 'A'; /* Pre-book some seats */ seat[0][0]=seat[0][1]=seat[0][2]='B'; seat[1][3]=seat[2][0]=seat[3][2]='B'; seat[5][1]=seat[7][0]=seat[9][3]='B'; /* ── Print seat map ──────────────────────────────── */ printf("\n========== BUS SEAT MAP ==========\n"); printf(" [A] [B] | [C] [D]\n"); printf(" (Window) (Aisle)(Aisle)(Window)\n"); printf("----------------------------------\n"); avail = 0; for (i = 0; i < ROWS; i++) { printf("Row %2d: ", i + 1); for (j = 0; j < COLS; j++) { if (j == 2) printf(" | "); /* aisle gap */ if (seat[i][j] == 'A') { printf("[ ] "); avail++; } else { printf("[X] "); } } printf("\n"); } printf("----------------------------------\n"); printf("[ ]=Available [X]=Booked Total available: %d/%d\n", avail, ROWS*COLS); /* ── Book a seat ─────────────────────────────────── */ printf("\nEnter seat to book (row 1-%d, col 1-%d): ", ROWS, COLS); scanf("%d %d", &row, &col); row--; col--; /* convert to 0-based */ if (row < 0 || row >= ROWS || col < 0 || col >= COLS) { printf("Invalid seat number!\n"); } else if (seat[row][col] == 'B') { printf("Sorry! Seat %d-%d is already booked.\n", row+1, col+1); } else { seat[row][col] = 'B'; printf("Seat %d-%d booked successfully! Enjoy your journey!\n", row+1, col+1); } return 0; }
========== BUS SEAT MAP ==========
[A] [B] | [C] [D]
(Window) (Aisle)(Aisle)(Window)
----------------------------------
Row 1: [X] [X] | [X] [ ]
Row 2: [ ] [ ] | [ ] [X]
Row 3: [X] [ ] | [ ] [ ]
Row 4: [ ] [ ] | [X] [ ]
Row 5: [ ] [ ] | [ ] [ ]
Row 6: [ ] [X] | [ ] [ ]
Row 7: [ ] [ ] | [ ] [ ]
Row 8: [X] [ ] | [ ] [ ]
Row 9: [ ] [ ] | [ ] [ ]
Row 10: [ ] [ ] | [ ] [X]
----------------------------------
[ ]=Available [X]=Booked Total available: 30/40
Enter seat to book (row 1-10, col 1-4): 5 2
Seat 5-2 booked successfully! Enjoy your journey!
'A' and 'B' as values instead of 0/1 makes the code readable — seat[i][j] == 'A' is instantly clear. Decrementing row and col after input converts human 1-based counting to C's 0-based indexing.Takes a sentence, splits it into words, uses a 2D string array to store unique words and a parallel int array to count how many times each appears. Then sorts by frequency (most common first) and displays a ranked word frequency table. This is the foundation of search engines, plagiarism detectors, and text analysis tools.
#include <stdio.h> #include <string.h> #include <ctype.h> #define MAXWORDS 50 #define MAXLEN 30 int main() { char sentence[] = "to be or not to be that is the question to be"; char words[MAXWORDS][MAXLEN]; /* unique words */ int freq[MAXWORDS]; /* frequency of each word */ int uniqueCount = 0; /* ── Tokenise: extract words one by one ──────────── */ char current[MAXLEN]; int ci = 0, si = 0, i, j, found; while (sentence[si] != '\0') { if (!isspace(sentence[si])) { current[ci++] = tolower(sentence[si]); /* lowercase */ } else if (ci > 0) { current[ci] = '\0'; /* end of word */ ci = 0; /* Check if word already exists */ found = -1; for (i = 0; i < uniqueCount; i++) { if (strcmp(words[i], current) == 0) { found = i; break; } } if (found == -1) { /* new word */ strcpy(words[uniqueCount], current); freq[uniqueCount] = 1; uniqueCount++; } else { /* seen before */ freq[found]++; } } si++; } /* Handle last word (no trailing space) */ if (ci > 0) { current[ci] = '\0'; found = -1; for (i = 0; i < uniqueCount; i++) if (strcmp(words[i], current)==0){found=i;break;} if (found==-1){ strcpy(words[uniqueCount],current); freq[uniqueCount++]=1;} else freq[found]++; } /* ── Sort by frequency (bubble sort) ─────────────── */ for (i = 0; i < uniqueCount - 1; i++) { for (j = 0; j < uniqueCount - i - 1; j++) { if (freq[j] < freq[j+1]) { int tmpF = freq[j]; freq[j] = freq[j+1]; freq[j+1] = tmpF; char tmpW[MAXLEN]; strcpy(tmpW, words[j]); strcpy(words[j], words[j+1]); strcpy(words[j+1], tmpW); } } } /* ── Display ranked frequency table ─────────────── */ printf("\nSentence: \"%s\"\n\n", sentence); printf("%-4s %-15s %-6s BAR\n", "Rank", "Word", "Count"); printf("%s\n", "-------------------------------"); for (i = 0; i < uniqueCount; i++) { printf("#%-3d %-15s %-6d ", i+1, words[i], freq[i]); for (j = 0; j < freq[i]; j++) printf("▓"); printf("\n"); } printf("\nUnique words: %d\n", uniqueCount); return 0; }
Sentence: "to be or not to be that is the question to be" Rank Word Count BAR ------------------------------- #1 to 3 ▓▓▓ #2 be 3 ▓▓▓ #3 or 1 ▓ #4 not 1 ▓ #5 that 1 ▓ #6 is 1 ▓ #7 the 1 ▓ #8 question 1 ▓ Unique words: 8
- Ex 1 — I understand parallel arrays: names[], price[], qty[] all linked by the same index i
- Ex 2 — I can traverse a 2D array both row-wise (student totals) and column-wise (subject averages)
- Ex 3 — I use strcmp() to compare strings, never == which compares addresses not content
- Ex 4 — I can use 2D arrays for sports data and compute both row sums and column sums
- Ex 5 — I can track min/max with a running variable while looping through an array once
- Ex 6 — I can use a 2D char array as a visual grid with 'A'/'B' flags for state
- Ex 7 — I understand that sorting two parallel arrays requires swapping BOTH arrays simultaneously