2 Darrays
2 Darrays
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];
marks o I 2 3 us 678 ↓
110 index
0 1234 5 6
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
1
for (int j 0;jz;j
=
+)
+
012
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 [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
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;
=
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
278 9 2 369
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
+
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]
!
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
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]