×

Search anything:

Two dimensional (2D) array in C

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

A 2D array is the simplest form of multi-dimensional array. A 2D array is a collection of homogeneous elements, where the elements are ordered in a number of rows and columns.

It is a collection of rows and columns which is known as a matrix. The data/elements are stored in tabular form.

In this article, we have explored 2D arrays in C along with following sub-topics:

  • Declaring 2D array
  • Calculating size, inserting elements and updating 2D array
  • Convert 3D array to 2D array and using malloc to define 2D arrays in C

Declaring a 2D array

The synatx for declaring a 2D array is:

Synatx:

data_type array_name[number of rows][number of columns];

datatype: datatype represents the type of values to be stored in the array.
i.e;integer, float, double etc.
array_name: name of the array.
[number of rows]: represents the total number of rows in an array.
[number of columns]: represents the total number of columns in an array.

Example:

int arr[3][3];    //rows:3|columns:3

10 20 30    
40 50 60
70 80 90
        3x3

Here, we have an array of type int which means the array will hold integer values.
Name of the array is arr and the number of rows and columns is 3.

We know that in case of 1D array it is not necessary to specify the size of the array when we declare it.But in case of 2D array you need to specify the second dimension even if the elements are specified during its declaration.

int arr[3][3]={1,2,3,4,5,6,7,8,9};    //valid declaration

int arr[][3]={1,2,3,4,5,6,7,8,9};     //valid declaration

int arr[][]={1,2,3,4,5,6};            //invalid->'must specify second dimension'

int arr[3][]={1,2,3,4,5,6,7,8,9};     //invalid->'must specify second dimension'

Calculating the size of a 2D array/ multi-dimensional array

The total number of elements in a multi-dimensional array can be easily calculated by multiplying the dimensions.

Example:

int arr[3][3];
rows=3
columns=3
total elements=rows*columns
can store total(3*3)=9 elements

int arr[10][20];
rows=10
columns=20
can store total(10*20)=200 elements

Inserting values in 2D array

In 2D array, if a user want to enter the values/elements then two for loops are used.

  • First for loop represents the number of rows.
  • Second for loop represents the number of columns.

Example:

#include<stdio.h>
int main()
{
	int arr[3][3];                     //2D array declaration
	int i,j;
	printf("enter the elements: \n");  //entering elements
	for(i=0;i<3;i++)
	{
		for(j=0;j<3;j++)
		{
			printf("elements at [%d][%d]: ",i,j);
			scanf("%d",&arr[i][j]);
		}
	}
	printf("\nprinting the elements of a 2D array: \n");
	for(i=0;i<3;i++)
	{
		for(j=0;j<3;j++)
		{
			printf("%d ",arr[i][j]);
			if(j==2)
			printf("\n");
		}
	}
	return 0;
}

OUTPUT

enter the elements:
elements at [0][0]: 10
elements at [0][1]: 20
elements at [0][2]: 30
elements at [1][0]: 40
elements at [1][1]: 50
elements at [1][2]: 60
elements at [2][0]: 70
elements at [2][1]: 80
elements at [2][2]: 90

printing the elements of a 2D array:
10 20 30
40 50 60
70 80 90

In the above program;

  • We have declared two variables i,j for two for loops.
  • We have declared an array of type integer int arr[3][3];(rows:3 columns:3).
  • First section of nested for loops ask the user to insert the values.
  • second section of nested for loops will print the inserted values in the matrix form.

Updating the 2D array

We can update the elements of 2D array either by specifying the element to be replaced or by specifying the position where replacment has to be done.
For updating the array we require,

  • Elements of an array
  • Element or position, where it has to be inserted
  • The value to be inserted

Example:

#include<stdio.h>
int main()
{
	int arr[3][3];                     //2D array declaration
	int i,j;                           //variables for 'for loops'
	int num;                           //number to be updated
	printf("enter the elements: \n");  //entering elements
	for(i=0;i<3;i++)
	{
		for(j=0;j<3;j++)
		{
			printf("elements at [%d][%d]: ",i,j);
			scanf("%d",&arr[i][j]);
		}
	}
	printf("\nprinting the elements of an array: \n");
	for(i=0;i<3;i++)
	{
		for(j=0;j<3;j++)
		{
			printf("%d ",arr[i][j]);
			if(j==2)
			printf("\n");
		}
	}
	printf("\nenter the location in a matrix where you want to update value: ");
	scanf("%d %d",&i,&j);   
	printf("\nenter the value to be replaced: ");
	scanf("%d",&num);
	arr[i][j]=num;                      //number assigned to particular position in array
	printf("\narray after updating: \n");
	for(i=0;i<3;i++)
	{
		for(j=0;j<3;j++)
		{
			printf("%d ",arr[i][j]);
			if(j==2)
			printf("\n");
		}
	}
	return 0;
}

OUTPUT:

enter the elements:
elements at [0][0]: 10
elements at [0][1]: 20
elements at [0][2]: 30
elements at [1][0]: 40
elements at [1][1]: 50
elements at [1][2]: 60
elements at [2][0]: 77
elements at [2][1]: 80
elements at [2][2]: 90

printing the elements of an array:
10 20 30
40 50 60
77 80 90

enter the location in a matrix where you want to update value: 2 0

enter the value to be replaced: 70

array after updating:
10 20 30
40 50 60
70 80 90

In the above program;

  • We have declared two variables i,j for two for loops and num variable which will hold the value/element to be updated.
  • We have declared an array of type integer int arr[3][3];(rows:3 columns:3)
  • First section of nested for loops ask the user to insert the values.
  • second section of nested for loops will print the inserted values in the matrix form.
  • Position/value will be updated.
  • Third section of nested for loop will print the updated 2D array.

Converting 2D array into 3D array

We can also convert a 2D array into a 3D array by following given steps;

  • Declare a 2D array and enter the elements in it and can print it.
  • Now declare a 3D array and copy the elements of 2D array into 3D array and print them.

Example

#include<stdio.h>
int main()
{
	int i,j,k;
	int a[3][3];                    //2D array declaration
	int b[3][3];
	printf("enter the elements in array a: \n");        //entering elements in array 'a'   
	for(i=0;i<3;i++)
	{
		for(j=0;j<3;j++)
		{
			printf("element at [%d][%d]: ",i,j);
			scanf("%d",&a[i][j]);
		}
	}
	printf("\nprinting 2D array a\n");                 //printing elements of array 'a'
	for(i=0;i<3;i++)                        
	{   
		for(j=0;j<3;j++)
		{
			printf("%d ",a[i][j]);	
		}
		printf("\n");
	}
	printf("\nenter the elements in array b: \n");     //entering elements in array 'b' 
	for(i=0;i<3;i++)
	{
		for(j=0;j<3;j++)
		{
			printf("element at [%d][%d]: ",i,j);
			scanf("%d",&b[i][j]);
		}
	}
	printf("\nprinting 2D array b\n");                 //printing elements of array 'b'
	for(i=0;i<3;i++)                        
	{   
		for(j=0;j<3;j++)
		{
			printf("%d ",b[i][j]);	
		}
		printf("\n");
	}
	int arr[3][3][3];                                  //3D array declaration
	printf("\ncopying values in 3D array: \n");
	for(i=0;i<2;i++)
	{   
		for(j=0;j<3;j++)                 
		{
			for(k=0;k<3;k++)
			{
				if(i==0)
				{
					arr[i][j][k]=a[j][k];              //copying elements of 2D array into 3D array
				}
				else
				{
					arr[i][j][k]=b[j][k];
				}
			}
		}
	}
	printf("\nprinting elements in 3D array: \n");    //printing elements of 3D array
	for(i=0;i<2;i++)
	{   
		for(j=0;j<3;j++)                 
		{
			for(k=0;k<3;k++)
			{
			printf("%d ",arr[i][j][k]);
			if(k==2)
			{
			printf("\n");
			}
			}
		}
		printf("\n");
	}
	return 0;				
}

OUTPUT

enter the elements in array a:
element at [0][0]: 10
element at [0][1]: 20
element at [0][2]: 30
element at [1][0]: 40
element at [1][1]: 50
element at [1][2]: 60
element at [2][0]: 70
element at [2][1]: 80
element at [2][2]: 90

printing 2D array a
10 20 30
40 50 60
70 80 90

enter the elements in array b:
element at [0][0]: 11
element at [0][1]: 22
element at [0][2]: 33
element at [1][0]: 44
element at [1][1]: 55
element at [1][2]: 66
element at [2][0]: 77
element at [2][1]: 88
element at [2][2]: 99

printing 2D array b
11 22 33
44 55 66
77 88 99

copying values in 3D array:

printing elements in 3D array:
10 20 30
40 50 60
70 80 90

11 22 33
44 55 66
77 88 99

Converting 3D array into 2D array

We can convert a 3D array into a 2D array. As we know that a 3D array is a colection of 2D arrays, we just have to follow certain steps to convrt a 3D array into 2D array;

  • First declare a 3D array and enter the elements in it.
  • After that, declare the 2D arrays(number of 2D arrays should be equal to the total number of blocks of 3D array).
  • Copy the elements of 3D array into 2D array.

Example:

#include<stdio.h>
int main()
{
	int i,j,k;               //variables for nested for loop
	int arr[2][3][3];        //declaration of 3D array
	printf("enter the elements: \n");
	for(i=0;i<2;i++)
	{
		for(j=0;j<3;j++)
		{
			for(k=0;k<3;k++)
			{
				printf("element at [%d][%d][%d]: ",i,j,k);
				scanf("%d",&arr[i][j][k]);
				//arr[i][j][k]=++ctr;
			}
		}
	}
	printf("\nprinting 3D array\n");
	for(i=0;i<2;i++)                        //printing 3d array
	{   
		for(j=0;j<3;j++)
		{
			for(k=0;k<3;k++)
			{
				printf("%d ",arr[i][j][k]);	
			}
			printf("\n");
		}
		printf("\n");
	}
	int a[3][3];                    //2D array declaration
	int b[3][3];
	printf("\ncopying values in new 2D array: \n");
	for(i=0;i<2;i++)
	{   
		for(j=0;j<3;j++)                 
		{
			for(k=0;k<3;k++)
			{
				if(i==0)
				{
					a[j][k]=arr[i][j][k];    //copying values in new 2d array
				}
				else
				{
					b[j][k]=arr[i][j][k];
				}
			}
		}
	}
	printf("\nprinting elements in first 2D array: \n");
	for(i=0;i<3;i++)
	{   
		for(j=0;j<3;j++)                 //printing first 2d array
		{
			printf("%d ",a[i][j]);
		}
		printf("\n");
	}
	printf("\n");
	printf("\nprinting elements in second 2D array: ");
	for(i=0;i<3;i++)
	{   
		for(j=0;j<3;j++)                //printing 2nd 2d array
		{
			printf("%d ",b[i][j]);
		}
		printf("\n");
	}
	return 0;				
}

OUTPUT:

enter the elements:
element at [0][0][0]: 10
element at [0][0][1]: 20
element at [0][0][2]: 30
element at [0][1][0]: 40
element at [0][1][1]: 50
element at [0][1][2]: 60
element at [0][2][0]: 70
element at [0][2][1]: 80
element at [0][2][2]: 90
element at [1][0][0]: 11
element at [1][0][1]: 22
element at [1][0][2]: 33
element at [1][1][0]: 44
element at [1][1][1]: 55
element at [1][1][2]: 66
element at [1][2][0]: 77
element at [1][2][1]: 88
element at [1][2][2]: 99

printing 3D array:
10 20 30
40 50 60
70 80 90

11 22 33
44 55 66
77 88 99


copying values in 2D array:
printing elements in first 2D array:
10 20 30
40 50 60
70 80 90

printing elements in second 2D array:
11 22 33
44 55 66
77 88 99

Dynamically allocating memory using malloc in 2D array

As we know that static array variables are fixed in size and can't be changed(enlarged or shrinked).To remove this drawback we use dynamic memory allocation.dynamic array is nothing but it is allocated during runtime with malloc or calloc.

syntax:

int *array=int(int *)malloc(sizeof(int)*element-count);

Example

#include<stdio.h>       //malloc library
#include<malloc.h>      //command line argument
int main(int argc, char* argv[])
{
  int row,column;      //variables for rows and columns
  int ***arr;
  int i,j;             //variables for 'for loop'
  printf("enter the number of rows and column: ");
  scanf("%d %d",&row,&column);
  arr=(int **)malloc(sizeof(int **)*row);
  for(i=0;i<row;i++)
  {
    arr[i]=(int *)malloc(sizeof(int)*column);
  }
  for(i=0;i<row;i++)
  {
    for(j=0;j<column;j++)
	{
    printf("element at: [%d][%d] :",i,j);
    scanf("%d",&arr[i][j]);
    }
  }
  printf ("Printing 2D Array:\n");
  for(i=0;i<row;i++)
  {
    for(j=0;j<column;j++)
	{
        printf("%.2d ",arr[i][j]);
    }
    printf("\n");
  }
  return 0;
}

OUTPUT:

enter the number of rows and column: 3 3
element at: [0][0] :10
element at: [0][1] :20
element at: [0][2] :30
element at: [1][0] :40
element at: [1][1] :50
element at: [1][2] :60
element at: [2][0] :70
element at: [2][1] :80
element at: [2][2] :90
Printing 2D Array:
10 20 30
40 50 60
70 80 90

With this article at OpenGenus, you must have a complete idea of 2D arrays in C. Enjoy.

Two dimensional (2D) array in C
Share this