Multi dimensional arrays in C language

Introduction

अब तक हमने single (one-dimensional) arrays देखे, जिनमें data एक line (list) की तरह store होता है।

लेकिन कई real-life problems ऐसी होती हैं जहाँ data को rows और columns के form में store करना होता है, जैसे:

  • matrix (2D table)
  • marks of students (rows = students, columns = subjects)
  • tables और grids

ऐसी situations में एक ही dimension वाला array sufficient नहीं होता।

इसीलिए C language में Multi-dimensional Arrays का उपयोग किया जाता है।

Multi-dimensional array basically array के अंदर array होता है।

सबसे common type है:

  • 2D array (rows × columns)

Definition

Multi-dimensional array वह array होता है जिसमें data multiple dimensions (जैसे rows और columns) में store किया जाता है।

2D Array (Two-Dimensional Array)

Syntax

data_type array_name[row][column];

Example

int a[2][3];

Explanation:

  • 2 rows
  • 3 columns
  • total elements = 6

Memory Representation

IndexValue
a[0][0]first element
a[0][1]second element
a[1][2]last element

Initialization of 2D Array

int a[2][3] = {
{1, 2, 3},
{4, 5, 6}
};

Example 1: Matrix Input and Output

#include <stdio.h>
int main()
{
int a[2][2], i, j; // input
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
scanf("%d", &a[i][j]);
}
} // output
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
} return 0;
}

Explanation:

  • outer loop → rows control करता है
  • inner loop → columns control करता है

Example 2: Matrix Addition

#include <stdio.h>
int main()
{
int a[2][2] = {{1, 2}, {3, 4}};
int b[2][2] = {{5, 6}, {7, 8}};
int c[2][2];
int i, j;
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
c[i][j] = a[i][j] + b[i][j];
}
} // output
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
printf("%d ", c[i][j]);
}
printf("\n");
}
return 0;
}

Output:

6 8
10 12

Explanation:

  • corresponding elements add हुए
  • result new matrix में store हुआ

Example 3: Matrix Transpose

#include <stdio.h>
int main()
{
int a[2][2] = {{1, 2}, {3, 4}};
int i, j;
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
printf("%d ", a[j][i]);
}
printf("\n");
}
return 0;
}

Output:

1 3
2 4

Explanation:

  • rows और columns swap हुए

Example 4: Sum of All Elements

#include <stdio.h>
int main()
{
int a[2][2] = {{1, 2}, {3, 4}};
int i, j, sum = 0;
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
sum += a[i][j];
}
} printf("Sum = %d", sum);
return 0;
}

Output:

Sum = 10

Technical Understanding

  • Multi-dimensional arrays matrix operations में use होते हैं
  • nested loops जरूरी होते हैं
  • row-major order में memory store होती है

Important Points

  • matrix programs exam में बहुत important हैं
  • Index हमेशा 0 से start होता है
  • 2D array = rows + columns
  • nested loop required

Important Points

  • Index हमेशा 0 से start होता है
  • 2D array = rows + columns
  • nested loop required
  • matrix programs exam में बहुत important हैं

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top