Two-Dimensional Array Operations
Working with data arranged in rows and columns.
Two-Dimensional Array Operations
Two-Dimensional Array एक ऐसा array है जिसमें data को rows (पंक्तियों) और columns (स्तंभों) के रूप में store किया जाता है। इसे सामान्यतः table या matrix के रूप में represent किया जाता है। C++ में two-dimensional arrays का उपयोग matrix, marks table और अन्य tabular data को store करने के लिए किया जाता है।
Declaration of a Two-Dimensional Array
Two-dimensional array declare करने के लिए दो sizes दिए जाते हैं—पहला rows की संख्या और दूसरा columns की संख्या।
General syntax:
data_type array_name[rows][columns];
उदाहरण:
int matrix[3][4];
यहाँ matrix में 3 rows और 4 columns हैं। इसलिए इसमें कुल 3 × 4 = 12 elements store किए जा सकते हैं।
Initialization of a Two-Dimensional Array
Two-dimensional array को declaration के समय values के साथ initialize किया जा सकता है।
int matrix[2][3] = {
{10, 20, 30},
{40, 50, 60}
};
इसे matrix के रूप में इस प्रकार समझ सकते हैं:
| Column 0 | Column 1 | Column 2 | |
|---|---|---|---|
| Row 0 | 10 | 20 | 30 |
| Row 1 | 40 | 50 | 60 |
Accessing Elements
Two-dimensional array के किसी element को access करने के लिए दो indices का उपयोग किया जाता है:
उदाहरण:
int matrix[2][3] = {
{10, 20, 30},
{40, 50, 60}
};
cout << matrix[0][1];
20
यहाँ matrix[0][1] का अर्थ row 0 और column 1 का element है।
1. Traversal of a Two-Dimensional Array
Two-dimensional array के सभी elements को access करने के लिए सामान्यतः 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
Inner loop → Columns
2. Taking Input in a Two-Dimensional Array
User से matrix के elements input लेने के लिए nested loops का उपयोग किया जाता है।
#include <iostream>
using namespace std;
int main()
{
int matrix[2][2];
cout << "Enter 4 elements: ";
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 elements: 1 2 3 4
Matrix:
1 2
3 4
3. Updating an Element
Two-dimensional array के किसी particular element की value को उसके row और column index की सहायता से बदला जा सकता है।
#include <iostream>
using namespace std;
int main()
{
int matrix[2][3] = {
{10, 20, 30},
{40, 50, 60}
};
matrix[1][2] = 100;
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 100
यहाँ row 1 और column 2 के element की value 60 से बदलकर 100 कर दी गई है।
4. Finding Sum of All Elements
Matrix के सभी elements का sum निकालने के लिए nested loops के साथ एक sum variable का उपयोग किया जा सकता है।
#include <iostream>
using namespace std;
int main()
{
int matrix[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 += matrix[i][j];
}
}
cout << "Sum = " << sum;
return 0;
}
Sum = 210
5. Finding Sum of Each Row
प्रत्येक row का अलग-अलग sum निकालने के लिए 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++)
{
int sum = 0;
for (int j = 0; j < 3; j++)
{
sum += matrix[i][j];
}
cout << "Row " << i + 1
<< " Sum = " << sum << endl;
}
return 0;
}
Row 1 Sum = 60
Row 2 Sum = 150
6. Finding Sum of Each Column
प्रत्येक column का sum निकालने के लिए outer loop columns के लिए और inner loop rows के लिए चलाया जा सकता है।
#include <iostream>
using namespace std;
int main()
{
int matrix[2][3] = {
{10, 20, 30},
{40, 50, 60}
};
for (int j = 0; j < 3; j++)
{
int sum = 0;
for (int i = 0; i < 2; i++)
{
sum += matrix[i][j];
}
cout << "Column " << j + 1
<< " Sum = " << sum << endl;
}
return 0;
}
Column 1 Sum = 50
Column 2 Sum = 70
Column 3 Sum = 90
7. Addition of Two Matrices
दो matrices को add करने के लिए दोनों matrices का order समान होना चाहिए। Corresponding elements को add करके result matrix बनाया जाता है।
यदि:
C[i][j] = A[i][j] + B[i][j]
Program for 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
8. Subtraction of Two Matrices
Matrix subtraction में corresponding elements को subtract किया जाता है। इसके लिए दोनों matrices का order समान होना चाहिए।
#include <iostream>
using namespace std;
int main()
{
int A[2][2] = {
{10, 20},
{30, 40}
};
int B[2][2] = {
{1, 2},
{3, 4}
};
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];
}
}
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
cout << C[i][j] << " ";
}
cout << endl;
}
return 0;
}
9 18
27 36
9. Transpose of a Matrix
Transpose में matrix की rows को columns और columns को rows में बदल दिया जाता है। यदि original matrix A है, तो उसका transpose सामान्यतः AT से represent किया जाता है।
उदाहरण:
1 2 3
4 5 6
Transpose:
1 4
2 5
3 6
Program to Find Transpose
#include <iostream>
using namespace std;
int main()
{
int A[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
int T[3][2];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
T[j][i] = A[i][j];
}
}
cout << "Transpose:" << endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
{
cout << T[i][j] << " ";
}
cout << endl;
}
return 0;
}
Transpose:
1 4
2 5
3 6
10. Matrix Multiplication
दो matrices का multiplication तभी संभव है जब पहली matrix के columns की संख्या दूसरी matrix की rows की संख्या के बराबर हो।
A(m × n) × B(n × p) = C(m × p)
Result matrix का प्रत्येक element तीन या अधिक multiplication और addition के आधार पर calculate किया जाता है:
Program for Matrix Multiplication
#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] = {0};
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 2; k++)
{
C[i][j] += A[i][k] * B[k][j];
}
}
}
cout << "Product:" << endl;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
cout << C[i][j] << " ";
}
cout << endl;
}
return 0;
}
Product:
19 22
43 50
11. Finding the Largest Element
Two-dimensional array में largest element खोजने के लिए सभी elements को एक-एक करके compare किया जा सकता है।
#include <iostream>
using namespace std;
int main()
{
int matrix[2][3] = {
{10, 45, 20},
{80, 25, 60}
};
int largest = matrix[0][0];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
if (matrix[i][j] > largest)
{
largest = matrix[i][j];
}
}
}
cout << "Largest element: " << largest;
return 0;
}
Largest element: 80
12. Finding the Smallest Element
इसी प्रकार smallest element खोजने के लिए प्रत्येक element की तुलना current minimum value से की जाती है।
#include <iostream>
using namespace std;
int main()
{
int matrix[2][3] = {
{10, 45, 20},
{80, 25, 5}
};
int smallest = matrix[0][0];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
if (matrix[i][j] < smallest)
{
smallest = matrix[i][j];
}
}
}
cout << "Smallest element: " << smallest;
return 0;
}
Smallest element: 5
13. Main Diagonal of a Matrix
Square matrix में main diagonal के elements वे होते हैं जिनके row और column indices समान होते हैं। अर्थात:
उदाहरण के लिए:
4 5 6
7 8 9
Main Diagonal → 1, 5, 9
Program to Display Main Diagonal
#include <iostream>
using namespace std;
int main()
{
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
cout << "Main diagonal: ";
for (int i = 0; i < 3; i++)
{
cout << matrix[i][i] << " ";
}
return 0;
}
Main diagonal: 1 5 9
14. Sum of Main Diagonal
Square matrix की main diagonal का sum निकालने के लिए matrix[i][i] elements को add किया जाता है।
#include <iostream>
using namespace std;
int main()
{
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int sum = 0;
for (int i = 0; i < 3; i++)
{
sum += matrix[i][i];
}
cout << "Diagonal sum = " << sum;
return 0;
}
Diagonal sum = 15
15. Finding the Number of Even and Odd Elements
Two-dimensional array में even और odd elements की संख्या nested loops और modulus operator की सहायता से निकाली जा सकती है।
#include <iostream>
using namespace std;
int main()
{
int matrix[2][3] = {
{10, 15, 20},
{25, 30, 35}
};
int even = 0;
int odd = 0;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
if (matrix[i][j] % 2 == 0)
even++;
else
odd++;
}
}
cout << "Even elements: " << even << endl;
cout << "Odd elements: " << odd;
return 0;
}
Even elements: 3
Odd elements: 3
Common Two-Dimensional Array Operations
| Operation | Meaning |
|---|---|
| Traversal | सभी elements को access करना |
| Input | Array में values enter करना |
| Updating | किसी element की value बदलना |
| Searching | किसी value को ढूँढना |
| Addition | दो matrices के corresponding elements को जोड़ना |
| Subtraction | दो matrices के corresponding elements को subtract करना |
| Transpose | Rows को columns और columns को rows में बदलना |
| Multiplication | दो compatible matrices का product निकालना |
| Diagonal Operations | Main या other diagonal के elements पर operation करना |
Important Points
- Two-dimensional array data को rows और columns में store करता है।
- Two-dimensional array के elements को दो indices से access किया जाता है।
- C++ में array indexing 0 से शुरू होती है।
- Nested loops का उपयोग two-dimensional array traversal में किया जाता है।
- Outer loop सामान्यतः rows और inner loop columns को represent करता है।
- Matrix addition और subtraction के लिए दोनों matrices का order समान होना चाहिए।
- Matrix multiplication के लिए पहली matrix के columns और दूसरी matrix की rows की संख्या समान होनी चाहिए।
- Transpose में rows और columns की positions interchange हो जाती हैं।
- Main diagonal के elements में row index और column index समान होता है।
- Two-dimensional array का उपयोग matrices और tabular data को represent करने के लिए किया जाता है।
Board Focus
Declaration → int a[rows][columns];
Access → a[i][j]
Traversal → Nested Loops
Outer Loop → Rows
Inner Loop → Columns
Matrix Addition → C[i][j] = A[i][j] + B[i][j]
Transpose → T[j][i] = A[i][j]
Main Diagonal → a[i][i]
Matrix Multiplication → First matrix columns = Second matrix rows
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 index और column index।
Q3. Two-dimensional array में पहला index किसे represent करता है?
Answer: पहला index सामान्यतः row को represent करता है।
Q4. Two-dimensional array में दूसरा index किसे represent करता है?
Answer: दूसरा index column को represent करता है।
Q5. Two-dimensional array traversal के लिए किसका उपयोग किया जाता है?
Answer: Nested loops का उपयोग किया जाता है।
Q6. Matrix का transpose क्या है?
Answer: Matrix की rows को columns और columns को rows में बदलना transpose कहलाता है।
Q7. Main diagonal के elements की क्या विशेषता है?
Answer: Main diagonal के elements में row index और column index समान होते हैं।
Short Answer Questions
Q8. Two-dimensional array को उदाहरण सहित समझाइए।
Answer: Two-dimensional array में elements rows और columns के रूप में store होते हैं। उदाहरण के लिए:
int a[2][3] = {
{10, 20, 30},
{40, 50, 60}
};
इसमें 2 rows और 3 columns हैं तथा कुल 6 elements store किए गए हैं।
Q9. Two-dimensional array में nested loops का उपयोग क्यों किया जाता है?
Answer: Two-dimensional array में rows और columns दोनों होते हैं। इसलिए सभी elements को access करने के लिए एक loop rows और दूसरा loop columns के लिए उपयोग किया जाता है। इन loops को nested loops कहा जाता है।
Q10. Matrix addition के लिए क्या condition आवश्यक है?
Answer: Matrix addition के लिए दोनों matrices का order समान होना चाहिए, अर्थात दोनों में rows और columns की संख्या समान होनी चाहिए।
Q11. Matrix multiplication की condition क्या है?
Answer: दो matrices A और B का multiplication तभी संभव है जब A के columns की संख्या B की rows की संख्या के बराबर हो।
Q12. Matrix transpose को उदाहरण सहित समझाइए।
Answer: Transpose में matrix की rows को columns और columns को rows में बदल दिया जाता है। यदि original matrix 2 × 3 है, तो उसका transpose 3 × 2 होगा।
Long Answer Questions
Q13. Two-dimensional array के सभी elements display करने का C++ program लिखिए।
#include <iostream>
using namespace std;
int main()
{
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++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
return 0;
}
1 2 3
4 5 6
7 8 9
Q14. दो matrices को add करने का C++ program लिखिए।
#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];
}
}
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
cout << C[i][j] << " ";
}
cout << endl;
}
return 0;
}
6 8
10 12
Q15. Matrix का transpose निकालने का C++ program लिखिए।
#include <iostream>
using namespace std;
int main()
{
int A[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
int T[3][2];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
T[j][i] = A[i][j];
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
{
cout << T[i][j] << " ";
}
cout << endl;
}
return 0;
}
1 4
2 5
3 6
Q16. दो matrices का multiplication करने का C++ program लिखिए।
#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] = {0};
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 2; k++)
{
C[i][j] += A[i][k] * B[k][j];
}
}
}
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
cout << C[i][j] << " ";
}
cout << endl;
}
return 0;
}
19 22
43 50
Q17. Two-dimensional array में प्रत्येक row का sum निकालने का program लिखिए।
#include <iostream>
using namespace std;
int main()
{
int a[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for (int i = 0; i < 3; i++)
{
int sum = 0;
for (int j = 0; j < 3; j++)
{
sum += a[i][j];
}
cout << "Row " << i + 1
<< " Sum = " << sum << endl;
}
return 0;
}
Row 1 Sum = 6
Row 2 Sum = 15
Row 3 Sum = 24
Q18. Square matrix की main diagonal का sum निकालने का C++ program लिखिए।
#include <iostream>
using namespace std;
int main()
{
int a[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int sum = 0;
for (int i = 0; i < 3; i++)
{
sum += a[i][i];
}
cout << "Diagonal sum = " << sum;
return 0;
}
Diagonal sum = 15
Quick Revision
- Two-Dimensional Array: Rows और columns में data store करने वाला array
- Index: row और column दोनों के लिए उपयोग होता है
- Traversal: Nested loops द्वारा सभी elements access करना
- Updating: किसी element की value बदलना
- Matrix Addition: Corresponding elements को जोड़ना
- Matrix Subtraction: Corresponding elements को subtract करना
- Transpose: Rows और columns को interchange करना
- Matrix Multiplication: Compatible matrices का product निकालना
- Main Diagonal: a[i][i]
- Row Sum: एक row के सभी elements का sum
- Column Sum: एक column के सभी elements का sum
Practice Questions
- Two-dimensional array क्या है?
- Two-dimensional array को declare और initialize करने का syntax लिखिए।
- Two-dimensional array के elements को access कैसे किया जाता है?
- Nested loops क्या हैं और matrix traversal में इनका उपयोग क्यों किया जाता है?
- Two-dimensional array के सभी elements display करने का program लिखिए।
- Matrix के सभी elements का sum निकालने का program लिखिए।
- प्रत्येक row का sum निकालने का program लिखिए।
- प्रत्येक column का sum निकालने का program लिखिए।
- दो matrices को add करने का program लिखिए।
- दो matrices को subtract करने का program लिखिए।
- Matrix का transpose निकालने का program लिखिए।
- दो matrices का multiplication करने का program लिखिए।
- Matrix में largest और smallest element खोजने का program लिखिए।
- Square matrix की main diagonal का sum निकालने का program लिखिए।
- Two-dimensional array में even और odd elements की संख्या ज्ञात करने का program लिखिए।