0% found this document useful (0 votes)
24 views25 pages

2D Array Part L

The document discusses 2D arrays including representation, declaration, initialization, traversal, taking input from user, finding largest element, printing sum of elements, adding two matrices, transposing a matrix, changing a matrix to its transpose, and rotating a matrix by 90 degrees clockwise. It also provides examples of programs to perform these operations on 2D arrays.
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)
24 views25 pages

2D Array Part L

The document discusses 2D arrays including representation, declaration, initialization, traversal, taking input from user, finding largest element, printing sum of elements, adding two matrices, transposing a matrix, changing a matrix to its transpose, and rotating a matrix by 90 degrees clockwise. It also provides examples of programs to perform these operations on 2D arrays.
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/ 25

2D Arrays-1

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)

2 (,0) (2,1) (2,2) [2,3K2, 4)


3 B3, 0) (3, 1) (3,2) B,3) (3,4)

4 x5

aritles
arr (index] index - 0 n-1
to
Declaration of a 2-Dimensional Array
int arr[4][2] ;

While initializing a 2D array it is necessary to mention the second


(column) dimension, whereas the first dimension (row) is optional.

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

for (inti= 1;i < n;i +) +

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

a[2][3] b[2]C3] wes[2][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

(0,0) (0, 1) (0,2) n xm

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

+(0] [I] arr[I](0]


=

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

3 13 14 15 16 3 (3,0) (3,1) (3,2) (3,3)


arr [4][4] i arr [Y][Y]
(i j)=

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

3678 26 10 14 114 106 2

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

126 10 14 0,0 0,1 0,2 0,3

2 3 7 11 15

3 48 12 16
1) MatrixMultiplication

2) Spiral Printing
Is Next Lecture

Thank you !!

You might also like