2D Array
-what
-when
-why
- need
- input
-print
- how does stored in memory
-access
What is a 2D Array?
A 2D array = Array of arrays.
Think of it like:
Excel sheet
Matrix in maths
Table (rows × columns)
Example structure:
123
456
789
This has:
3 Rows
3 Columns
So declaration size = 3 x 3
Why Do We Need 2D Arrays?
We need 2D arrays when one dimension is not enough to represent data.
1D array stores data in a single line.
But real-world data often exists in rows and columns.
So we use 2D arrays to store and process such structured data.
When Data Has Two Attributes
Example: Student marks in 3 subjects.
If you use 1D array:
int marks[6] = {90,80,70,85,75,65};
Problem
You don’t know which marks belong to which student.
Using 2D array:
int marks[2][3] = {
{90,80,70}, // Student 1
{85,75,65} // Student 2
};
Now data is organized
Syntax (Declaration)
dataType arrayName[rows][columns];
Example:
int arr[3][3];
Meaning:
3 rows
3 columns
Total elements = 3 × 3 = 9
Question 1-
Create a 2D array with 5 rows 7 columns.
3. Initialization Methods
Method 1 — Full initialization
int arr[2][3] = {
{1,2,3},
{4,5,6}
};
Method 2 — Without inner braces
int arr[2][3] = {1,2,3,4,5,6};
Method 3 — Partial initialization
int arr[2][3] = {
{1,2},
{3}
};
Output memory:
120
300
(Unfilled = 0)
4. How 2D Array Elements Are Stored in Memory
5. Acess the 2D array
cout << arr[0][0];
6. Taking Input
int arr[2][3];
for(int i=0;i<2;i++){
for(int j=0;j<3;j++){
cin >> arr[i][j];
7. Printing 2D Array
for(int i=0;i<2;i++){
for(int j=0;j<3;j++){
cout << arr[i][j] << " ";
cout << endl;
8. Traversal Types
Row-Wise Traversal
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
cout << arr[i][j];
Order:
123456
Column-Wise Traversal
for(int j=0;j<cols;j++){
for(int i=0;i<rows;i++){
cout << arr[i][j];
Order:
142536
9. Memory Representation (Important for interviews)
2D array is stored in Row Major Order in C++.
Meaning memory stores like this:
arr[0][0]
arr[0][1]
arr[0][2]
arr[1][0]
arr[1][1]
arr[1][2]
...
Formula to calculate address:
Address = Base + (i * columns + j) * size
Where:
i = row index
j = column index
SECTION A — MCQ QUESTIONS
Q1. What is the correct declaration of a 2D array in C++?
A. int arr[][];
B. int arr[3][4];
C. array arr[3][4];
D. int arr[3,4];
Q2. Total elements in int arr[4][5]; are:
A. 9
B. 20
C. 10
D. 4
Q3. Index range of arr[3][3] is:
A. 1–3
B. 0–2
C. 0–3
D. 1–2
Q4. C++ stores 2D arrays in:
A. Column-major order
B. Row-major order
C. Random order
D. Diagonal order
Q5. Output?
int arr[2][2] = {1,2,3,4};
cout << arr[1][1];
A. 1
B. 2
C. 3
D. 4
Q6. Which loop prints column-wise traversal?
A.
for(i) for(j)
B.
for(j) for(i)
C. Both
D. None
Q7. What happens here?
int arr[2][3] = {1,2,3};
A. Error
B. Garbage values
C. Remaining elements become 0
D. Program crashes
Q8. Address formula of arr[i][j] is:
A. base + i + j
B. base + (i*j)
C. base + (i*cols + j)*size
D. None
Q9. Valid access for int arr[2][3];
A. arr[2][1]
B. arr[1][3]
C. arr[1][2]
D. arr[3][0]
Q10. Default value of uninitialized global 2D array is:
A. Garbage
B. 0
C. 1
D. NULL
SECTION B — CODING QUESTIONS
Q1. Write a program to input and print a 3×3 matrix.
Q2. Find the sum of all elements of a matrix.
Q3. Find the largest element in a matrix.
Q4. Print row-wise sum of matrix.
Q5. Print column-wise sum of matrix.
Q6. Find diagonal sum of a square matrix.
Q7. Print transpose of a matrix.
Q8. Count even and odd numbers in matrix.
Q9. Search an element in matrix.
Q10. Check whether matrix is identity matrix.