🟡 Intermediate · Lesson 21
2D Arrays और Matrix
2D Array क्या है?
2D array arrays का array है — essentially एक matrix जिसमें rows और columns होते हैं। इसे tabular data जैसे students के multiple subjects में marks, mathematics में matrices, या किसी भी grid-based data store करने के लिए use किया जाता है।
Concept
// 2D array = matrix (rows और columns)
// col0 col1 col2
// row0: 10 20 30
// row1: 40 50 60
// Access: arr[row][col] — दोनों 0 से शुरूDeclaration और Initialization
C Language
int matrix[3][3]; // Declare only — garbage values int a[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; // Initialize with values int zeros[4][4] = {0}; // सब 0
Matrix Operations
C Language – Matrix Print
int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) printf("%4d", a[i][j]); printf("\n"); }
1 2 3
4 5 6
7 8 9
💡 Student Marks Example
marks[3][4] — 3 students, 4 subjects। marks[0][2] = पहले student का तीसरा subject।
सारांश
- 2D array = rows और columns वाला matrix:
type name[ROWS][COLS] - Element access:
arr[row][col]— दोनों indices 0 से शुरू - Traverse: outer loop = rows, inner loop = columns
- Uses: student marks, matrix calculations, game boards, image processing
अक्सर पूछे जाने वाले प्रश्न (FAQ)
C में two-dimensional array क्या है?
Two-dimensional array data को rows और columns के grid में रखता है, दो sizes से declare किया जाता है जैसे 3 rows और 4 columns के लिए
int matrix[3][4];। आप हर element को दो indexes से access करते हैं, जैसे matrix[i][j], इसे tables और matrices के लिए आदर्श बनाते हुए।C में 2D array कैसे declare करते हैं?
आप type, नाम और कोष्ठकों में दो sizes देते हैं, जैसे
int a[3][3];। आप इसे nested braces से initialise कर सकते हैं, जैसे int a[2][2] = {{1, 2}, {3, 4}};, जहाँ braces का हर भीतरी समूह एक row है।C में 2D array memory में कैसे रखा जाता है?
C 2D array को row-major क्रम में रखता है, यानी पूरी row 0 पहले रखी जाती है, फिर पूरी row 1, और ऐसे ही, memory के एक निरंतर block में। इसीलिए row-by-row loop करना आमतौर पर column-by-column से ज़्यादा cache-friendly होता है।
2D array के elements कैसे access करते हैं?
आप दो indexes इस्तेमाल करते हैं: पहला row के लिए और दूसरा column के लिए, जैसे
a[i][j]। Nested loops — rows पर बाहरी loop और columns पर भीतरी loop — हर element तक पहुँचने का सामान्य तरीका है।C में 2D arrays किसलिए इस्तेमाल होते हैं?
Two-dimensional arrays किसी भी grid-आकार चीज़ के लिए इस्तेमाल होते हैं: matrices, game boards, data की tables, pixel grid के रूप में images, और seating charts। जब भी data में स्वाभाविक रूप से rows और columns हों, 2D array उपयुक्त है।