A two-dimensional array is an array that stores data in a two-dimensional structure. It is also sometimes referred to as a matrix, or multidimensional array. This type of array allows for the storage of information within rows and columns which users can access quickly through the use of indices. In this article, we’ll be diving into the specifics of 2D arrays in C. We will explain the benefits, provide an overview on declaring and accessing elements, and explore the utility of 2D arrays with examples.
What is a 2d Array?
A two-dimensional array is an array of arrays. The structure of a 2D array is composed of rows and columns that together form a two-dimensional structure. This type of array is beneficial in situations where you may need to compare data within the same column or row or across multiple columns or rows. Additionally, the two dimensional array is useful when you would like your program to store multiple items along one specific attribute such as price or quantity.
To illustrate this concept, let’s say that you have a gradebook program. When you define a two dimensional array for this program, you could represent each student as an array in a larger collection of arrays (see Figure 1). Each student’s name, grades, and other characteristics could be stored in separate columns and rows. This would give you quick access to a student’s information by simply referencing the row and column associated with their data.
Advantages of 2d Arrays
Using a two dimensional array, as opposed to multiple single dimensional arrays, has multiple advantages. The primary benefit is that by using two dimensions in C, you can store more elements in the same amount of memory. This means that two dimensional arrays can save users memory and program run time, while providing an organized way to store and access data or lists of items.
Another advantage of two dimensional arrays is its ability to process data quickly. When you need to access or traverse through data, 2D arrays can make life easier. For instance, if you need to process data based on columns or rows, or even both, using 2D arrays can save you immense processing time.
Declaring 2d Arrays in C
Declaring a two dimensional array in C can be done in various ways. Generally speaking, you will provide information about the length for each row as well as for each column. For example, below we create a 3×4 array which contains 12 elements:
// declare 3x4 2D array int arr[3][4];
Notice that each dimension is specified within the brackets (which correspond to rows and columns). This can be interpreted as a three row, four column array with 12 total elements. You can also provide initialization values while declaring your array as such:
int arr[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} };
The above two dimensional array will contain each initialized value at the corresponding index. For example: arr[0][3] = 4.
Accessing Elements in a 2d Array
Once you have declared your two dimensional array, you can access each element inside the array by referencing the row and column indexes associated with the element declaration. As an example below we access the fourth element in each row:
int arr[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }; // output element at arr[0][3], arr[1][3], arr[2][3] printf("%d %d %d\n", arr[0][3], arr[1][3], arr[2][3]); // prints 4 8 12
You can also use nested loops to access elements in a 2D array. Nested loops involve positioning a loop inside a loop; the outer loop describes the row index of the array and the inner loop describes the column index. Below we will use a nested loop to access every element found at arr[i][j]:
int arr[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }; // use nested loop to access each element in array for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { printf("%d ", arr[i][j]); } printf("\n"); } // prints // 1 2 3 4 // 5 6 7 8 // 9 10 11 12
By using nested loops, you can easily process each element in a two dimensional array.
Processing Elements in a 2d Array
To process elements in a two dimensional array you can use various methods such as nested loops and multiple pointers. Using nested loops you can iterate through each row and column index of the given array Structure; each time a loop iteration happens in C you can use previously declared logic to process elements within that given location.
For example below we will use nested loops to iterate through each element at arr[i][j]. While doing so we will increase each processed element by 1:
int arr[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }; // use nested loop to increase each element by 1 for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { arr[i][j] += 1; } }
By increasing arr[i][j] by 1 for every iteration we have effectively increased every element in the given two dimensional array by 1.
Examples of Using 2d Arrays
Two dimensional arrays can be used for various purposes depending on user intention. Essentially every program which contains multiple lists of data or which references multiple values in relation to a single attribute can benefit from using two dimensional arrays. Here are three examples:
- Gradebook: As discussed earlier two dimensional arrays are widely used in the educational domain. Here a two dimensional array can store multiple values such as student names and grades.
- Matrix: Here two dimensional arrays prove beneficial when performing matrix operations. By using two dimensions you can store multiple matrix elements while easily referencing each individual element using row and column indexes.
- Map: Two dimensional arrays are commonly used to represent maps within a program. Here each location on the map can be represented by an individual row and column in the array. Through this method users can not only store map data but also quickly access locations simply by referencing row and columns indexes.
Summary
In conclusion, two dimensional arrays are beneficial when storing and accessing elements based on multiple attributes. Through this structure data is more organized and accessible; all elements found in a two dimensional array can be referenced quickly using row and column indexes. Additionally two dimensional arrays provide an efficient way to store multiple elements without taking up too much memory. In this article we discussed how to declare two dimensional arrays in C as well as how to process and access elements found within it.