Array (2dim 3dim)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Two-Dimensional Arrays

–an array of arrays


- a good example to illustrate a two-dimensional
array is a MATRIX w/c is a group of lists, or arrays
that are organized into one data set

syntax:
data-type arrayName[row][col];

Example:
int score[2][3];
Graphical Representation Analogy
Column 0 1 2
10 80 30
0
40 50 90
Row 1
Our two dimensional array score[r][c] is an integer
data type and it ca hold only an integer data with a
maximum of 6 values ([2] x [3]).

➢The row 2 and a maximum of 6 is simply called


2 by 3 (multiplied by 3).

➢The index-number inside the square brackets


are part of the variable name.
Here is the individual value of array variable
score[2][3].
score[0][0] = 10
score[0][1] = 80
score[0][2] = 30
score[1][0] = 40
score[1][1] = 50
score[1][2] = 90

The first index-umber of two dimensional arrays


specifies the maximum row(r), while the second
index-number specifies the maximum column(c).
The value of each array is in between the
intersection of row and column.
The following table illustrates how the
subscripts are specified for this array, w/c
has five (5) rows and four (4) columns.

0 1 2 3
0 [0][0] [0][1] [0][2] [0][3]
1 [1][0] [1][1] [1][2] [1][3]
2 [2][0] [2][1] [2][2] [2][3]
3 [3][0] [3][1] [3][2] [3][3]
4 [4][0] [4][1] [4][2] [4][3]
To initialize these values in the
program:
Column 0 1 2
0 10 80 30
Row 1 40 50 90

int x[2][3]={10,80,30},{40,50,90}};
main ()
{//Declaring an Array; Intialization of 2D Array
int array2D[2][3]={{10,80,30},{40,50,90}};
int i,j;
printf("\n\nA Two Dimensional Array\n\n");
for (j=0; j<2; j++)
{
for (i = 0; i<3; i++)
{// Displaying the Array
printf ("%d ",array2D[j][i]); }
printf("\n");
}
}

You might also like