Multidimensional Arrays In C Language

Multidimensional Arrays:

Multidimensional arrays of any dimension are possible in C but in practice only two or three dimensional arrays are workable. The most common multidimensional array is a two dimensional array for example the computer display, board games, a mathematical matrix etc.

Syntax
type   name [ rows ] [ columns ]  ;

For Example :- 2D array of dimension 2 X 3.

Syntax
int d[ 2 ] [ 3 ] ;
d[0][0] d[0][1] d[0][2]
d[1][0] d[1][1] d[1][2]

A two dimensional array is actually an array of arrays, in the above case an array of two integer arrays (the rows) each with three elements, and is stored row-wise in memory.

For Example :- Program to fill in a 2D array with numbers 1 to 6 and to print it out row-wise.

CODE/PROGRAM/EXAMPLE
#include <stdio.h>
void main( )
{
int i, j, num[2][3] ;

for ( i = 0; i < 2; i++ )
	for ( j = 0; j < 3; j ++ )
		num[i][j] =  i * 3 + j + 1 ;

for ( i = 0; i < 2; i++ )
	{
	for ( j = 0; j < 3; j ++ )
		printf("%d ",num[i][j]  ) ;
	printf("\n" );
	}
}

For Example :- Program to tabulate sin(x) from x = 0 to 10 radians in steps of 0.1 radians.

CODE/PROGRAM/EXAMPLE
#include <stdio.h>
#include <math.h>
void main()
{
int i  ;
double x ;
double table[100][2] ;// we will need 100 data points for
				  // the above range and step size and
// will store both x and f(x)
for ( x = 0.0, i = 0; x < 10.0; x += 0.1, i++ ) {
  table[i][0] = x ;
  table[i][1] = sin( x ) ;
  printf(“
 Sin( %lf ) = %lf”, table[i][0], table[i][1] );
}
}

To initialise a multidimensional array all but the leftmost index must be specified so that the compiler can index the array properly.

Syntax
For Example :-    
  int d[ ] [ 3 ] = { 1, 2, 3, 4, 5, 6 } ;

However it is more useful to enclose the individual row values in curly braces for clarity as follows.

Syntax
int d[ ] [ 3 ] = { {1, 2, 3}, {4, 5, 6} } ;

Arrays of Strings:

An array of strings is in fact a two dimensional array of characters but it is more useful to view this as an array of individual single dimension character arrays or strings.

CODE/PROGRAM/EXAMPLE
For Example :-     
  char str_array[ 10 ] [ 30 ] ;

where the row index is used to access the individual row strings and where the column index is the size of each string, thus str_array is an array of 10 strings each with a maximum size of 29 characters leaving one extra for the terminating null character.

For Example :- Program to read strings into str_array and print them out character by character.

CODE/PROGRAM/EXAMPLE
#include <stdio.h>

char str_array[10][30] ;

void main()
{
int i, j ;

puts("Enter ten strings\n") ;

for ( i = 0 ; i < 10; i++ ) // read in as strings so a single for
 // loop suffices
{
printf( " %d : ", i + 1) ;
gets( str_array[i] ) ;        
}

for ( i = 0; i < 10; i++ )//printed out as individual chars so a
{                      // nested for loop structure is required
  for ( j=0; str_array[i][j] != \0 ; j++ )
	putchar ( str_array[i][j] ) ;
  putchar( "\n" ) ;
}
}
#multidimensional_array_in_c #multidimensional_arrays_in_c #two_dimensional_array_in_c #2_dimensional_array_in_c #syntax_of_multidimensional_array_in_c #Arrays_of_Strings_in_c #

(New page will open, for Comment)

Not yet commented...