0% found this document useful (0 votes)
13 views15 pages

2D Array Complete Notes

A 2D array is an array of arrays, useful for representing structured data in rows and columns, similar to a matrix or an Excel sheet. It is declared with a specific number of rows and columns, and elements are stored in memory in row-major order. The document also covers initialization methods, memory representation, and the differences between 2D arrays and 2D vectors in C++.

Uploaded by

b7396753
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)
13 views15 pages

2D Array Complete Notes

A 2D array is an array of arrays, useful for representing structured data in rows and columns, similar to a matrix or an Excel sheet. It is declared with a specific number of rows and columns, and elements are stored in memory in row-major order. The document also covers initialization methods, memory representation, and the differences between 2D arrays and 2D vectors in C++.

Uploaded by

b7396753
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

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 * colum ns + 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.

Function with 2D Array?


1. Basic Syntax

void functionName(int arr[][COL], int rows);

Column size compulsory.

2. Example — Print 2D Array

Function Definition

void print(int arr[][3], int rows){

for(int i=0;i<rows;i++){

for(int j=0;j<3;j++){

cout << arr[i][j] << " ";

cout << endl;

Function Call

int main(){
int arr[2][3] = {

{1,2,3},

{4,5,6}

};

print(arr,2);

3. Why Column Size Mandatory?

Compiler must know memory layout.

Recall memory formula:

Address = base + (i * columns + j) * size

If column size unknown → address cannot be calculated.

So this is wrong:

void print(int arr[][]); // Error

4. Example — Sum of Elements

int sumArray(int arr[][3], int rows){

int sum = 0;

for(int i=0;i<rows;i++){

for(int j=0;j<3;j++){

sum += arr[i][j];

return sum;

Call:

cout << sumArray(arr,2);


Why Column Size Must Be Defined?

When you pass a 2D array to a function, the compiler needs column size to calculate
memory address.

Recall formula:

Address = base + (i * columns + j) * size

If column size unknown → compiler can’t jump rows in memory.

So this is compulsory:

void fun(int arr[][3], int rows);

What You Tried (User-Defined Column)

Example attempt:

int col;

cin >> col;

void fun(int arr[][col]); // Error

Why error?

Because:

 col value is decided at runtime

 Function parameter needs compile-time constant

What Is Allowed?

✔ Fixed column size

void fun(int arr[][3], int rows);

✔ Using constant

#define COL 3

void fun(int arr[][COL], int rows);

✔ Using const variable

const int COL = 3;


void fun(int arr[][COL], int rows);

All compile-time known → valid.

Full Example — Print 2D Array Using Function

Why Use const Instead of Direct 3?


Bad practice

void fun(int arr[][3], int rows);

Better practice

const int COL = 3;

void fun(int arr[][COL], int rows);

Benefits:

 Easy to change size later

 More readable

 Used in large systems

2D Vector in C++
 What is 2D vector

 Why use it instead of 2D array

 Declaration & initialization

 Input/output

 Functions

 Dynamic sizing

 Interview differences

What is a 2D Vector?

A 2D vector = vector of vectors

Think like:

vector → 1D dynamic array

2D vector → dynamic matrix

Syntax idea:

vector<vector<int>> matrix;

So each row itself is a vector.

Why Use 2D Vector Instead of 2D Array?

2D Array 2D Vector

Fixed size Dynamic size


2D Array 2D Vector

Compile-time size Runtime size

Less flexible Highly flexible

Hard to resize Easy resize

Manual memory Auto managed

Industry prefers vectors

Declaration of 2D Vector

Empty Declaration

vector<vector<int>> arr;

No size yet.

With Fixed Size

vector<vector<int>> arr(3, vector<int>(4));

Meaning:

 3 rows

 4 columns

 Default value = 0

Matrix:

0000

0000

0000

With Initial Value

vector<vector<int>> arr(3, vector<int>(4, 5));

All elements = 5.

Initialization Like Array

vector<vector<int>> arr = {
{1,2,3},

{4,5,6}

};

Matrix:

123

456

Accessing Elements

Same as 2D array:

cout << arr[0][1];

Output:

Modify:

arr[1][2] = 99;

Input in 2D Vector

int r = 2, c = 3;

vector<vector<int>> arr(r, vector<int>(c));

for(int i=0;i<r;i++){

for(int j=0;j<c;j++){

cin >> arr[i][j];

Runtime size possible

Output / Traversal

for(int i=0;i<[Link]();i++){

for(int j=0;j<arr[i].size();j++){

cout << arr[i][j] << " ";


}

cout << endl;

You might also like