9.1 2D Arrays - Watermark
9.1 2D Arrays - Watermark
9.1 2D Arrays - Watermark
Notes
1. It is similar to 2D matrices that we studied in 11th and 12th class.
2. It has 2 parts
a. Rows - Horizontal Arrays in the 2D matrix. For instance, in the above
example, we have 3 rows:
i.
ii.
iii.
b. Columns - Vertical Arrays in the 2D matrix. For instance, in the above
example, we have 5 columns:
i.
ii.
iii.
iv.
v.
3. Note: Indexing of both rows and columns starts with 0.
Declaration of 2D matrices
1. 2D Arrays are declared similar to 1D arrays but with an additional
dimension.
Syntax: int arr[rows][columns]
For example,
Code:
Searching in a matrix
Problem: We have to find if value x is present in the 2D array.
1. While traversing in the 2D matrix, just we have to put one if statement which
checks if(a[i][j] == x) , then x is present otherwise not.
Code:
Spiral Order Matrix Traversal
Problem: We have to print the given 2D matrix in the spiral order. Spiral
Order means that firstly, first row is printed, then last column is printed,
then last row is printed and then first column is printed, then we will come
inwards in the similar way.
Code: Input the array first then perform the following code