2 DArray
2 DArray
1
Imagine……
Class of 5 students
Each student is enrolled in 3 subjects
CSE115, ENG101, EEE101
Store the marks of all students in all three
subjects…
2
Solution (kind of!)……
int m1[3] = { 78, 83, 82 };
int m2[3] = { 90, 88, 94 };
int m3[3] = { 71, 73, 78 };
int m4[3] = { 97, 96, 95 };
int m5[3] = { 89, 93, 90 };
What if we have 40
students in the class?
3
Efficient solution…
Store this information in a two-dimensional
array
First dimension: which student 0, 1, 2, 3 or 4
Second dimension: which subject 0, 1, or 2
4
Pictorially
0 1 2
0 78 83 82
1 90 88 94
2 71 73 78
3 97 96 95
4 89 93 90
5
In general a 2D-array
datatype array_name[row_size][column_size];
int matrix[3][4];
Row 0 4 1 0 2
Row 1 -1 2 4 3
Row 2 0 -1 3 1
int matrix[3][4]; 4
1
0
Row 0 4 1 0 2 2
-1
Row 1 -1 2 4 3 2
4
Row 2 0 -1 3 1 3
0
-1
3
Row 0 4 1 0 2
Row 1 -1 2 4 3
Row 2 0 -1 3 1
8
Column 0 Column 1 Column 2 Column 3
Initialization (1st way)
Initialize when you declare
int x[4][4] = { {2, 3, 7, 2},
{7, 4, 5, 9},
{5, 1, 6, -3},
{2, 5, -1, 3}};
int x[][4] = { {2, 3, 7, 2},
{7, 4, 5, 9},
{5, 1, 6, -3},
{2, 5, -1, 3}};
9
Initialization (2nd way)
Using assignment operator
int i, j, matrix[3][4];
for (i=0; i<3; i++)
for (j=0; j<4; j++)
matrix[i][j] = i; matrix[i][j] = j;
j j
0 1 2 3
0 1 2 3
0 0 0 0 0
0 0 1 2 3
i 1 1 1 1 1
i 1 0 1 2 3
2 2 2 2 2
2 0 1 2 3 10
Exercise
Write the nested loop to initialize a 2D
array as follow
0 1 2 int i, j, x[4][3];
1 2 3 for(i=0; i<4; i++)
2 3 4 for(j=0; j<3; j++)
3 4 5 x[i][j] = i+j;
11
Initialization (3rd way)
By taking input from user
int i, j, matrix[3][4];
12
Showing content of a 2-Dim Array
0 1 2
for (i=0; i<4; i++){
1 2 3
for (j=0; j<3; j++){ 2 3 4
printf("%d ",m[i][j]);
} 3 4 5
printf("\n");
}
13
Computations on 2D arrays
14
Max in 2D
Find the maximum of int matrix[3][4]
max = 0
0 1 2 3
int max = matrix[0][0];
0 0 1 0 2
for (i=0; i<3; i++)
1 -1 2 4 3
for (j=0; j<4; j++){
2 0 -1 3 1
if (matrix[i][j] > max){
max = matrix[i][j];
}
}
15
Find a value in 2D
Find the number of times x appears in int matrix[3][4]
0 1 2 3
int count = 0;
0 0 1 0 2
for (i=0; i<3; i++)
1 -1 2 4 3
for (j=0; j<4; j++){
2 0 -1 3 1
if (matrix[i][j] == x)
count = count + 1;
}
16
Matrix sum
Compute the addition of two matrices
0 1 2 3 0 1 2 3 0 1 2 3
0 0 1 0 2 0 3 -1 3 1 0 3 0 3 3
1 -1 2 4 3 + 1 1 4 2 0 = 1 0 6 6 3
2 0 -1 3 1 2 2 1 1 3 2 2 0 4 4
17
solution
int matrix1[3][4],
matrix2[3][4],
sum[3][4];
// initialize matrix1 and matrix2
18
Exchange Two Rows
for (k=0; k<3; k++){
t = a[i][k];
4 6 2 a[i][k] = a[j][k];
a[j][k] = t;
0 5 3 i }
0 8 1 4 6 2
2 1 4 j
2 1 4
0 8 1
0 5 3
19
Transpose
int N = 3;
int a[N][N],b[N][N];
a
/* Transfer values to the
1 5 3 transpose matrix. */
for(i=0; i<N; i++){
4 2 6 for(j=0; j<N; j++) {
7 9 8 b[j][i] = a[i][j];
}
b }
1 4 7
5 2 9
3 6 8
20
In-place Transpose
(you can not use b array)
int N = 3,t;
int a[N][N],b[N][N];
a
/* Transfer values to the
1 5 3 transpose matrix. */
for(i=0; i<N; i++){
4 2 6 for(j=i+1; j<N; j++) {
7 9 8 t = a[i][j];
a[i][j] = a[j][i];
b a[j][i] = t;
}
1 4 7 }
5 2 9
3 6 8
21
Distance between 3
two points 1
1 2
22
double p[3][2] = {{1,1},
{2,1},
{1,3}};
double d1,d2,d3,dx,dy;
dx = p[0][0]-p[1][0];
dy = p[0][1]-p[1][1];
d1 = dx*dx + dy*dy;
dx = p[0][0]-p[2][0];
dy = p[0][1]-p[2][1];
d2 = dx*dx + dy*dy;
dx = p[1][0]-p[2][0];
dy = p[1][1]-p[2][1];
d3 = dx*dx + dy*dy;
min = (d1<d2)? d1: d2;
min = (min < d3? Min:d3;
23