2d arrays
🔹 Step 1: Introduction
👨🏫 Teacher:
“Bachcho, aaj hum 2D Array in C पढ़ेंगे.
Pehle yaad करो 1D array kya hota hai?
1D array ek line jaisa hota hai jisme elements side-by-side store hote hain.
But kabhi kabhi hume data ko rows aur columns ki form me store karna padta hai, jaise:
Cricket score table
Students ke marks in 3 subjects
Chess board
👉 Tab hum use karte hain 2D Array.”
🔹 Step 2: Definition
👨🏫 Teacher:
“2D array is basically an array of arrays.
Socho jaise ek table:
Row 0 → [ 10 20 30 ]
Row 1 → [ 40 50 60 ]
Yaha row aur column dono hote hain.”
🔹 Step 3: Declaration
👨🏫 Teacher:
“Syntax:”
data_type array_name[rows][columns];
Example:
int marks[3][4]; // 3 rows, 4 columns
💡 Question for Students:
👉 Agar maine int arr[5][6]; banaya hai, to total kitne elements honge?
Answer = 5 × 6 = 30 elements.
🔹 Step 4: Initialization
👨🏫 Teacher:
“Array ko initialize karne ke 3 common tarike hote hain.”
(i) Row-wise braces:
int a[2][3] = { {1,2,3}, {4,5,6} };
(ii) Without braces:
int b[2][3] = {1,2,3,4,5,6};
(iii) Partial initialization:
int c[2][3] = { {1}, {4,5} };
// Result = { {1,0,0}, {4,5,0} }
🔹 Step 5: Accessing Elements
👨🏫 Teacher:
“Kisi bhi element ko access karne ka formula:”
arr[row][column]
Example:
printf("%d", a[1][2]);
// Means → 2nd row, 3rd column
🔹 Step 6: Input/Output with Loops
👨🏫 Teacher:
“Jab hume user se values leni hain aur print karni hain, to nested loop ka use hota hai.”
#include <stdio.h>
int main() {
int arr[2][3];
// Input
printf("Enter 6 elements:\n");
for(int i=0; i<2; i++) {
for(int j=0; j<3; j++) {
scanf("%d", &arr[i][j]);
}
}
// Output
printf("Matrix is:\n");
for(int i=0; i<2; i++) {
for(int j=0; j<3; j++) {
printf("%d ", arr[i][j]);
}
printf("\n"); // row complete hone ke baad new line
}
return 0;
}
📌 Teacher dry run:
Suppose input = 1 2 3 4 5 6
Matrix banega:
1 2 3
4 5 6
🔹 Step 7: Memory Representation
👨🏫 Teacher:
“C me data row-major order me store hota hai.”
Example:
int a[2][3] = { {1,2,3}, {4,5,6} };
Memory:1 2 3 4 5 6
👉 Matlab pehle puri row-0 store hoti hai, fir row-1.
🔹 Step 8: Applications
👨🏫 Teacher:
“2D arrays kaha use hote hain?
Matrices solve karne me
Tabular data (marks, sales report)
Image processing (pixels row × column)
Games (tic-tac-toe, chessboard)”
🔹 Step 9: Practice Questions
👨🏫 Teacher to Class:
“Ab kuch important problems karte hain.”
Matrix ko row-wise aur column-wise print karo.
Transpose of matrix likho.
Har row aur column ka sum nikalo.
Kisi element ko search karo.
2 matrices ka multiplication karo.
📢 Teacher:
“Ab homework: Har problem ko ek-ek karke code karke try karo.
Next class me hum transpose aur sum of rows/columns ka code detail me dry run karenge.”