Two-Dimensional Arrays: Declaration, Initialization, and Accessing Elements
Learn two-dimensional arrays in C++. Understand rows and columns, array declaration, initialization, accessing elements, and nested loops through beginner-friendly examples and real-world programming exercises.
Learning Objectives
After completing this lesson, students will be able to:
- Understand the concept of two-dimensional arrays.
- Identify rows and columns in an array.
- Declare and initialize a two-dimensional array.
- Access and display individual array elements.
- Use nested loops to process array elements.
- Modify programs to solve real-world problems.
1. Introduction to Two-Dimensional Arrays
In C++, a two-dimensional array (2D array) is a collection of elements of the same data type arranged in rows and columns.
It is useful when we need to store data in a table format.
For example:
- Student marks in different subjects.
- Monthly sales of different products.
- Temperature records for different cities.
- Seats in a classroom.
- Student attendance records.
Real-Life Example: Student Marks
Suppose a teacher wants to store the marks of 3 students in 4 subjects.
Instead of declaring 12 separate variables, we can use a two-dimensional array.
| Student | English | Maths | Computer | Physics |
|---|---|---|---|---|
| Student 1 | 75 | 80 | 85 | 90 |
| Student 2 | 65 | 70 | 75 | 80 |
| Student 3 | 85 | 90 | 95 | 88 |
Here:
- Rows represent students.
- Columns represent subjects.
- Each cell contains one student's marks in a particular subject.
Remember: A two-dimensional array is like a table containing rows and columns.
2. Structure of a Two-Dimensional Array
Consider the following array:
int marks[3][4];This array contains:
- 3 rows.
- 4 columns.
- 12 total elements.
Total Elements = Rows × Columns
$$ 3 \times 4 = 12 $$
3. Declaration of a Two-Dimensional Array
To declare a two-dimensional array, use the following syntax:
dataType arrayName[rows][columns];Example:
int marks[3][4];Understanding the code:
| Part | Meaning |
|---|---|
int |
Data type |
marks |
Array name |
[3] |
Number of rows |
[4] |
Number of columns |
This declaration creates an integer array with 3 rows and 4 columns.
Other examples:
float temperature[7][2];
char seats[5][6];
double sales[12][4];4. Initialization of a Two-Dimensional Array
Initialization means assigning values to an array when it is declared.
Example:
int marks[3][4] = {
{75, 80, 85, 90},
{65, 70, 75, 80},
{85, 90, 95, 88}
};Understanding the code:
- The first set of braces
{75, 80, 85, 90}represents the first row. - The second set represents the second row.
- The third set represents the third row.
- Each row contains four elements.
Another Way to Initialize
We can also initialize the array without separate row braces:
int marks[3][4] = {
75, 80, 85, 90,
65, 70, 75, 80,
85, 90, 95, 88
};C++ fills the array row by row.
5. Accessing Elements of a Two-Dimensional Array
We can access an individual element using its row and column indexes.
Syntax:
arrayName[rowIndex][columnIndex];Example:
#include <iostream>
using namespace std;
int main()
{
int marks[3][4] = {
{75, 80, 85, 90},
{65, 70, 75, 80},
{85, 90, 95, 88}
};
cout << marks[0][0] << endl;
cout << marks[1][2] << endl;
cout << marks[2][3] << endl;
return 0;
}This array contains:
- 3 rows (students).
- 4 columns (subjects).
- 12 total elements.
Visual representation of a 2D array
The following table shows the array values with their row and column indexes.
| Row / Column | Column 0 | Column 1 | Column 2 | Column 3 |
|---|---|---|---|---|
| Row 0 | 75 | 80 | 85 | 90 |
| Row 1 | 65 | 70 | 75 | 80 |
| Row 2 | 85 | 90 | 95 | 88 |
Understanding indexes:
- Row indexes:
0, 1, 2 - Column indexes:
0, 1, 2, 3
Remember: Array indexes always start from 0 in C++.
Understanding each element's index
In C++, we access an element using:
marks[row][column]
| Element (Marks) | Row Index | Column Index | C++ Access |
|---|---|---|---|
| 75 | 0 | 0 | marks[0][0] |
| 80 | 0 | 1 | marks[0][1] |
| 85 | 0 | 2 | marks[0][2] |
| 90 | 0 | 3 | marks[0][3] |
| 65 | 1 | 0 | marks[1][0] |
| 70 | 1 | 1 | marks[1][1] |
| 75 | 1 | 2 | marks[1][2] |
| 80 | 1 | 3 | marks[1][3] |
| 85 | 2 | 0 | marks[2][0] |
| 90 | 2 | 1 | marks[2][1] |
| 95 | 2 | 2 | marks[2][2] |
| 88 | 2 | 3 | marks[2][3] |
Accessing individual elements
To access a particular mark, specify its row and column indexes.
cout << marks[0][0]; // Output: 75
cout << marks[1][2]; // Output: 75
cout << marks[2][3]; // Output: 88For example:
marks[1][2] means:
1→ Second row.2→ Third column.- Value →
75.
Important points to remember
| Concept | Explanation |
|---|---|
| Array declaration | int marks[3][4] |
| Number of rows | 3 |
| Number of columns | 4 |
| First row index | 0 |
| First column index | 0 |
| Last row index | 2 |
| Last column index | 3 |
| Total elements | 3 × 4 = 12 |
| Last element | marks[2][3] = 88 |
Modify the Code
Try making the following changes:
- Display the value at
marks[0][2]. - Display the value at
marks[1][3]. - Display the value at
marks[2][0]. - Change the value of
marks[0][0]from75to95. - Display the updated value.
6. Displaying All Elements Using Nested Loops
A nested loop means placing one loop inside another loop.
To display all elements of a two-dimensional array:
- The outer loop controls the rows.
- The inner loop controls the columns.
Example: Display Student Marks
#include <iostream>
using namespace std;
int main()
{
int marks[3][4] = {
{75, 80, 85, 90},
{65, 70, 75, 80},
{85, 90, 95, 88}
};
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
cout << marks[i][j] << " ";
}
cout << endl;
}
return 0;
}Understanding the Code
Outer Loop:
for (int i = 0; i < 3; i++)irepresents the row index.- The loop runs three times.
- It processes rows 0, 1, and 2.
Inner Loop:
for (int j = 0; j < 4; j++)jrepresents the column index.- The loop runs four times for each row.
- It accesses all columns of the current row.
Display Statement:
cout << marks[i][j] << " ";This statement displays the element at the current row and column.
cout << endl;Moves the cursor to the next line after displaying one complete row.
Program Output
75 80 85 90
65 70 75 80
85 90 95 88Dry Run
Consider the first row:
| i (Row) | j (Column) | Element | Output |
|---|---|---|---|
| 0 | 0 | 75 | 75 |
| 0 | 1 | 80 | 80 |
| 0 | 2 | 85 | 85 |
| 0 | 3 | 90 | 90 |
After completing four columns, the inner loop stops and the outer loop moves to the next row.
Remember: The inner loop completes all its iterations before the outer loop moves to the next iteration.
7. Taking Input from the User
So far, we have initialized array values in the program. Now, let's learn how to take values from the user.
Example: Input and Display Marks
#include <iostream>
using namespace std;
int main()
{
int marks[3][4];
// Input marks
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
cout << "Enter marks: ";
cin >> marks[i][j];
}
}
// Display marks
cout << "\nStudent Marks:\n";
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
cout << marks[i][j] << " ";
}
cout << endl;
}
return 0;
}Understanding the Code
Step 1: Declare the array
int marks[3][4];Creates an array with 3 rows and 4 columns.
Step 2: Take input
cin >> marks[i][j];Stores the entered value at the current row and column.
Step 3: Display input values
cout << marks[i][j] << " ";Displays the stored values.
Sample Input
Enter marks: 75
Enter marks: 80
Enter marks: 85
Enter marks: 90
Enter marks: 65
Enter marks: 70
Enter marks: 75
Enter marks: 80
Enter marks: 85
Enter marks: 90
Enter marks: 95
Enter marks: 88Sample Output
Student Marks:
75 80 85 90
65 70 75 80
85 90 95 888. Real-World Example: Weekly Temperature Records
Suppose a weather department wants to record the morning and evening temperatures of three cities for one day.
We can use a two-dimensional array to store this information.
Program: Store and Display Temperature
#include <iostream>
using namespace std;
int main()
{
float temperature[3][2];
// Input temperatures
for (int i = 0; i < 3; i++)
{
cout << "Enter temperatures for City "
<< i + 1 << endl;
for (int j = 0; j < 2; j++)
{
if (j == 0)
cout << "Morning: ";
else
cout << "Evening: ";
cin >> temperature[i][j];
}
}
// Display temperatures
cout << "\nTemperature Report\n";
for (int i = 0; i < 3; i++)
{
cout << "City " << i + 1 << ": ";
for (int j = 0; j < 2; j++)
{
cout << temperature[i][j] << " ";
}
cout << endl;
}
return 0;
}Understanding the Code
float temperature[3][2];Creates an array with 3 rows and 2 columns.
- Rows represent three cities.
- Column 0 represents morning temperature.
- Column 1 represents evening temperature.
if (j == 0)
cout << "Morning: ";
else
cout << "Evening: ";This condition displays the appropriate label according to the column index.
Sample Input:
City 1: 25 35
City 2: 28 38
City 3: 22 32Sample Output:
Temperature Report
City 1: 25 35
City 2: 28 38
City 3: 22 32Modify the Code – Practice 2
Make the following modifications:
- Change the number of cities from 3 to 5.
- Add a third column for night temperature.
- Display morning, evening, and night temperatures.
- Calculate and display the average temperature of each city.
9. Real-World Example: Student Attendance Record
A university wants to maintain the attendance record of five students for four days.
We can represent this information using a two-dimensional array.
- Rows represent students.
- Columns represent days.
1represents Present.0represents Absent.
Program: Student Attendance
#include <iostream>
using namespace std;
int main()
{
int attendance[5][4] = {
{1, 1, 0, 1},
{1, 0, 1, 1},
{1, 1, 1, 1},
{0, 1, 1, 0},
{1, 1, 0, 1}
};
for (int i = 0; i < 5; i++)
{
cout << "Student " << i + 1 << ": ";
for (int j = 0; j < 4; j++)
{
if (attendance[i][j] == 1)
cout << "P ";
else
cout << "A ";
}
cout << endl;
}
return 0;
}Program Output:
Student 1: P P A P
Student 2: P A P P
Student 3: P P P P
Student 4: A P P A
Student 5: P P A PUnderstanding the Code
if (attendance[i][j] == 1)Checks whether the student is present.
cout << "P ";Displays P for Present.
cout << "A ";Displays A for Absent.
Modify the Code – Practice 3
- Take attendance input from the user instead of initializing values.
- Change the number of students to 10.
- Change the number of days to 5.
- Count and display the total present days for each student.
- Display the total number of absences for each student.
10. Important Points to Remember
| Concept | Explanation |
|---|---|
| 2D Array | Stores elements in rows and columns |
| Declaration | int arr[3][4]; |
| Initialization | Assigning values during declaration |
| Row Index | Identifies the row |
| Column Index | Identifies the column |
| First Index | Starts from 0 |
| Nested Loops | Used to process rows and columns |
| Total Elements | Rows × Columns |
Important: Always use valid row and column indexes. For an array declared as int arr[3][4], valid row indexes are 0–2 and valid column indexes are 0–3.
References and Bibliography
-
[1] cppreference.com, “Arrays,” C++ Reference. [Online]. Available: https://en.cppreference.com/w/cpp/language/array. [Accessed: Nov. 29, 2026].
-
[2] Microsoft, “Arrays (C++),” Microsoft Learn. [Online]. Available: https://learn.microsoft.com/en-us/cpp/cpp/arrays-cpp. [Accessed: Nov. 29, 2026].