It is a collection of data elements of same data type arranged in rows and columns (that is, in two dimensions).
Type arrayName[numberOfRows][numberOfColumns];
For example,
int Sales[3][5];
An two-dimensional array can be initialized along with declaration. For two-dimensional array initialization, elements of each row are enclosed within curly braces and separated by commas. All rows are enclosed within curly braces.
int A[4][3] = {{22, 23, 10},
{15, 25, 13},
{20, 74, 67},
{11, 18, 14}};
To access the elements of a two-dimensional array, we need a pair of indices: one for the row position and one for the column position. The format is as simple as:
name[rowIndex][columnIndex]
Examples:
printf("%d", A[1][2]); /* print an array element */
A[1][2]=13; /* assign value to an array element */
scanf("%d", &A[1][2]); /* input element */
#include <stdio.h>
#define MAX_ROWS 10
#define MAX_COLS 10
int main()
{
int array[MAX_ROWS][MAX_COLS];
int rows, cols;
// Prompt the user to enter the dimensions of the array
printf("Enter the number of rows (max %d): ", MAX_ROWS);
scanf("%d", &rows);
printf("Enter the number of columns (max %d): ", MAX_COLS);
scanf("%d", &cols);
// Input loop to populate the array
printf("Enter elements of the array:\n");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
printf("Enter element at position (%d, %d): ", i + 1, j + 1);
scanf("%d", &array[i][j]);
}
}
// Display the entered 2D array
printf("\nEntered 2D Array:\n");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
printf("%d ", array[i][j]);
}
printf("\n");
}
return 0;
}
Output
Enter the number of rows (max 10): 3 Enter the number of columns (max 10): 4 Enter elements of the array: Enter element at position (1, 1): 1 Enter element at position (1, 2): 2 Enter element at position (1, 3): 3 Enter element at position (1, 4): 4 Enter element at position (2, 1): 5 Enter element at position (2, 2): 6 Enter element at position (2, 3): 7 Enter element at position (2, 4): 8 Enter element at position (3, 1): 9 Enter element at position (3, 2): 10 Enter element at position (3, 3): 11 Enter element at position (3, 4): 12 Entered 2D Array: 1 2 3 4 5 6 7 8 9 10 11 12
Two-dimensional arrays can be passed as parameters to a function, and they are passed by
reference. When declaring a two-dimensional
array as a formal parameter, we can omit the size of the first dimension, but not the
second; that is, we must specify the number of columns. For example:
void print(int A[][3],int n, int m)
In order to pass to this function an array declared as:
int arr[4][3];
we need to write a call like this:
print(arr);
Here is a complete example:
#include <stdio.h>
void input(int matrix[][10], int, int);
void display(int matrix[][10], int, int);
int main()
{
int r, c;
int A[10][10];
printf("Enter number of rows: ");
scanf("%d", &r);
printf("Enter number of columns: ");
scanf("%d", &c);
input(A, r, c);
printf("\n");
display(A, r, c);
return 0;
}
void input(int matrix[][10], int m, int n)
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("Enter data in [%d][%d] : ", i, j);
scanf("%d", &matrix[i][j]);
}
}
}
void display(int matrix[][10], int m, int n)
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
}
Output:
Enter number of rows: 3
Enter number of columns: 4
Enter data in [0][0] : 10
Enter data in [0][1] : 11
Enter data in [0][2] : 12
Enter data in [0][3] : 13
Enter data in [1][0] : 18
Enter data in [1][1] : 19
Enter data in [1][2] : 40
Enter data in [1][3] : 35
Enter data in [2][0] : 33
Enter data in [2][1] : 25
Enter data in [2][2] : 16
Enter data in [2][3] : 56
10 11 12 13
18 19 40 35
33 25 16 56