create your own

How to work with Multidimensional Array in C Programming

72
rate or flag this page

By rajkishor09


C Programming Array
C Programming Array

C allows array of two or more dimensions and maximum numbers of dimension a C program can have is depend on the compiler we are using. Generally, an array having one dimension is called 1D array, array having two dimensions called 2D array and so on. So in C programming an array can have two or three or four or even ten or more dimensions. More dimensions in an array means more data it can hold and of course more difficulties to manage and understand these arrays. A multidimensional array has following syntax:

Syntax:

type array_name[d1][d2][d3][d4]………[dn];
Where dn is the size of last dimension.

Example:

int table[5][5][20];
float arr[5][6][5][6][5];

In our example array “table” is a 3D (A 3D array is an array of arrays of arrays.) array which can hold 500 integer type elements. And array “arr” is a 5D array which can hold 4500 floating-point elements. Can see the power of array over variable? When it comes to hold multiple values in a C programming, we need to declare several variables (for example to store 150 integers) but in case of array, a single array can hold thousands of values (depending on compiler, array type etc).

Note: To make this multidimensional array example simple I will discuss 3D array for the sake of simplicity. Once you grab the logic how 3D array works then you can handle 4D array or any multidimensional array easily.

How to Declaration and Initialization 3D Array

Before we move to serious programming let's have a look of 3D array. A 3D array can be assumed as an array of arrays of arrays, it is array (collection) of 2D arrays and as you know 2D array itself is array of 1D array. It sounds a bit confusing but don't worry as you will lead your learning on multidimensional array, you will grasp all logic and concept. A diagram can help you to understand this.

3D Array Conceptual View
3D Array Conceptual View
3D array memory map.
3D array memory map.

We can initialize a 3D array at the compile time as we initialize any other variable or array, by default an un-initialized 3D array contains garbage value. Let’s see a complete example on how we can work with a 3D array.

Example of Declaration and Initialization 3D Array

#include<stdio.h>
#include<conio.h>

void main()
{
int i, j, k;
int arr[3][3][3]=   
		{
		    {
			{11, 12, 13},
			{14, 15, 16},
			{17, 18, 19}
		    },
		    {
			{21, 22, 23},
			{24, 25, 26},
			{27, 28, 29}
			},
			{
			{31, 32, 33},
			{34, 35, 36},
			{37, 38, 39}
			},
		};
clrscr();
printf(":::3D Array Elements:::\n\n");
for(i=0;i<3;i++)
{
	for(j=0;j<3;j++)
	{
		for(k=0;k<3;k++)
		{
		printf("%d\t",arr[i][j][k]);
		}
		printf("\n");
	}
	printf("\n");
}
getch();
}

Microsoft Windows 7 Ultimate Microsoft Windows 7 Ultimate
Price: Too low to display
List Price: $319.99
Microsoft Visual Studio 2008 Professional with MSDN Professional Microsoft Visual Studio 2008 Professional with MSDN Professional
Price: $999.99
List Price: $1,199.00
Mac Box Set â (with Snow Leopard) Mac Box Set – (with Snow Leopard)
Price: Too low to display
List Price: $169.00
MobileMe Individual (Updated 2009 Version) MobileMe Individual (Updated 2009 Version)
Price: $60.90
List Price: $99.99
Kaspersky Internet Security 2009 (3 User) Kaspersky Internet Security 2009 (3 User)
Price: $20.88
List Price: $79.95
Microsoft Office 2008 for Mac Home & Student Edition Microsoft Office 2008 for Mac Home & Student Edition
Price: $100.00
List Price: $149.95

C Books for your learning.

C Programming Language (2nd Edition) C Programming Language (2nd Edition)
Price: $39.79
List Price: $60.33
Programming in Objective-C 2.0 (2nd Edition) Programming in Objective-C 2.0 (2nd Edition)
Price: $24.99
List Price: $44.99
Programming in C (3rd Edition) Programming in C (3rd Edition)
Price: $31.46
List Price: $49.99
C Programming: A Modern Approach, 2nd Edition C Programming: A Modern Approach, 2nd Edition
Price: $55.74
C Programming for the Absolute Beginner C Programming for the Absolute Beginner
Price: $15.84
List Price: $29.99
Expert C Programming Expert C Programming
Price: $19.99
List Price: $42.99

So in the above example we have declared multidimensional array and named this integer array as “arr” which can hold 3x3x3 (27 integers) elements. We have also initialized multidimensional array with some integer values.
As I told you earlier that a 3D array is array of 2D array therefore I have divided elements accordingly so that you can get 3D array better and understand it easily. See the C code sample above, line no. 9-13, 14-18 and 19-23, each block is a 2D array and collectively from line no. 2-24 makes a 3D array. You can also assign values to this multidimensional array in other way like this.

int arr[3][3][3] = {11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 24, 25, 26, 27, 28, 29, 31, 32, 33, 34, 35, 36, 37, 38, 39};

This kind of C multidimensional array (3D array) declaration is quite confusing for new C programmers; you cannot guess location of array element by just looking at the declaration. But look at the above multidimensional array example where you can get a clear idea about each element location. For example, consider 3D array as a collection of tables, to access or store any element in a 3D array you need to know first table number then row number and lastly column number. For instance you need to access value 25 from above 3D array. So, first check the table (among 3 tables which table has the value), once you find the table number now check which row of that table has the value again if you get the row no then check column number and you will get the value. So applying above logic, 25 located in table no. 1 row no. 1 and column no. 1, hence the address is arr[1][1][1]. Print this address and you will get the output.

So the conceptual syntax for 3D array stands like this.

data_type array_name[table][row][column];

If you want to store values in any 3D array then first point to table number, row number and lastly to column number.

arr[0][1][2] = 32;
arr[1][0][1] = 49;

Above code is for assigning values at particular location of an array but if you want to store value in continuous location of array then you should use loop. Here is an example using for loop.

#include<stdio.h>
#include<conio.h>

void main()
{
int i, j, k, x=1;
int arr[3][3][3];
clrscr();
printf(":::3D Array Elements:::\n\n");

for(i=0;i<3;i++)
{
	for(j=0;j<3;j++)
	{
		for(k=0;k<3;k++)
		{
		arr[i][j][k] = x;
		printf("%d\t",arr[i][j][k]);
		x++;
		}
		printf("\n");
	}
	printf("\n");
}
getch();
}

I hope this will make concept of multidimensional array a bit easy for you. Feel free to ask your question and doubts.

Thank You

Download source code of above C program examples

Download Link

Visitor's Opinion

Is this tutorial helpful for you?

  • Yes, very much.
  • No, Not at all
  • I know this, but looking for more examples.
See results without voting

Comments

RSS for comments on this Hub

No comments yet.

Submit a Comment

Members and Guests

Sign in or sign up and post using a hubpages account.


optional


  • No HTML is allowed in comments, but URLs will be hyperlinked
  • Comments are not for promoting your hubs or other sites

working