2D Arrays
in One Shot
What and
Why?
So far we have explored arrays with only one dimension. It is also
possible for arrays to have two or more dimensions. The two
dimensional array is also called a matrix.
datatype array_name[r][c];
This is a 2D array where r depicts number of rows in matrix and c
depicts number of columns in the matrix.
aur
8
maths 12 3 M S 6 789 arr
[3] 8
=
marks o I 2 3 us 678 ↓
110 index
0 1234 5 6
Ply ⑥ 123 4 S 635 bou [I](4] 100
=
Chem 1 7 8 9 10 100 1 2
3 S
- cell
Math
2 Y 11 13/2 IS
bru
bor (r] [c]
↓ 3 column
number
now no.
intarr [33[2];
o 1
awr 90190] 1;=
0 aur(03/0] arr[03C1]
aur 90][1] 2
=
;
I aw(1][0] acr(ICI] ↓
2 aur(2720] awk]21] -
What is
&
actually 2D
asway
int and [2][n]
⑭
0;i(;i
theelements
are
#immunoprint for (it i )
=
+
1
1
for (int j 0;jz;j
=
+)
+
012
0 aCO3[o] aCoJCl] aS0TK]
I printf ("%od", a (i][s]);
3
aCl]Co] all][l] aSl][2]
printf( "\");
1
3
o I 2
0 9 8
10
1 7 · S
Initialisation of a 2-Dimensional
Array
int arr[4][2] = { { 1234, 56 }, { 1256, 43 }, { 1434, 32 }, { 1312, 96 } } ;
int arr[4][2] = { 1234, 56 , 1256, 43 , 1434, 32 , 1312, 96} ;
int arr[2][3] = { 12, 34, 56, 78, 91, 23 } ;
int arr[ ][3] = {12, 34, 56, 78, 91, 23 } ;
int [23[23:
[[123,53,433/[1,2,3,43
a
Ques : Write a program to store roll number and
marks obtained by 4 students side by side in a
matrix. students
R. No. marks #H user input - no
of
d I
Marks
of P, C, M
Raghav ⑥ 76 80
Sanket 1 57 81
2 40 90
urvi
321 95
Manvi
intaur
(4](2]=<76,80,57,81,40,90,21,953;
Ques : Write a program to store 10 at every index of
a 2D matrix with 5 rows and 5 columns.
12 34
[10,10,10,....3
⑥
int arr[S][S] =
d 10 10 10 10 10
1 1010 10 10 10
2 1010 10
1010
10 10 10 10
3 10
4 1010 10 10 10
Ques : Write a program to add two matrices.
⑲ o 1
1 d I
⑥ 1 2 · S 6 06 8
↳
3 Y 1 8
1 7 110 12
inta (23(2) 21,2,3,43;
=
res(iii] a(i](s] b(i)5]
:
+
int b (23(2) [5,6,7,83;
=
int res(2][z];
H.W Do it
using
without
:
extra matrix
-
⑤
Ques : Find the sum of a given matrix of n x m.
↓ ↓
columns
012345 67 rows
135248/2
sum 0;
=
Homework Find out
the element mint
max
elementin
- 1) a 2D
array
2)
& the
of
index max lement, (i,j)
HW : Given a matrix ‘a’ of dimension n x m and 2
coordinates (l1, r1) and (l2, r2). Return the sum of
the rectangle from (l1,r1) to (l2, r2).
int a (m][n]; a (i]]e (i,j)
01 2 34
(0,0)(0,1)(0,2)(0,5)(0,4)
(1,0)(1,1)(1,2)(1,3)(1,4)
2 (2,0)(2,1)(2,2)(2,3)(2,4)
3(3,0)(3,1)(3,2)(3,3)(3,4)
Homework : Write a program to print the row
number having the maximum sum in a given
matrix. & the maxSumsow
maxsum =
o 7 2 3
->
o 123 1 7
10502 7
29003 12
Ques : Given a matrix having 0-1 only, find the
row with the maximum number of 1’s.
0 2 3
o 1 o 1 1
7 o 1 .
2 100 I
Ques : Write a program to Print the transpose of
the matrix entered by the user. (Leetcode - 867)
transpose?
1
1 Y
: 2 s
3 6
are(23[3] 1
(0.8)(1,0)
pip,na,
o
⑥
(0,1)(k))
Ques : Write a program to Print the transpose of
the matrix entered by the user. (Leetcode - 867)
& Store it
in a
separate matrin
d 1 2 ⑥ 1
o 3
12
->
o 11
I Y 56
1 29
arr (2][3] 3 6
2
bur[3](z]
bruiji):arr(i](i];
*
Ques : Write a program to Print the transpose of
the matrix entered by the user. (Leetcode - 867)
to change the given non matrix
to its
transpose.
012 o 1 2
o 1 23 o 17 7
1456 -> 1 258
278 9 2 369
arr (n](n]; arr (n](n];
d 1 2 3
↑
&
0 (0,0)(0,1)(0,2)(0,3)
I(1,0)(1))(1,2)(13)
2(2,0)(2,1)(2,2)(2,3)
3(3,9(3,4)(3,2)(3,3)
d 1 2 3
↑
13
2/23/3
Y
&
o 7
M
43
7
I 6
*
if 818
2
939 10,0 11 123
+
i 0tn 1 -
134
3 13
AM
/Y By 16 i+ n -
1
IS
* i
Ques : Write a program to rotate a matrix 90°
clockwise. (Leetcode - 48)
original transpose rotated 90
123 147 7
u 1
Y S S 8 8 5 2
6 2
7 8 9 3 69 963
G
arr (3][3]
#l l ili
Steps:1) Transpose
each
2)Reverse now ,
⑮ a / B
jjkk
&*
Ques : Write a program to print the multiplication
of two matrices given by the user.
·nx] (55%)
u
·(i) I
1x5 + 2x7 1x6 2x8
1),j
0 +
S =
+
=
7 3x5 4x7
+
a[c][2] b(27/2] res [2](2]
f
d column is
now dependent
is dependent
on this
on this
0
12
·!
01 2
p 1I I 1 il
1
⑥ 2 10 2 2 o 6
12 1 2x1 121
=
,,
,
- 2
,,
1x1 2x2
+ (x)
+
6
=
# multiplication:
Immatrix
aSmiCn] b(p][9]
x res(m][q]
=
-
-
1) p
=
n =
order
2) resultant is
ma
3) AXB = BXA
a
-foot: ·
a[z](17 res[1](1]
3x1 4x2
+
3 8 1/
=
+
=
o 1 o I 2 3
⑥ Δ 12 3 o 0.0
0 I (1,2)
1 X =
1 2
2
b(z][y]
a[3](z]
res(3](u]
res(l](z] aCI][0] b(07k]
*
=
aCi[i] *b(,3[2];
+
res(i)(5]:it row of ith
*
b
a column
of
res(i][i] (a(i]I0], a(iJ21], a(Y(z]) *(b(03(i],bLi39i],b(2,j])
=
I
1 xxes:337 1
I 2
!
11 1Y 1720
3: "( =
20 so we
35 465768
res(i)(5]
aCi](n] +b(n][i]
=
B. Wave print-1
Output
0 ↑ 2
123
⑥
I 23
1
456
654/123634789
789
2789
no
of columnn
a <m]n]
IW: wave print-2 no
of rown:
m
Algo:
0 ↑ 2
if (column no =
even
=
⑥
I 2 3
now no -> m -1 to
1456 3
es4
278 9
rowno - 0 to m -
1
3
Ques : Given an n x m matrix ‘a’, print all elements
of the matrix in spiral order. (Leetcode - 54)
0 ↑ 2 Output: 123698745
⑥
I 2 3
1456
1237
2789
5678
9 10 11 12
723481211109567
polmai mine
iE
output
12 3 48 12/11095676
count 0x2* ** *** *** * *
=
13
mins X
0123u5 - loop2
minu+ +
[minv] [coe]
:if
a
3 coleminc to max
maxc
minu maxc
(minr-maxr)
--
maxr [everse]
man--
Δ
maxc-minc
3 maxr
minc (reverse]
u 30
minc++
I
maxremin
mine max 2
a[S][s]
int tue =
mn; count the
HW : Given a positive integer n, generate a n x n &quare
matrix filled with elements from 1 to n2 in spiral
order. (Leetcode - 59)
-
⑨
n 3
=
I 2 3
g 9 Y
7 6 S
a(i][i]