Two-Dimensional Arrays
Storing data in a grid/table form using rows and columns.
Two-Dimensional Arrays
Two-Dimensional Array (2-D Array) एक ऐसा array है जिसमें data को rows और columns के रूप में store किया जाता है। इसे table या matrix के रूप में समझना आसान होता है। प्रत्येक element को access करने के लिए दो indices (सूचकांक) की आवश्यकता होती है—एक row के लिए और दूसरा column के लिए।
What is a Two-Dimensional Array?
जब किसी array में elements को दो dimensions अर्थात rows और columns में व्यवस्थित किया जाता है, तो उसे Two-Dimensional Array कहते हैं।
उदाहरण के लिए, यदि किसी class के 3 students के 4 subjects के marks store करने हों, तो हम 3 × 4 का two-dimensional array बना सकते हैं। इसमें 3 rows students को और 4 columns subjects को represent कर सकते हैं।
| Student | Maths | Science | English | Computer |
|---|---|---|---|---|
| Student 1 | 80 | 75 | 85 | 90 |
| Student 2 | 70 | 82 | 78 | 88 |
| Student 3 | 90 | 85 | 92 | 95 |
Syntax of Two-Dimensional Array
C++ में two-dimensional array declare करने का सामान्य syntax निम्न है:
data_type array_name[row_size][column_size];
उदाहरण:
int marks[3][4];
यहाँ marks एक two-dimensional integer array है जिसमें 3 rows और 4 columns हैं। इसमें कुल 3 × 4 = 12 elements store किए जा सकते हैं।
Declaration of Two-Dimensional Array
Two-dimensional array को declare करने के लिए row और column दोनों का size specify किया जाता है।
int matrix[2][3];
इस array में 2 rows और 3 columns हैं। इसलिए इसमें कुल 6 integer values store की जा सकती हैं।
| Row | Column 1 | Column 2 | Column 3 |
|---|---|---|---|
| Row 0 | matrix[0][0] | matrix[0][1] | matrix[0][2] |
| Row 1 | matrix[1][0] | matrix[1][1] | matrix[1][2] |
Initialization of Two-Dimensional Array
Two-dimensional array को declaration के समय values से initialize किया जा सकता है।
Method 1: Row-wise Initialization
int matrix[2][3] =
{
{10, 20, 30},
{40, 50, 60}
};
यहाँ पहली row में 10, 20, 30 और दूसरी row में 40, 50, 60 store होंगे।
Method 2: Without Inner Braces
int matrix[2][3] = {10, 20, 30, 40, 50, 60};
C++ values को row-major order में array में store करता है।
Accessing Elements of a Two-Dimensional Array
Two-dimensional array के किसी element को access करने के लिए दो indices का उपयोग किया जाता है। पहला index row को और दूसरा index column को represent करता है।
array_name[row][column];
उदाहरण:
int matrix[2][3] =
{
{10, 20, 30},
{40, 50, 60}
};
cout << matrix[1][2];
60
यहाँ matrix[1][2] का अर्थ है row index 1 और column index 2 वाला element।
Displaying a Two-Dimensional Array
Two-dimensional array के सभी elements को display करने के लिए सामान्यतः nested loops का उपयोग किया जाता है। Outer loop rows के लिए और inner loop columns के लिए होता है।
#include <iostream>
using namespace std;
int main()
{
int matrix[2][3] =
{
{10, 20, 30},
{40, 50, 60}
};
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}
10 20 30
40 50 60
Input in a Two-Dimensional Array
Two-dimensional array में user से values input लेने के लिए भी nested loops का उपयोग किया जाता है।
#include <iostream>
using namespace std;
int main()
{
int matrix[2][2];
cout << "Enter 4 numbers: ";
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
cin >> matrix[i][j];
}
}
cout << "Matrix:" << endl;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}
Enter 4 numbers: 1 2 3 4
Matrix:
1 2
3 4
Traversing a Two-Dimensional Array
Array के सभी elements को एक-एक करके access करने की प्रक्रिया को traversing कहते हैं। Two-dimensional array को traverse करने के लिए nested loops का उपयोग किया जाता है।
Outer Loop → Rows
Inner Loop → Columns
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
// Access matrix[i][j]
}
}
Row and Column Representation
मान लीजिए हमारे पास निम्न array है:
int a[3][3] =
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
इसे matrix के रूप में इस प्रकार देखा जा सकता है:
| Column 0 | Column 1 | Column 2 | |
|---|---|---|---|
| Row 0 | 1 | 2 | 3 |
| Row 1 | 4 | 5 | 6 |
| Row 2 | 7 | 8 | 9 |
इसमें:
a[0][0] = 1a[0][2] = 3a[1][1] = 5a[2][0] = 7a[2][2] = 9
Sum of All Elements
Two-dimensional array के सभी elements का sum निकालने के लिए nested loops का उपयोग किया जा सकता है।
#include <iostream>
using namespace std;
int main()
{
int a[2][3] =
{
{10, 20, 30},
{40, 50, 60}
};
int sum = 0;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
sum = sum + a[i][j];
}
}
cout << "Sum = " << sum;
return 0;
}
Sum = 210
Finding the Largest Element
Nested loops की सहायता से two-dimensional array में सबसे बड़ी value भी find की जा सकती है।
#include <iostream>
using namespace std;
int main()
{
int a[2][3] =
{
{10, 45, 20},
{60, 25, 35}
};
int largest = a[0][0];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
if (a[i][j] > largest)
{
largest = a[i][j];
}
}
}
cout << "Largest = " << largest;
return 0;
}
Largest = 60
Matrix Addition
यदि दो matrices के dimensions समान हों, तो उनके corresponding elements को जोड़कर matrix addition किया जा सकता है।
#include <iostream>
using namespace std;
int main()
{
int A[2][2] =
{
{1, 2},
{3, 4}
};
int B[2][2] =
{
{5, 6},
{7, 8}
};
int C[2][2];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
C[i][j] = A[i][j] + B[i][j];
}
}
cout << "Result:" << endl;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
cout << C[i][j] << " ";
}
cout << endl;
}
return 0;
}
Result:
6 8
10 12
Transpose of a Matrix
Matrix की rows को columns और columns को rows में बदलने की प्रक्रिया को Transpose कहते हैं। यदि original matrix में a[i][j] element है, तो transpose matrix में वह a[j][i] position पर होगा।
#include <iostream>
using namespace std;
int main()
{
int a[2][3] =
{
{1, 2, 3},
{4, 5, 6}
};
cout << "Transpose:" << endl;
for (int j = 0; j < 3; j++)
{
for (int i = 0; i < 2; i++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
return 0;
}
Transpose:
1 4
2 5
3 6
Two-Dimensional Character Array
Two-dimensional arrays केवल numbers के लिए ही नहीं, बल्कि characters को store करने के लिए भी उपयोग किए जा सकते हैं। उदाहरण के लिए, कई words को character array में store किया जा सकता है।
#include <iostream>
using namespace std;
int main()
{
char names[3][10] =
{
"Amit",
"Ravi",
"Neha"
};
for (int i = 0; i < 3; i++)
{
cout << names[i] << endl;
}
return 0;
}
Amit
Ravi
Neha
Difference Between One-Dimensional and Two-Dimensional Array
| One-Dimensional Array | Two-Dimensional Array |
|---|---|
| Data को एक dimension में store करता है। | Data को rows और columns में store करता है। |
| एक index की आवश्यकता होती है। | दो indices की आवश्यकता होती है। |
Example: int a[5]; |
Example: int a[3][4]; |
| List को represent करने में उपयोगी है। | Matrix या table को represent करने में उपयोगी है। |
Applications of Two-Dimensional Arrays
Two-dimensional arrays का उपयोग विभिन्न प्रकार के programs में किया जाता है। इसके प्रमुख applications निम्न हैं:
- Matrix को represent करने में
- Tables और tabular data store करने में
- Students के marks store करने में
- Game boards बनाने में
- Image pixels को represent करने में
- Matrix addition और subtraction में
- Matrix transpose और अन्य matrix operations में
Important Points
- Two-dimensional array data को rows और columns में store करता है।
- इसे matrix या table के रूप में represent किया जा सकता है।
- Two-dimensional array के प्रत्येक element को access करने के लिए दो indices का उपयोग होता है।
- C++ में array indexing 0 से शुरू होती है।
- Nested loops का उपयोग सामान्यतः 2-D array को traverse करने के लिए किया जाता है।
- Outer loop सामान्यतः rows और inner loop columns को handle करता है।
int a[3][4]में 3 rows और 4 columns होते हैं।int a[3][4]में कुल 12 elements store किए जा सकते हैं।- Two-dimensional arrays का उपयोग matrix operations में व्यापक रूप से किया जाता है।
Board Focus
2-D Array → Rows और Columns
Syntax →
data_type array_name[rows][columns];Element Access →
array_name[row][column]Total Elements → Rows × Columns
Traversal → Nested Loops
First Element →
array[0][0]
Board Important Questions
Very Short Answer Questions
Q1. Two-Dimensional Array क्या है?
Answer: ऐसा array जिसमें data को rows और columns के रूप में store किया जाता है, Two-Dimensional Array कहलाता है।
Q2. Two-Dimensional Array में किसी element को access करने के लिए कितने indices की आवश्यकता होती है?
Answer: दो indices की आवश्यकता होती है—एक row के लिए और दूसरा column के लिए।
Q3. Two-Dimensional Array का सामान्य syntax लिखिए।
Answer: data_type array_name[rows][columns];
Q4. int a[3][4] में कितने elements store किए जा सकते हैं?
Answer: इसमें 3 × 4 = 12 elements store किए जा सकते हैं।
Q5. C++ में array indexing कहाँ से शुरू होती है?
Answer: C++ में array indexing 0 से शुरू होती है।
Q6. 2-D array को traverse करने के लिए किसका उपयोग किया जाता है?
Answer: 2-D array को traverse करने के लिए nested loops का उपयोग किया जाता है।
Short Answer Questions
Q7. Two-Dimensional Array की declaration और initialization उदाहरण सहित समझाइए।
Answer: Two-dimensional array की declaration में rows और columns दोनों का size दिया जाता है। उदाहरण:
int a[2][3] =
{
{10, 20, 30},
{40, 50, 60}
};
यहाँ 2 rows और 3 columns हैं तथा कुल 6 elements store किए गए हैं।
Q8. Two-Dimensional Array में nested loops की क्या भूमिका है?
Answer: Nested loops का उपयोग 2-D array के सभी elements को access और process करने के लिए किया जाता है। Outer loop rows को और inner loop columns को represent करता है।
Q9. One-Dimensional और Two-Dimensional Array में अंतर बताइए।
Answer: One-dimensional array में data एक dimension में store होता है और element access करने के लिए एक index की आवश्यकता होती है। Two-dimensional array में data rows और columns में store होता है तथा दो indices की आवश्यकता होती है।
Q10. Matrix Addition क्या है?
Answer: दो समान dimensions वाली matrices के corresponding elements को जोड़ने की प्रक्रिया Matrix Addition कहलाती है।
Long Answer Questions
Q11. Two-Dimensional Array को उदाहरण सहित समझाइए।
Answer: Two-dimensional array ऐसा array है जिसमें elements को rows और columns के रूप में व्यवस्थित किया जाता है। प्रत्येक element को access करने के लिए row और column index का उपयोग किया जाता है। C++ में इसे data_type array_name[rows][columns] syntax से declare किया जाता है। Nested loops की सहायता से इसके elements को input, display और process किया जा सकता है।
Q12. Two-Dimensional Array के सभी elements को display करने का C++ program लिखिए।
#include <iostream>
using namespace std;
int main()
{
int a[2][3] =
{
{1, 2, 3},
{4, 5, 6}
};
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
return 0;
}
1 2 3
4 5 6
Q13. Two-Dimensional Array के सभी elements का sum निकालने का C++ program लिखिए।
#include <iostream>
using namespace std;
int main()
{
int a[2][2] =
{
{10, 20},
{30, 40}
};
int sum = 0;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
sum += a[i][j];
}
}
cout << "Sum = " << sum;
return 0;
}
Sum = 100
Q14. निम्न program का output बताइए:
#include <iostream>
using namespace std;
int main()
{
int a[2][3] =
{
{10, 20, 30},
{40, 50, 60}
};
cout << a[0][2] << endl;
cout << a[1][1];
return 0;
}
30
50
Q15. निम्न 2-D array का output बताइए:
int a[2][2] =
{
{1, 2},
{3, 4}
};
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
1 2
3 4
Quick Revision
- 2-D Array: Rows और columns में data store करना
- Declaration:
int a[rows][columns]; - Access:
a[row][column] - Indexing: 0 से शुरू
- Total Elements: Rows × Columns
- Traversal: Nested loops
- Matrix: 2-D array का common representation
- Transpose: Rows को columns और columns को rows में बदलना
Practice Questions
- Two-Dimensional Array की परिभाषा लिखिए।
- Two-Dimensional Array का syntax लिखिए।
int a[4][5]में कुल कितने elements store किए जा सकते हैं?- Two-Dimensional Array में element को access करने का syntax लिखिए।
- Nested loops का उपयोग 2-D array में क्यों किया जाता है?
- एक 3 × 3 matrix declare और initialize कीजिए।
- एक C++ program लिखिए जो 2-D array के सभी elements display करे।
- एक C++ program लिखिए जो 2-D array के सभी elements का sum निकाले।
- एक C++ program लिखिए जो 2-D array में largest element find करे।
- Matrix Addition को उदाहरण सहित समझाइए।
- Matrix Transpose क्या है?
- One-Dimensional और Two-Dimensional Array में अंतर बताइए।
- निम्न array में
a[2][1]का value बताइए:int a[3][2] = { {10, 20}, {30, 40}, {50, 60} }; - एक C++ program लिखिए जो 2-D matrix का transpose display करे।
- एक C++ program लिखिए जो दो 2 × 2 matrices को add करे।