Monday, 28 June 2010

Two Dimensional Array (2 D)

Definition
Which contain rows and columns (tabular form or matrix form)

Syntax:
DataType Identifier[RowSize][ColumSize];

Example:
int table[5][4];
Initializing:
int a[3][4] = { 
   {0, 1, 2, 3} ,   /*  initializers for row indexed by 0 */
   {4, 5, 6, 7} ,   /*  initializers for row indexed by 1 */
   {8, 9, 10, 11}   /*  initializers for row indexed by 2 */

Accessing
Displaying
Example Program



1) wap to read an 2d array and display */
#include<conio.h>
#include<stdio.h>
void main()
{
 int a[2][3];
/*int a[3][4] = { 
   {0, 1, 2, 3} ,   /*  initializers for row indexed by 0 */
   {4, 5, 6, 7} ,   /*  initializers for row indexed by 1 */
   {8, 9, 10, 11}   /*  initializers for row indexed by 2 */
};
*/
 int i,j;
 printf("enter the elements in 2d array\n");
 for(i=0;i<=2-1;i++)
 {
  for(j=0;j<=3-1;j++)
  {
   scanf("%d",&a[i][j]);
  }
 }
 printf("2d array is\n");
 for(i=0;i<=2-1;i++)
 {
  for(j=0;j<=3-1;j++)
  {
   printf("%d\t",a[i][j]);
  }
  printf("\n");
 }
 getch();
}

// 2 D Initialization
#include<conio.h>
#include<stdio.h>
void main()
{
 int a[2][3];
int a[3][4] = { 
   {0, 1, 2, 3} ,   /*  initializers for row indexed by 0 */
   {4, 5, 6, 7} ,   /*  initializers for row indexed by 1 */
   {8, 9, 10, 11}   /*  initializers for row indexed by 2 */
};

 int i,j;
 printf("2d array is\n");
 for(i=0;i<=2-1;i++)
 {
  for(j=0;j<=3-1;j++)
  {
   printf("%d\t",a[i][j]);
  }
  printf("\n");
 }
 getch();
}

No comments:

Post a Comment