Types of Arrays

There are 3 types of arrays.

  • One Dimensionl Array
  • Two Dimensionl Array
  • Multi Dimensionl Array

Two Dimensionl Array:- The simplest form of the Multi Dimensionl Array is the Two Dimensionl Array. A Multi Dimensionl Array is essence a list of One Dimensionl Arrays. To declare a two dimensional integer array of size x, y ypu would write something as follows :

 Data type array_name[x][y];

A two dimensional array call be think as a table which will have x number of rows and y number of columns.

Accessing Two Dimensionl Array Elements:-

An element in Two Dimensionl Array is accessed by using the subscripts, that is rpw index and column index of the array. For example-

 int val = a[2][3];

Example:-

// Program to print the element of Array
#include <stdio.h>
int main()
{
    /*an array with 5 rows and 2 columns*/
    int a[5][2] = {{0,0},{1,2},{2,4},{3,6},{4,8}};
    int i,j;
    /* Output each array elements value*/
    for(i=0; i<5; i++);
    {
      for(j=0; j<2; j++);
      {
        printf("a[%d][%d]=%d",i,j,a[i][j]);
      }
    }
    return 0;
}

Output:-

 a[0][0] = 0
 a[0][1] = 0
 a[1][0] = 1
 a[1][1] = 2
 a[2][0] = 2
 a[2][1] = 4
 a[3][0] = 3
 a[3][1] = 6
 a[4][0] = 4

Multi Dimensionl Array:-  C programming language allows multi dimensionl arrays. Multi Dimensionl Array may be initialized by specifying bracketed values for each row.

Syntax:-

 Data_type array_name[array size1][array size2]...[array sizeN];

Example:-

 int threedim [5][10][15];