2D-Array
Prepared By: Mr. Sangram A. Patil
Assistant Professor PVPIT,Budhgaon
Two Dimensional Array
 2D Array is called as Matrix
 Requires pair of square brackets.
 Declaration Syntax
Datatype Array_name[size][size];
Example:
int m[2][3];
float m2[4][4];
 m is 2D array of integer type having subscript each of capacity.
 m is array of having two rows and three columns.
Two Dimensional Array
 D array m declared as two arrays with each have three columns.
[0][0] [0][1] [0][2]
[1][0] [1][1] [1][2]
Col 0 Col 1 Col 2
Row 0
Row 1
 Array reserves certain amount of memory.
m[0][0] m[0][1] m[0][2] m[1][0] m[1][1] M[1][2]
m
Initialization of 2D array
 Assigning initial values to multidimensional array.
 Rules:
1. Last subscript (right most) increase most rapidly.
2. First subscript (left most) increase least rapidly.
 Elements are assigned to 2d array row wise.
 Example:
int m[2][3]={10,20,30,40,50,60};
or
int m[2][3]={{10,20,30},{40,50,60}};
10 20 30
40 50 60
0 1 2
0
1
 Memory map
10 20 30 40 50 60
[0][0] [0][1] [0][2] [1][0] [1][1] [1][2]
m
Matrix addition
10 20 30
40 50 60
70 80 90
5 6 7
8 9 10
11 12 13
15 26 37
48 59 70
81 92 103
0
1
2
0 1 2
A B C
+ =
C[0][0]=A[0][0]+B[0][0]
C[0][1]=A[0][1]+B[0][1]
C[0][2]=A[0][2]+B[0][2]
C[1][0]=A[1][0]+B[1][0]
C[1][1]=A[1][1]+B[1][1]
C[1][2]=A[1][2]+B[1][2]
C[2][0]=A[2][0]+B[2][0]
C[2][1]=A[2][1]+B[2][1]
C[2][2]=A[2][2]+B[2][2]
Matrix Multiplication
10 20 10
4 5 6
2 3 5
3 2 4
3 3 9
4 4 2
130 120 240
51 47 73
35 33 45
x =
C[0][0]=A[0][0]*B[0][0]+A[0][1]*B[1][0]+A[0][2]*B[2][0]
C[0][1]=A[0][0]*B[0][1]+A[0][1]*B[1][1]+A[0][2]*B[2][1]
C[0][2]=A[0][0]*B[0][2]+A[0][1]*B[1][2]+A[0][2]*B[2][2]
C[1][0]=A[1][0]*B[0][0]+A[1][1]*B[1][0]+A[1][2]*B[2][0]
C[1][1]=A[1][0]*B[0][1]+A[1][1]*B[1][1]+A[1][2]*B[2][1]
C[1][1]=A[1][0]*B[0][2]+A[1][1]*B[1][2]+A[1][2]*B[2][2]
C[2][0]=A[2][0]*B[0][0]+A[2][1]*B[1][0]+A[2][2]*B[2][0]
C[2][1]=A[2][0]*B[0][1]+A[2][1]*B[1][1]+A[2][2]*B[2][1]
C[2][0]=A[2][0]*B[0][2]+A[2][0]*B[1][2]+A[2][2]*B[2][2]

Two dimensional Array

  • 1.
    2D-Array Prepared By: Mr.Sangram A. Patil Assistant Professor PVPIT,Budhgaon
  • 2.
    Two Dimensional Array 2D Array is called as Matrix  Requires pair of square brackets.  Declaration Syntax Datatype Array_name[size][size]; Example: int m[2][3]; float m2[4][4];  m is 2D array of integer type having subscript each of capacity.  m is array of having two rows and three columns.
  • 3.
    Two Dimensional Array D array m declared as two arrays with each have three columns. [0][0] [0][1] [0][2] [1][0] [1][1] [1][2] Col 0 Col 1 Col 2 Row 0 Row 1
  • 4.
     Array reservescertain amount of memory. m[0][0] m[0][1] m[0][2] m[1][0] m[1][1] M[1][2] m
  • 5.
    Initialization of 2Darray  Assigning initial values to multidimensional array.  Rules: 1. Last subscript (right most) increase most rapidly. 2. First subscript (left most) increase least rapidly.  Elements are assigned to 2d array row wise.  Example: int m[2][3]={10,20,30,40,50,60}; or int m[2][3]={{10,20,30},{40,50,60}}; 10 20 30 40 50 60 0 1 2 0 1
  • 6.
     Memory map 1020 30 40 50 60 [0][0] [0][1] [0][2] [1][0] [1][1] [1][2] m
  • 7.
    Matrix addition 10 2030 40 50 60 70 80 90 5 6 7 8 9 10 11 12 13 15 26 37 48 59 70 81 92 103 0 1 2 0 1 2 A B C + = C[0][0]=A[0][0]+B[0][0] C[0][1]=A[0][1]+B[0][1] C[0][2]=A[0][2]+B[0][2] C[1][0]=A[1][0]+B[1][0] C[1][1]=A[1][1]+B[1][1] C[1][2]=A[1][2]+B[1][2] C[2][0]=A[2][0]+B[2][0] C[2][1]=A[2][1]+B[2][1] C[2][2]=A[2][2]+B[2][2]
  • 8.
    Matrix Multiplication 10 2010 4 5 6 2 3 5 3 2 4 3 3 9 4 4 2 130 120 240 51 47 73 35 33 45 x = C[0][0]=A[0][0]*B[0][0]+A[0][1]*B[1][0]+A[0][2]*B[2][0] C[0][1]=A[0][0]*B[0][1]+A[0][1]*B[1][1]+A[0][2]*B[2][1] C[0][2]=A[0][0]*B[0][2]+A[0][1]*B[1][2]+A[0][2]*B[2][2] C[1][0]=A[1][0]*B[0][0]+A[1][1]*B[1][0]+A[1][2]*B[2][0] C[1][1]=A[1][0]*B[0][1]+A[1][1]*B[1][1]+A[1][2]*B[2][1] C[1][1]=A[1][0]*B[0][2]+A[1][1]*B[1][2]+A[1][2]*B[2][2] C[2][0]=A[2][0]*B[0][0]+A[2][1]*B[1][0]+A[2][2]*B[2][0] C[2][1]=A[2][0]*B[0][1]+A[2][1]*B[1][1]+A[2][2]*B[2][1] C[2][0]=A[2][0]*B[0][2]+A[2][0]*B[1][2]+A[2][2]*B[2][2]