Insertion and deletion in 2D arrays in C Programming – Everything you want to know!

                      The insertion and deletion in a multidimensional array can be performed by using the indices of the cell. That is in other words a multidimensional array can be inserted or delete values by accessing the cells or the memory blocks present in it.

Introduction:

                      The insertion and deletion in a multidimensional array can be performed by using the indices of the cell. That is in other words a multidimensional array can be inserted or delete values by accessing the cells or the memory blocks present in it.

Insertion:

                               A multidimensional array is just an array with more than one row and one column. So the multidimensional array can be accessed via the row number and the column number. Those are also known as indices. The indices are unique for each of the elements or the variables in the array. The array stores the values in an orderly fashion. In other words, the values of the array are stored continuously in the memory. By using this principle we can insert the values anywhere in the array. That is as long as we swap the existing value either forward or backward.

Example :

Consider the array : a[2][2]={1,2,3,4} of structure  2×2.
Array a, is a two-dimensional array with 2 rows and 2 columns. The rows and columns can be plotted as given below.

By using the visual representation of the array, we can see that the row numbers start from 0 to 1 and the column starts from 0 to 1. 

Syntax to insert values at the end of the array:

a[1][1]=5;

Here, the value is inserted at the position of 1 st row and 1st column, and hence the previous value would’ve been deleted.

To delete a value from the 2D array:

Consider we have to remove the value of 1 from the array all we have to do is move the elements right to it forwards.

a[0][1]=a[1][0];

a[1][0]=a[1][1];

This will change the array into :

As you might be able to see, there is a duplication of the value 4 in the array. To eliminate the duplication we can easily just delete the last element, which is duplicated. To do that we have to change the value to NULL.

a[1][1]=NULL;

Now the array stands :

Conclusion :

            To do both insertion and deletion there is an easy way, that is looping which we will see in later articles. You can view all our articles on C programming here.

References :

https://www.geeksforgeeks.org/multidimensional-arrays-c-cpp/
https://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm
https://www.programiz.com/c-programming/c-multi-dimensional-arrays

Default image
Karan
I'm Karan Dharmalingam, Co-founder of Criss Cross Tamizh. My responsibilities in Criss Cross Tamizh include Head Content Writer and Editor, Video Producer, and Editor. I have always believed that my best work is in the content creation field. The journey of CCT was almost like a bar joke. While talking to my bestie Arun Kumar, he jokingly said how cool it would be if we can have our own websites and YouTube channels. And I also felt the same and right at that moment we started a YouTube channel and it's almost 2 years and we have come a very long way both in business and as humans as well. Thanks for reading nanba/nanbi !!
Articles: 4

Leave a Reply