2D ARRAY IN C
“We also call 2D array as a two-dimensional array.In 2D Array we arrange a set of data in a grid of rows and columns.” These are arrays of arrays. Each array element being an array in and of itself. An array with two dimensions is called a 2D array in C. These are imagined as a two-dimensional plane with rows and columns arranged in it.
This is a few characteristics of 2D arrays:
- A pair of dimensions rows and columns make up the two dimensions of a 2D array. Each element of array has a unique row and column index location.
- These arrays are in rectangular shape. It’s row and column have the same number of rows and columns.
- Rows are progressively stored in memory and that the elements of a row are stored next to one another in memory.
- The elements of this array are kept in contiguous memory regions.
- For identify a particular placement of element the array these arrays can be accessed via row and column indices.
- These can be specified as either static or dynamic:
- Dynamic arrays are enlarged at runtime by utilizing memory allocation procedures like malloc and realloc.
- Static arrays have a fixed size that is defined at build time. Integers, floats, characters and more complicated data structures like structs or objects are stored in these.
SYNTAX FOR DECLARING 2D ARRAY
data_type array_name[number_of_rows][number_of_columns];
Example: 1
We show a two-dimensional (2D) array of integers having three rows and four columns below:
int arr[3][4];
This creates an array with the name arr. Itcan hold three rows and four columns of integers.
Here is an example program.
It creates a 2D array in C also initializes it, and prints its contents:
#include <stdio.h>
int main( ) {
// Now Declare and initialize a 2D array
int arr[3][3]= {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
// Here wePrint the array's contents
for( int i = 0; i < 3; i++) {
for(int j= 0; j < 3; j++){
printf("%d ", arr[i][j]) ;
}
printf("\n");
}
return 0;
}
In this program, a two-dimensional array (arr) having three rows and three columns is declared and initialized. The printf function is used to cycle through each element of the array with two nested loops. We then return 0 to show the program was successful in running.
Example 2:-
#include <stdio.h>
int main( ) {
// Declare a 2D array with three rows and three columns…
int arr[3][3] ;
// Here Assign values to the array elements
arr[0][0] = 1; arr[0][1] = 2, arr[0][2] = 3;
arr[1][0] = 4, arr[1][1] = 5; arr[1][2] = 6;
arr[2][0] = 7; arr[2][1] = 8, arr[2][2] = 9;
//Now we Print the array's contents
for(int i = 0; i < 3; i++) {
for (int j= 0; j < 3; j++) {
printf("%d ", arr[i][j]) ;
}
printf("\n");
}
return 0;
}
In this program, we declare the 2D array arr. It has three rows and three columns, we use the row and column indices of the array elements to access them. Then we assign values to them. we use two nested loops. Finally with printf function to output the contents of the array.
INITIALIZING 2D ARRAY
The following C example program initializes a 2D array and prints its contents:
#include <stdio.h>
int main( ) {
// To Initialize a 2D array with three rows and three columns….
int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
//To Print the array's contents
for (int i = 0; i < 3; i++) {
for (int j= 0; j < 3; j++)
{
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}
ACCESSING 2D ARRAY
Example:
#include <stdio.h>
int main( ) {
// Declare a 2D array [three rows and three columns]
int arr[3][3];
// Assigning values
arr[0][0] = 1; arr[0][1] = 2;arr[0][2] = 3;
arr[1][0] = 4;arr[1][1] = 5; arr[1][2] = 6;
arr[2][0] = 7; arr[2][1] = 8; arr[2][2] = 9;
// Accessing to print the value of a specific element
int element = arr[1][2];
// Row index 1 (second row), column index 2 (third column)
printf("The value of arr[1][2] is: %d\n", element);
return 0;
}
Here We declare the 2D array arr .It has three rows and three columns. Then we use the row and column indices of the array elements. we access them and assign values to them. The arr array and printf function are used to access. Then we print the value of a particular element (the element in the second row and third column).
USES OF 2D ARRAY
In Multiple programming languages we use 2D arrays.These have a wide range of applications. Here we have a few frequent applications for 2D arrays:
- Table or grid representation: we use2D arrays to represent data in the shape of tables or grids. Data may be stored in a table with a 2D array in spreadsheet programs.
- Image processing: Images are frequently shown as 2D arrays of pixels, where each pixel corresponds to a point in the image and has a unique color value.
- Matrices: 2D arrays can be used to represent the mathematical entities known as matrices. Calculus and linear algebra are mathematical applications.
- Games: Game boards or game states can be represented using these.For instance, 2D array might be used by a chess program to represent the locations of the pieces on the board.
- Graphs: Each member of these array can be used to represent an edge connecting two vertices in a graph. In network analysis and graph theory, this is frequently utilized.
In general, 2D arrays are a flexible and strong data format that may be applied in various ways.