Updated March 16, 2023
Introduction to 2-D Arrays in C
Arrays can be defined as collection of elements or data that are of similar or different data types, which is implemented in one or more dimensions with respect to the requirement provided to the program developer. 2-D or two dimensional array are represented as ‘datatype variable[n][n]’, where datatype can be an int, char, etc, and the [n][n] is n*n to represent the position of the value of the variable in the array. A few basic operations necessary for all the two dimensional array are ‘initializing the array’, ‘inserting the value in the array’, ‘updating the value in the array’, and ‘deleting a value from the array’. In this article will see about 2-D Arrays in C.
Concepts in 2-D Arrays in C
We can define arrays in
- Single-Dimensional
- Double-Dimensional
And so on up to N-Dimensional based upon the requirement. But here we are going to deal with 2-D Arrays. As the name suggests, 2-D Arrays can be a matrix representation of data, which are created to implement a relational database lookalike data structure and can be stored in tabular forms. It provides ease of holding the bulk data which can be passed to any number of functions based on the requirement. The data in these arrays can be accessed through the row and column ids.
How can we define and implement them? Where can we use them? Going further, let’s understand those concepts.
In C, Dimensional arrays can be declared as follows:
Syntax
So, in the same way, we can declare the 2-D array as:
The meaning of the above representation can be understood as:
- The memory allocated to variable b is of data type int.
- The data is being represented in the form of 2 rows and 3 columns.
The data inside the array can be accessed through the above representation. In 2-D arrays representation, the first square bracket represents the number of rows, and the second one is for the number of columns. The index representation of the array for the first element always starts with zero and ends with size-1. Array variable (here b) always holds the base address of the memory block and is called an internal pointer variable.
So, for example, if the number of rows is 3, then the index representation for accessing the data in rows will be 0, 1 and 2. The same logic applies to the column indexes too. For the above representation, to get the data of the 2nd row 3rd column, we can access by b[1][2].
Initializing Arrays
We have two different methods in initializing the values in C. The methods only differ syntactically.
Below is one of them.
Another way of initializing is as follows:
Generally, the first method of initialization is preferred as we can clearly understand and visualize the rows and columns of 2-D Arrays in C.
Below is the example for the pictorial representation of elements and their address for array b.
The elements of an array are usually stored in consecutive memory locations based upon the data type of the elements.
Inserting Elements in 2-D Arrays
For inserting elements in 2-D Arrays, we need to insert the data in both rows and columns. So, for this, we use the concept of loops. In the above process for initializing the data in an array, we had predefined the values.
Here, elements can be dynamically inserted by the user, as per the requirements. Below is an example code for inserting the elements.
#include <stdio.h>
int main()
{
int b[2][3];
int i,j,num;
printf("Enter elements into 2-D array: ");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
scanf("%d" , &b[i][j]);
}
}
}
As observed in the code:
- First, we are declaring the array variable and the dimensions of the array with the number of rows and columns.
- We are then declaring two variables for iterating over the elements in the array.
- Then, for loops are used. The outside for loop is for the rows iteration and the inside loop is for the columns.
- Scanf function is used to read the data as we input, and then place the value inserted at those positions of i and j.
In the above example, we inserted the data in a matrix having 2 rows and 3 columns. The output of the following can be obtained as below:
As we have not used the printf function to display the output, the program written had only read the user inputted values. After writing the print function (using for loops), the output would be displayed as:
Update Elements in 2-D Arrays
The updating of elements in an array can be done by either specifying a particular element to be replaced or by identifying a position where the replacement has to be done. For updating, we generally require the following details.
- Elements of an array
- Position/element, where it has to be inserted
- The value to be inserted.
For updating the data in an array through element details, first, we need to search for that element in the array, understand it’s the position, and then replace the old element with the new element.
Here, we have given below two examples of updating the element of a 2-D array.
Firstly, let us go through an example where the position of the element to be updated is already known.
#include <stdio.h>
int main()
{
int b[2][3];
int i,j,num;
printf("Enter elements into 2-D array: ");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
scanf("%d" , &b[i][j]);
}
}
b[0][2]=10;
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("\t%d" , b[i][j]);
}
printf("\n");
}
return 0;
}
In the above program, the element on the 1st row and 3rd column are selected and the value of the data in that position has been updated.
Output for above is as follows:
In the second example, we are going to show how the position of the element can be dynamically taken as a user inputted value and update the value of the element at that particular position.
#include <stdio.h>
int main()
{
int b[2][3];
int i,j,num;
printf("Enter elements into 2-D array: ");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
scanf("%d" , &b[i][j]);
}
}
printf("Enter the value of row and coulmn number :");
scanf("%d %d", &i,&j);
printf("Enter the number you want to update with: ");
scanf("%d" , &num);
b[i][j]=num;
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("\t%d" , b[i][j]);
}
printf("\n");
}
return 0;
}
Here, we used the scanf function to read the value given by the user as per their choice for the position of an element based on row and column numbers.
The output is as follows:
As an exercise, can you try writing a program in updating the whole row of the matrix with user-inputted values?
Now, as we know, in the 2-D array, we declare the size of array at the beginning itself. We are aware of the size of array but what if the user gives a random row and column number outside our array size?
Notice that as we had not written any if/else condition or try/catch blocks, the output of the matrix does not change. However, we can write the code using the above-mentioned conditions to display errors for such cases.
Deleting Elements in 2-D Arrays
After the concepts of insertion and updating of the data inside the array, let’s now see how we can delete an entire row from the array.
We have written a program in a simple format so that the concept of different operations in a 2-d array can be understood easily.
#include <stdio.h>
int main()
{
int b[2][3],i,j,num,x;
printf("Enter elements into 2-D array: ");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
scanf("%d" , &b[i][j]);
}
}
printf("Enter the value of row number :");
scanf("%d", &x);
for(i=0;i<2;i++)
{
if(i==x)
{
for(j=0;j<3;j++)
{
if((i+1)<2)
{
printf("\t%d" , b[i+1][j]);
}
}
i++;}
else
{
for(j=0;j<3;j++)
{
printf("\t%d" , b[i][j]);
}
}
printf("\n");
}
}
The steps followed are:
- Took the values of an array dynamically
- Asked the user to input the number (index) of the row that has to be deleted.
- Using for loop iteration, we are comparing if the row number and the user input number are matching or not.
- If they are matching and if the row number is less than the size of an array, we are printing the next row. Else, we are printing the row as it is.
The output is as follows:
What if, I give the row number outside the array boundary?
It will not find the row to delete and exit the program by printing the whole array.
As already known, we can even declare the values for the row and column numbers dynamically and write the program accordingly.
Doesn’t this look simple and easy to learn?
As an exercise, can you try in deleting a particular element for the 2-d array now?
Conclusion
In this section, we have learned the basic operations on 2-dimensional arrays. These 2-d arrays are useful in real-time with matrix operations and many mathematical calculations.
Arrays can even be used in displaying calendars, placements of the parking lot, and we can even have a chess game.
Many other data structures like Linked lists, Queue, Graphs, Trees have to use this concept of 2-D arrays as the basic requirement in storing and accessing the locations of different elements. Try solving the basic operations of the 2d arrays and have fun learning C.
Recommended Articles
This is a guide to 2-D Arrays in C. Here we discuss the Introduction, Initializing Arrays, Inserting, Updating, Deleting Elements in a 2-D Arrays. You may also look at the following articles to learn more –