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 s1[3] = { 78, 83, 82 };
int s2[3] = { 90, 88, 94 };
int s3[3] = { 71, 73, 78 };
int s4[3] = { 97, 96, 95 };
int s5[3] = { 89, 93, 90 };
4
Pictorially
0 1 2
0 78 83 82
1 90 88 94
2 71 73 78 t
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
Row 1
Row 2
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
9
Column 0 Column 1 Column 2 Column 3
How to put values in a 2D
array
m[0][0] = 4;
int m[3][4]; m[0][1] = 1;
m[0][2] = 0;
m[0][3] = 2;
……………
Row 0 4 1 0 2
Row 1 -1 2 4 3
Row 2 0 -1 3 1
Row 0 4 1 0 2
Row 1 -1 2 4 3
Row 2 0 -1 3 1
Row 0 4 1 0 2
Row 1 -1 2 4 3
Row 2 0 -1 3 1
Row 0 4 1 5 0
Row 1 -1 2 4 3
Row 2 0 0 0 0
Row 0 4 1 0 2
Row 1 -1 2 4 3
Row 2 0 -1 3 1
int i, j, matrix[3][4];
15
By taking input from user
int i, j, m , n;
int matrix[m][n];
16
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 17
Exercise
Write the nested loop to initialize a 2D
array as follow
j
0 1 2
0 0 1 2 int i, j, x[4][3];
i 1 1 2 3 for(i=0; i<4; i++)
2 2 3 4 for(j=0; j<3; j++)
3 3 4 5 x[i][j] = i+j;
18
Create an array like below
19
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");
}
20
Computations on 2D arrays
21
Max in 2D
Find the maximum of int matrix[3][4]
max = 5
0 1 2 3
int max = matrix[0][0];
0 5 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];
}
}
22
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;
}
23
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
24
Solution
int matrix1[3][4],
matrix2[3][4],
sum[3][4];
// initialize matrix1 and matrix2
25
Matrix Multiplication
1 x 1 + 4 X 2 + 6 X 9 + 10 X 3
= 1 + 8 +54 +30
= 93
26
Matrix Multiplication
27
Solution
28
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
29
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
30
In-place Transpose
(you can not use b array)
int N = 3,t;
int a[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
31
Higher-Dimensional Arrays
double Coord[100][100][100];
•
Distance between 3
two points 1
1 2
33
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;
34