0% found this document useful (0 votes)
8 views

2 DArray

Uploaded by

fatimasadsudipta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

2 DArray

Uploaded by

fatimasadsudipta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

2D Arrays and Matrices

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 };

What if we have 120


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 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

Column 0 Column 1 Column 2 Column 3


6
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

Column 0 Column 1 Column 2 Column 3


7
In general a 2D-array
datatype array_name[row_size][column_size];

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

Column 0 Column 1 Column 2 Column 3 1


8
in memory
Accessing Array Elements
int matrix[3][4];
 matrix has 12 integer elements

 matrix[0][0] element in first row, first column

 matrix[2][3] element in last row, last column

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

Column 0 Column 1 Column 2Column 3 10


How to put values in a 2D
array
int m[3][4] = {4, 1, 0, 2,-1, 2, 4, 3,
0, -1, 3, 1};

Row 0 4 1 0 2
Row 1 -1 2 4 3

Row 2 0 -1 3 1

Column 0 Column 1 Column 2Column 3 11


How to put values in a 2D
array
int m[3][4] = { {4, 1, 0, 2},
{-1, 2, 4, 3},
{0, -1, 3, 1}};

Row 0 4 1 0 2
Row 1 -1 2 4 3

Row 2 0 -1 3 1

Column 0 Column 1 Column 2Column 3 12


How to put values in a 2D
array (omitted ones set to 0)
int m[3][4] = { {4, 1, 5},
{-1, 2, 4, 3},
};

Row 0 4 1 5 0
Row 1 -1 2 4 3

Row 2 0 0 0 0

Column 0 Column 1 Column 2Column 3 13


How to put values in a 2D
array
int m[][4] = { {4, 1, 0, 2},
{-1, 2, 4, 3},
{0, -1, 3, 1}};

Row 0 4 1 0 2
Row 1 -1 2 4 3

Row 2 0 -1 3 1

Column 0 Column 1 Column 2Column 3 14


By taking input from user

int i, j, matrix[3][4];

for (i=0; i<3; i++)


for (j=0; j<4; j++){
scanf("%d", &matrix[i][j]);
}

15
By taking input from user

int i, j, m , n;

scanf("%d%d", &m, &n);

int matrix[m][n];

for (i=0; i < m; i++)


for (j=0; j < n; j++){
scanf("%d", &matrix[i][j]);
}

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

for (i=0; i<3; i++)


for (j=0; j<4; j++)
sum[i][j]= matrix1[i][j]+matrix2[i][j];

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];

Multiple dimensions get difficult to visualize graphically.


Distance between 3

two points 1

1 2

 Imagine a point in a 2-dimentional space (each point is represented by (x,y))


and we store 3 points in an array declared by double p[3][2] (you can
think that p[i][0] is the x value and p[i][1] is the y value of ith point).
 We are now interested in finding the closest two points.
For example, if p[3][2] = {{1,1},
{2,1},
{1,3} };
Then we will say that points (1,1) and (2,1) are the closest two points.
 Write a program to do that:

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

You might also like