Nearby lessons

41 of 72

C - Two Dimensional Array

A two-dimensional array stores values in rows and columns, like a table. In this tutorial, you will learn how to declare, initialize, access, and display a 2D array using simple C programs.

What Is a Two-Dimensional Array?

A two-dimensional array (2D array) stores multiple values in rows and columns.

Think of a classroom table with 2 rows and 3 columns. Each position stores one value.

Example:

10 20 30
40 50 60

Here, the array contains 2 rows and 3 columns.

Example01
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int numbers[2][3] = {
6 {10, 20, 30},
7 {40, 50, 60}
8 };
9 
10 printf("%d", numbers[0][0]);
11 
12 return 0;
13}
Output
10

Syntax of a Two-Dimensional Array

The basic syntax is:

data_type array_name[rows][columns];
  • data_type: Type of values, such as int or float.
  • array_name: Name of the array.
  • rows: Number of rows.
  • columns: Number of columns.

For example, int numbers[2][3]; creates an array with 2 rows and 3 columns. It can store 6 integer values.

Example02
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int numbers[2][3];
6 
7 printf("2D array declared successfully.");
8 
9 return 0;
10}
Output
2D array declared successfully.

Initializing a Two-Dimensional Array

We can assign values to a 2D array when declaring it. Each pair of inner braces represents one row.

Example03
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int numbers[2][3] = {
6 {10, 20, 30},
7 {40, 50, 60}
8 };
9 
10 printf("%d\n", numbers[0][0]);
11 printf("%d\n", numbers[0][1]);
12 printf("%d\n", numbers[1][2]);
13 
14 return 0;
15}
Output
10
20
60

Accessing Array Elements

We use two indexes to access a 2D array element:

array_name[row_index][column_index]

Remember:

  • The first row has index 0.
  • The first column has index 0.
  • numbers[0][1] means the first row and second column.
  • numbers[1][2] means the second row and third column.
Example04
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int numbers[2][3] = {
6 {10, 20, 30},
7 {40, 50, 60}
8 };
9 
10 printf("First row, second column: %d\n", numbers[0][1]);
11 printf("Second row, third column: %d", numbers[1][2]);
12 
13 return 0;
14}
Output
First row, second column: 20
Second row, third column: 60

Updating Array Elements

We can change an element by assigning a new value to its row and column indexes.

Example05
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int numbers[2][2] = {
6 {10, 20},
7 {30, 40}
8 };
9 
10 numbers[0][1] = 100;
11 
12 printf("%d", numbers[0][1]);
13 
14 return 0;
15}
Output
100

Displaying Elements Using Nested Loops

A nested loop means using one loop inside another loop.

  • The outer loop handles the rows.
  • The inner loop handles the columns.

This method helps us display all elements of a 2D array.

Example06
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int numbers[2][3] = {
6 {10, 20, 30},
7 {40, 50, 60}
8 };
9 
10 int i, j;
11 
12 for (i = 0; i < 2; i++)
13 {
14 for (j = 0; j < 3; j++)
15 {
16 printf("%d ", numbers[i][j]);
17 }
18 
19 printf("\n");
20 }
21 
22 return 0;
23}
Output
10 20 30
40 50 60

Taking Input from the User

We can use nested loops and scanf() to accept values from the user.

Here, the user enters 4 values for an array containing 2 rows and 2 columns.

Example07
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int numbers[2][2];
6 int i, j;
7 
8 printf("Enter 4 numbers:\n");
9 
10 for (i = 0; i < 2; i++)
11 {
12 for (j = 0; j < 2; j++)
13 {
14 scanf("%d", &numbers[i][j]);
15 }
16 }
17 
18 printf("Array elements:\n");
19 
20 for (i = 0; i < 2; i++)
21 {
22 for (j = 0; j < 2; j++)
23 {
24 printf("%d ", numbers[i][j]);
25 }
26 
27 printf("\n");
28 }
29 
30 return 0;
31}
Output
Enter 4 numbers:
10 20 30 40
Array elements:
10 20
30 40

Finding the Sum of All Elements

We can use nested loops to add all elements of a 2D array.

Example08
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int numbers[2][2] = {
6 {10, 20},
7 {30, 40}
8 };
9 
10 int i, j, sum = 0;
11 
12 for (i = 0; i < 2; i++)
13 {
14 for (j = 0; j < 2; j++)
15 {
16 sum = sum + numbers[i][j];
17 }
18 }
19 
20 printf("Sum = %d", sum);
21 
22 return 0;
23}
Output
Sum = 100

Adding Two Matrices

Matrix addition means adding the values at the same positions in two matrices.

For example:

First matrix:     Second matrix:
1 2               5 6
3 4               7 8

Result:
6  8
10 12

Both matrices must have the same number of rows and columns.

Example09
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int a[2][2] = {
6 {1, 2},
7 {3, 4}
8 };
9 
10 int b[2][2] = {
11 {5, 6},
12 {7, 8}
13 };
14 
15 int sum[2][2];
16 int i, j;
17 
18 for (i = 0; i < 2; i++)
19 {
20 for (j = 0; j < 2; j++)
21 {
22 sum[i][j] = a[i][j] + b[i][j];
23 }
24 }
25 
26 printf("Result matrix:\n");
27 
28 for (i = 0; i < 2; i++)
29 {
30 for (j = 0; j < 2; j++)
31 {
32 printf("%d ", sum[i][j]);
33 }
34 
35 printf("\n");
36 }
37 
38 return 0;
39}
Output
Result matrix:
6 8
10 12

Common Mistakes

  • Wrong index: Remember that array indexes start from 0.
  • Wrong loop condition: For 2 rows, use i < 2.
  • Missing index: Use both row and column indexes, such as numbers[0][1].
  • Wrong array size: Do not access elements outside the declared rows and columns.
  • Missing ampersand: Use &numbers[i][j] with scanf().
📝 Key Takeaways
  • A 2D array stores values in rows and columns.
  • Syntax: data_type array_name[rows][columns].
  • Array indexing starts from 0.
  • The outer loop handles rows.
  • The inner loop handles columns.
  • Nested loops are used to process 2D arrays.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4