Array in C Language: 2D Array, Matrix Addition, Multiplication, Inverse and Transpose with Programs
Array in C Language
Welcome to Online Guru 2020!
आज हम C Programming Language में Array के बारे में विस्तार से सीखेंगे। इस tutorial में हम Array की Definition, Types of Array और Two Dimensional Array (2D Array) को आसान उदाहरणों के साथ समझेंगे।
इसके साथ ही हम Matrix के चार महत्वपूर्ण Operations के C Programs भी सीखेंगे:
- Addition of Two Matrices
- Multiplication of Two Matrices
- Inverse of a Matrix
- Transpose of a Matrix
1. What is an Array in C?
Definition: An array is a collection of similar types of data elements stored in continuous memory locations under a single variable name.
आसान भाषा में: Array एक ऐसा variable है जिसमें हम एक ही प्रकार के कई data elements को एक ही नाम से store कर सकते हैं।
Example of Array
int a[5] = {10, 20, 30, 40, 50};
यहाँ:
- int = Data Type
- a = Array Name
- 5 = Array Size
- 10, 20, 30, 40, 50 = Array Elements
2. Types of Array in C
C Language में मुख्य रूप से Array के तीन प्रकार होते हैं:
- One Dimensional Array (1D Array)
- Two Dimensional Array (2D Array)
- Multi Dimensional Array
3. Two Dimensional Array (2D Array)
Definition: A two-dimensional array is an array that stores data in rows and columns.
आसान भाषा में: 2D Array में data को Rows और Columns के रूप में store किया जाता है। इसे Matrix भी कहा जाता है।
Example of 2D Array
int a[2][2] = {
{1, 2},
{3, 4}
};
इस Matrix में 2 Rows और 2 Columns हैं।
| 1 | 2 |
|---|---|
| 3 | 4 |
यहाँ a[0][0] = 1, a[0][1] = 2, a[1][0] = 3 और a[1][1] = 4 है।
4. Addition of Two Matrices
Definition: Matrix Addition means adding the corresponding elements of two matrices.
आसान भाषा में: दो Matrix के समान Row और Column वाले elements को आपस में जोड़ना Matrix Addition कहलाता है।
Matrix A:
1 2
3 4
Matrix B:
5 6
7 8
Result:
6 8
10 12
C Program: Addition of Two Matrices
#include <stdio.h>
int main()
{
int a[2][2], b[2][2], c[2][2];
int i, j;
printf("Enter first matrix:\n");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("Enter second matrix:\n");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
scanf("%d", &b[i][j]);
}
}
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
printf("Addition of Matrix:\n");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
printf("%d ", c[i][j]);
}
printf("\n");
}
return 0;
}
Output
Enter first matrix: 1 2 3 4 Enter second matrix: 5 6 7 8 Addition of Matrix: 6 8 10 12
5. Multiplication of Two Matrices
Definition: Matrix Multiplication means multiplying the rows of the first matrix with the columns of the second matrix and adding their products.
आसान भाषा में: पहले Matrix की Row को दूसरे Matrix के Column से Multiply करके उनके products का जोड़ करते हैं।
Matrix A:
1 2
3 4
Matrix B:
5 6
7 8
Calculation:
First element = (1×5) + (2×7) = 19
Second element = (1×6) + (2×8) = 22
Third element = (3×5) + (4×7) = 43
Fourth element = (3×6) + (4×8) = 50
Result:
19 22
43 50
C Program: Multiplication of Two Matrices
#include <stdio.h>
int main()
{
int a[2][2], b[2][2], c[2][2];
int i, j, k;
printf("Enter first matrix:\n");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("Enter second matrix:\n");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
scanf("%d", &b[i][j]);
}
}
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
c[i][j] = 0;
for(k=0; k<2; k++)
{
c[i][j] += a[i][k] * b[k][j];
}
}
}
printf("Multiplication of Matrix:\n");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
printf("%d ", c[i][j]);
}
printf("\n");
}
return 0;
}
Output
Multiplication of Matrix: 19 22 43 50
6. Inverse of a Matrix
Definition: The inverse of a square matrix is a matrix which, when multiplied by the original matrix, gives the Identity Matrix.
आसान भाषा में: किसी Matrix का Inverse वह Matrix होता है जिसे Original Matrix से Multiply करने पर Identity Matrix प्राप्त होता है।
Formula for 2 × 2 Matrix
मान लीजिए:
[ c d ]
Determinant = ad - bc
A⁻¹ = 1/(ad-bc) × [ d -b ]
[ -c a ]
यहाँ ad-bc को Determinant कहते हैं।
Example
मान लीजिए:
A = [2 1]
[1 1]
Determinant = (2 × 1) - (1 × 1) = 1
इसलिए इसका Inverse होगा:
A⁻¹ = [1 -1]
[-1 2]
C Program: Inverse of a 2 × 2 Matrix
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c, d, det;
printf("Enter four elements of matrix:\n");
scanf("%f%f%f%f", &a, &b, &c, &d);
det = (a*d) - (b*c);
if(fabs(det) < 0.000001)
{
printf("Inverse does not exist.");
}
else
{
printf("Inverse of Matrix:\n");
printf("%.2f %.2f\n", d/det, -b/det);
printf("%.2f %.2f\n", -c/det, a/det);
}
return 0;
}
Input
2 1 1 1
Output
Inverse of Matrix: 1.00 -1.00 -1.00 2.00
7. Transpose of a Matrix
Definition: Transpose of a matrix is obtained by interchanging its rows and columns.
आसान भाषा में: Matrix की Row को Column और Column को Row में बदलना Transpose कहलाता है।
Example
Original Matrix:
1 2
3 4
Transpose Matrix:
1 3
2 4
C Program: Transpose of a Matrix
#include <stdio.h>
int main()
{
int a[2][2], i, j;
printf("Enter matrix elements:\n");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("Transpose of Matrix:\n");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
printf("%d ", a[j][i]);
}
printf("\n");
}
return 0;
}
Input
1 2 3 4
Output
Transpose of Matrix: 1 3 2 4
8. Difference Between Matrix Operations
| Operation | Meaning |
|---|---|
| Addition | Corresponding elements को जोड़ना। |
| Multiplication | Row × Column करके जोड़ना। |
| Inverse | Matrix का Multiplicative Inverse निकालना। |
| Transpose | Rows को Columns में बदलना। |
9. Practice Questions for Students
अब आप इन Programs को खुद लिखने की कोशिश करें:
- Write a C program to add two 3×3 matrices.
- Write a C program to multiply two 3×3 matrices.
- Write a C program to find the inverse of a 2×2 matrix.
- Write a C program to find the transpose of a 3×3 matrix.
- Write a C program to find the sum of all elements of a 2D array.
10. Conclusion
इस Tutorial में हमने C Language के Array और Two Dimensional Array के बारे में सीखा। इसके साथ ही Matrix Addition, Matrix Multiplication, Matrix Inverse और Matrix Transpose के C Programs को Examples और Outputs के साथ समझा।
हमें उम्मीद है कि यह Tutorial आपके लिए उपयोगी होगा। नियमित अभ्यास से आप C Programming में Array और Matrix Operations को आसानी से सीख सकते हैं।
Thanks for visiting Online Guru 2020.
Related Topics
- Introduction to C Programming
- Operators in C Language
- Looping Statements in C
- Functions in C Language
- One Dimensional Array in C
Share this article with your friends and classmates.


Welcome to OnlineGuru2020.blogspot.com, your number one source for all things related to Online Education. We're dedicated to providing you the very best of Digital Education, with an emphasis on E-notes,Download Links in PDF and youtube videos etc
OnlineGuru2020 Founded in [2020] by Sapan Kumar Das (Asst. Proff Ramchandi College Saraipali), OnlineGuru2020.blogspot.com has come a way from its beginnings .When I first started, my passion inspired me to start my own blog, seeing the need for students in lockdown2020
We hope you enjoy our products as much as we enjoy offering them to you. If you have any questions or comments, please don't hesitate to contact us.
Contact Datails:-
Mobile Number:-9826026747
Email:-sapam.online@gmail.com
Sincerely,
Sapan Kumar Das
.