2D Array Part L
2D Array Part L
Lecture- 14
Raghav Garg
What and P, c, M -
6 students
Why? es
P C M
01 2 3 4 5 6
90 95 6S
91 908175 10056 68
91 100 56
Est 92 99 91
81 9887
82 9188
8390 89
Representation of 2D array
⑧
emateandeting
O
C0,0) (0,17 (0,2) (0,3) (0,4)
cont<> brr[2] (37;
bur · (1,0) (1,1) (1,2) (1,3) (14)
4 x5
aritles
arr (index] index - 0 n-1
to
Declaration of a 2-Dimensional Array
int arr[4][2] ;
Correct
int arr[2][3]; int arr[ ][3];
Incorrect
int arr[2][ ]; int arr[ ][ ];
"...t
int air (3][3];
I
avr(3]/37:[[1,2,33,
int
[4,5,63,[7,8,933;
also
2D
Arrays are
array of arrays
Initialisation of a 2-Dimensional
Array
~
int arr[4][2] = { { 1234, 56 }, { 1256, 43 }, { 1434, 32 }, { 1312, 96 } } ;
w
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 } ;
Xintavr(2])]=312, 34,56,78,91,233;
Traversal through 2D array
out
I
int arr(3]/33= 21,2, 3, 4, 5, 6, 7, 8, 93;
E I 2
0
arr(oDSo] an[O]/l] aur(o][2]
1 2 3
1456
2789
Array of ways
int arr[27/3] =
[1,2,3,4,5,63;
* I 2
⑧ I 2 3
I
YS 6
+
Taking 2D array as input from the user
Very simple
Ques : Write a program to store roll number and
marks obtained by 4 students side by side in a
matrix.
sin"
stylet
Ques: Write a C++ program to find the largest
element of a given 2D array of integers.
an
in
int max arr[O];
=
I
if (max < arr (i])
an
max= [i]
3
coutmax;
Ques : Write a program to print sum of all the
elements of a 2D matrix. Ditto same C.W. attempt
-
Ques : Write a program to add two matrices.
⑧ I 2 ⑧ I 2 ⑧ I 2
⑧ I 2 3 ⑧ 7 8 9 ⑧ 8 10 12
t -
Y S 6
I I 10 11 12
I 14 16 18
2 x 3 2 x 3 2 x 3
b (:1)] b(i][s]+a[i][j];
=
X
Ques : Write a program to print the transpose of
the matrix entered by the user and store it in a new
matrix.
O I
⑧ I 2
⑧ 14
⑧ I 2 3
12 S
S 6
·Y
2 3 6
2 x 3
arr (m)[n]
m h
x
3x2
t[n][m]
(10) (1, 1) (1, 2)
O I
⑧ I 2
⑧ 14
⑧ I 23
2 S
I
S 6
·Y
2 3 6
2 x 3
arr (m)[n]
m h
x
3x2
t[n][m] n xm
+(0][0] arr[o](o)
=
+[I](0] =
arr[o][]
Ques : Write a program to change the given matrix
with its transpose. [Leetcode 867]
O, you are given a
matrix/2D-Array of size (nxn).
this matrix into its transpose.
Change
I U 7
I 2 3
2 5 8
y S 6 ->
369
7
89
⑧ 12 3 01 2 3
I
I
⑧ 234 · (0,0) (0,1) (0,2) (0,3)
I 567 8 -
(1,0) (1,1) 4,2) (1,3)
9
(2,0) (2,1) (2,2) (2,3)
2 10 12 2
11
copy
=
-
⑧ I 2 3 ⑧ I 2 3
⑧ ↑4243 B Y
Y ⑧ I I qB Y
13
I 45 S 6 * I 25 6
-100,4
15
24 10
I #2
12
2
39 * 11
k,s
S
3 *,B 14 Es 16 3
By 148 4,2 16
13 14 15
arr [4][4] arr [4][4]
Ques : Write a program to rotate the matrix by 90
degrees clockwise. [Leetcode 48]
#Hint1:Transpose
I 2 3 I 4 7
6
transpose 2 S 8
YS ->
8 9 3 6 9
7
8
Y
S
I
2
S If
each
we
now
severe
of transposed
matrix
9 6 3
I 2 13
transpose
->
24
34
3 I
Savera
y 2
0 I 23
123 Y 15913 O 13 9 5 I
9 10 11 12 37 11 15 2 15 I1 73
13 14 15 16 4 8 12 16 16 128 Y
3
- -a
neverse
transpose
I 39 13
j i
012 3
O 15 9 13
I 3 9 13
2 3 7 11 15
3 48 12 16
1) MatrixMultiplication
2) Spiral Printing
Is Next Lecture
Thank you !!