Write about Two Dimensional Arrays?


Two Dimensional Array is the combination of similar single dimensional array’s contains rows and columns. We can represent the data in Two Dimensional Arrays as table format.

Two Dimensional Arrays are declared as follows:

Syntax: Type array_name [row-size] [column-size];

Ex : int x[3] [4];

Now x is an array of 3 elements and each element is gain an array of 4 elements.

The first index selects the row and 2nd index select the column with in that row.

Insertion of row and column is a cell.

We can represent particular value in a table by using two subscripts i & j.

As with the single dimensional arrays each of the array is indexed from zero to its max size minus one.

INITIALIZING Two Dimensional Arrays:

Two Dimensional Arrays may be initialized follow their declaration with a list of initial values enclosed in braces.

Ex: int table [2][3]= {0.0,0,1,1,1};

Initializes the elements of the first row to zero and second row to one. The initialization is done row by row.

We can also initialize a Two Dimensional Arrays in the form of matrix as shown below:

int table [2][3]={{0,0,0},
                         {1,1,1}
                        };

When the arrays is completely initialized with all values. Explicitly, we need not specify the size of the first dimension. i.e

int table [ ] [3]={
                        {0,0,0},
                        {1,1,1}
                        };

If the values are missing in an initialization, they are automatically set to zero.

Ex: int table [2][3]= {{1,1},{2}};

Will initialize the first 2 elements of the first row to one, the first element of the 2nd row to two and all other elements to zero.

When all the elements are to be initialized to zero, the following short –cut method may be used.

 int m[2][5]={{0},{0},{0}};

The first element of each row is explicitly initialized to zero while other elements are automatically initialized to zero.


READING ELEMENTS INTO Two Dimensional Arrays:

printf (“enter number of rows & columns”);
scanf (“%d%d”, & n, & m);
printf (“enter the elements in to an array);
for ( i =0; i {
for (j =0; j < m; j++)
{
scanf (“%d”,x[i][j]);
}
}

WAP to read the order of matrix and read elements into Two Dimensional Arrays and display the elements in matrix format

#include
#include
main ( )
{
int x[10][10],i, j, m, n;
clrscr ( );
printf (“enter order of matrix”);
scanf (“%d% d”,& n, &m);
for ( i =0; < n; i++)
{
for (j =0; j < m, j++)
{
scanf (“%d”,&x[i][j]);
}
}
printf(“\nelements in the matrix \n” );
for ( i =0; i < n; i++)
{
for (j=0; j < m; j++)
{
printf (“\t %d”, x[i][j]);
}
printf (“\n”);
}
getch ( );
}

Related

C Language 2948910537461360179

Post a Comment

emo-but-icon

item