TWO DIMENSIONAL ARRAY
INTRODUCTION
2-D ARRAY DECLARATION
NEED OF 2-D ARRAY
INITIALIZATION OF 2-D ARRAY
VALID/INVALID DECLARATIONS
READ AND ACCESS 2-D ARRAY ELEMENTS
GUESS THE OUTPUT
MEMORY REPRESENTATION OF 2-D ARRAY
PROGRAMS ON 2-D ARRAY
Two Dimensional Arrays
A two dimensional array is defined as an array which requires two
indices/subscripts to represent an element.
Let us understand it ! 2-D Array is also known as an array of arrays.
0 1
0 1 2 3 4 5 6 7 0 1 2 3 0 1 2 3
a 23 45 32 78 89 98 30 40 b 23 45 32 78 89 98 30 40
a[0] =23 b[0] [0] =23
a[1] =45 2-D Array b[0] [1] =45
1-D Array
a[2] =32 b[0] [2] =32
//2-D Array Declaration
a[3] =78 //1-D Array Declaration b[0] [3] =78 data_type arr_name[row_size][column_size];
data_type arr_name[size];
a[4] =89 int a[8]; b[1][0] =89 int b[2][4];
a[5] =98 b[1][1] =98
a[6] =30 b[1][2] =30
a[7] =40 b[1][3] =40
What is the need of Two Dimensional Array ?
Two Dimensional array provides the way to store data in the form of
grids and tables.
int roll, marks, age, fee, pin, mob, pan, id; roll[0] [0] = 201
roll[0] [1] = 202
Int roll[8]; roll 201 202 203 204 205 206 207 208 roll[0] [2] = 203
0 1 2 roll[0] [3] = 204
Int roll[3][4]; 0 1 2 3 0 1 2 3 0 1 2 3 roll[1] [0] = 401
roll 201 202 203 204 401 402 403 404 601 602 603 604 roll[1] [1] = 402
2002 2004 2006 2008 2010 2012 2014 2016 2018 2020 2022 2024 roll[1] [2] = 403
0 1 2 3 roll[1] [3] = 404
2-D Array
0 201 202 203 204
roll[2] [0] = 601
Int roll [5][4][70]; roll[2] [1] = 602
1 401 402 403 404
roll[2] [2] = 603
Int roll [10][5][4][70]; 2 601 602 603 604
roll[2] [3] = 604
Initialization of 2-D Array
int a[3][3] = {10, 20, 30, 40, 50, 60, 70, 80, 90};
0 1 2 0 1 2
0 10 20 30
0 1 2 0 1 2 0 1 2
1 40 50 60
a 10 20 30 40 50 60 70 80 90
2 70 80 90
2002 2004 2006 2008 2010 2012 2014 2016 2018
// 2nd Method: a[0] [0] = 10
a[0] [1] = 20
int a[3][3] = { a[0] [2] = 30
a[1] [0] = 40
{10, 20, 30}, a[1] [1] = 50
{40, 50, 60}, a[1] [2] = 60
a[2] [0] = 70
{70, 80, 90} a[2] [1] = 80
a[2] [2] = 90
};
Legal/Illegal Declarations of 2-D Array
Linear Array:
int a[5]; //Valid Declaration
int a[ ]; //Invalid Declaration
int a[5] = {5, 4, 3, 2, 1}; //Valid Declaration
int a[ ] = {5, 4, 3, 2, 1}; //Valid Declaration
2-D Array:
int a[2][4]; //Valid Declaration
int a[ ][ ]; //Invalid Declaration
int a[2][4] = {10, 20, 30, 40, 50, 60, 70, 80}; //Valid Declaration
int a[ ][ ] = {10, 20, 30, 40, 50, 60, 70, 80}; //Invalid Declaration
int a[ ][4] = {10, 20, 30, 40, 50, 60, 70, 80}; //Valid Declaration
int a[2][ ] = {10, 20, 30, 40, 50, 60, 70, 80}; //Invalid Declaration
Access Elements of 2-D Array: (Read and Display)
Read: a 10 20 30 40 50 60 70 80
int a[row][column],
a[2][4]; i, j; a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3]
i<2; i++)
for(i=0; i<row; i++) i=0, j=0 i j
a[0] [0] = 10
{ a[0] [1] = 20
for(j=0;
for(j=0; j<column;
j<4; j++) j++) a[0] [2] = 30
a[0] [3] = 40
{{ i=1, j=0 i j
scanf(“%d”, &a[i][j]);
scanf(“%d”, &a[i][j]);
a[1] [0] = 50
a[1] [1] = 60
} a[1] [2] = 70
}
} a[1] [3] = 80
}
Access Elements of 2-D Array: (Read and Display)
Display: a 10 20 30 40 50 60 70 80
int a[row][column],
int a[2][4]; i, j; a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3]
for(i=0; i<row;
for(i=0; i<2; i++)
i++) i=0, j=0 i j
a[0] [0] = 10
{{ a[0] [1] = 20
for(j=0;
for(j=0; j<column;
j<4; j++) j++) a[0] [2] = 30
a[0] [3] = 40
{{ i=1, j=0 i j
printf(“%d”,
printf(“%d”, a[i][j]);
a[i][j]); a[1] [0] = 50
a[1] [1] = 60
}
} a[1] [2] = 70
} a[1] [3] = 80
}
Guess the Output !
Linear Array:
int a[5]; for(i=0;i<5;i++) { printf(“%d ”, a[i]); }
int a[7] = {1, 2, 3}; for(i=0;i<7;i++) { printf(“%d ”, a[i]); }
int a[5] = {}; for(i=0;i<5;i++) { printf(“%d ”, a[i]); }
int a[5] = {1, 2, 3, 4, 5, 6, 7}; for(i=0;i<5;i++) { printf(“%d ”, a[i]); }
int a[ ] = {1, 2, 3}; for(i=0;i<5;i++) { printf(“%d ”, a[i]); }
2-D Array: for(i=0; i<2; i++)
int a[2][3]; {
int a[2][3] = {10, 20, 30, 40, 50, 60}; for(j=0; j<3; j++)
{
int a[ ][3] = {10, 20, 30, 40, 50, 60};
printf(“%d”, a[i][j]);
int a[2][3] = {10, 20}; }
int a[ ][3] = {10, 20}; }
int a[ ][ ] = {10, 20, 30};
Memory Representation of 2-D Array Linear Array
LOC(A[k])=BA + w(k – LB)
Row Major Order: In row-major order, the elements of an array are stored
consecutively along the row in memory. Suppose an array,
a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2] a[2][0] a[2][1] a[2][2]
a 10 20 30 40 50 60 70 80 90 a[3][3] = { {10, 20, 30},
2002 2004 2006 2008 2010 2012 2014 2016 2018 {40, 50, 60},
Address calculation in row major order {70, 80, 90},
LOC(A[j][k])=Base(A)+w[N(j-Row_lowerbound)+(k-column_lowerbound)]
};
LOC(A[1][2]) =2002 + 2(3(1-0) + (2-0) = 2002 + 2(3+2) = 2002 + 10 = 2012
Column Major Order: In column-major order, the elements of an array are
stored consecutively along the column in memory.
Base(A)--->Base Address of Array
a[0][0] a[1][0] a[2][0] a[0][1] a[1][1] a[2][1] a[0][2] a[1][2] a[2][2] w---> Size of Data Type
a 10 40 70 20 50 80 30 60 90
M---> No. of Rows
2002 2004 2006 2008 2010 2012 2014 2016 2018
N---> No. of Columns
Address calculation in column major order
j---> jth index
LOC(A[j][k])=Base(A)+w[M(k-column_lowerbound)+(j-row_lowerbound)]
LOC(A[1][2]) = 2002 + 2(3(2-0) + (1-0) = 2002 + 2(6+1) = 2002 + 14 = 2016 k---> kth index
PROGRAMS ON 2-D Array
READ AND PRINT ELEMENTS OF A MATRIX
SUM AND PRODUCT OF ALL ELEMENTS OF MATRIX
SUM OF ALL ELEMENTS OF EACH ROW OF A MATRIX
TRANSPOSE OF A MATRIX
PRINT DIAGONAL ELEMENTS AND SUM OF DIAGONAL
ELEMENTS OF A MATRIX
ADDITION AND SUBTRACTION OF TWO MATRICES
MULTIPLICATION OF TWO MATRICES
a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3]
TRANSPOSE OF MATRIX 21 22 43 44 41 42 23 24 51 52 33 84
The matrix obtained by Interchanging rows and columns of original matrix is
known as transpose of that matrix.
Transpose of Matrix (n X m)
Original Matrix (m X n)
0 1 2
0 1 2 3
0 21 41 51
0 21 22 43 44
Interchange Row 1 22 42 52
1 41 42 23 24
and Columns 2 43 23 33
2 51 52 33 84
3 44 24 84
for(i=0; i<3; i++) for(i=0; i<4; i++)
{ {
for(j=0; j<4; j++) for(j=0; j<3; j++)
{ {
printf(“%d\t”, a[i][j]); printf(“%d\t”, a[j][i]);
} }
printf(“\n”); printf(“\n”);
} }
DIAGONAL OF MATRIX
• Diagonal of matrix exists only if the matrix is square matrix.
Principal Diagonal
• There are two diagonals of a matrix.
Secondary Diagonal
0 1 2 3 0 1 2 3 0 1 2 3
0 21 41 51 47 0 21 0 47
1 22 42 52 97 1 42 1 52
2 43 23 33 99 2 33 2 23
3 44 24 84 77 3 77 3 44
Matrix A
m=n Principal Diagonal Secondary Diagonal
i=j i+j=n-1
i.e. A[0][0], A[1][1], A[2][2], A[3][3] i.e. A[0][3], A[1][2], A[2][1], A[3][0]
SUBTRACTION OF TWO MATRICES
SUM OF TWO MATRICES
• Two
TwoMatrcies
Matrciescan
can
bebe added only
subtracted only ifif both
bothmatrices
matricesareare of same
of same order.
order.
• Corresponding
Corresponding Elements will will
Elements be subtracted.
be added.
0 1 2 3 0 1 2 3 0 1 2 3
0 1 5 3 15 0 11 15 5 7 0 -10
12 -10
20 -2
8 8
22
1
2
3
2
3
8
7
9
18
10
39 12
13
17
8
+- 1
2
3
18
14
4
12
2
11 12
9
3
8
4
30
= 1
2
3
-16
20
-11
17
4
12
-5
19
7
11
28
50
9
27
7
13
0
24
5
21
13
21
-22
38
Matrix A Matrix B
Matrix C
C[i][j] = A[i][j] +- B[i][j]
B[i][j]
MULTIPLICATION OF TWO MATRICES
• Two Matrcies can be multiplied only if number of columns of first
matrix are equal to number of rows of second matrix.
• i.e. Multiplication, (m x n) X (p x q) = (m x q) is possible only if n=p.
for example, (2 x 3) X (3 x 4) = (2 x 4).
0 1 2 0 1 2 0 1 2
=
0 1 5 2 0 2 9 1 0 38 34 30
1
2
2
3 9
7 18
10
X 1
2
4
8
1
10
5
2
1
2
176
122
205
136 68
73
Matrix A Matrix B Matrix C
C=AXB
= =A[2][0]
C[0][0]
C[1][0]
C[2][0] A[0][0]
A[1][0] + +A[2][1]
X B[0][0]
X B[0][0] A[0][1]
A[1][1] + +A[2][2]
X B[1][0]
X B[0][1]
B[1][0] A[0][2]
A[1][2] X B[2][0] = =(3
X B[2][0] (2 (1 X 2)
X 2) + (5
+ (9
(7 X 4)
X 4) + (2XX8)8) ==622++36
+ (10
(18 20++80
20 16==122
16 38
176
= =A[2][0]
C[0][1]
C[1][1]
C[2][1] A[0][0]
A[1][0] + +A[2][1]
X B[0][1]
X B[0][1] A[0][1]
A[1][1] + +A[2][2]
X B[1][1]
X B[1][1] A[0][2]
A[1][2] X B[2][1] = =(3X
X B[2][1] (2 X(19)
9)X++9)(9
(7+XX(51)
1)X++1)(10
+ (2
(18 X10)
XX10)10)==27
=189+++975+++100
20 ==34
180 205
136
= =A[2][0]
C[0][2]
C[1][2]
C[2][2] A[0][0]
A[1][0] + +A[2][1]
X B[0][2]
X B[0][2] A[0][1]
A[1][1] + +A[2][2]
X B[1][2]
X B[1][2] A[0][2]
A[1][2] X B[2][2] = = (3
X B[2][2] (2 (1 X 1)
X 1) + (5
(7
+ (9 X 5)
X 5) + (2XX2)2) = =32 1+ +45
(18
+ (10 3525+ +20
364==68
30
73
PROGRAM IMPLEMENTATION
•Lets do it in Compiler.