# Traversing elements present in a 2-Dimensional Array/Matrix

In this article we will go through code examples in different languages to traverse/iterate a 2 dimensional array. Traversal or iterating means visiting or accessing all elements in present in the given array.

Let’s consider the use-case we used in previous articles: There are `4` students in a class and we need to store marks obtained by each student in `3` different subjects. In order to achieve this, we create a 2-dimensional array with `4` rows and `3` columns, Each row contains marks in all the subjects obtained by the student.

``````[
[81, 75, 92], // Student-0's marks
[86, 94, 79], // Student-1's marks
[84, 90, 75], // Student-2's marks
[79, 75, 89]  // Student-3's marks
]
``````

Now let’s look at different ways to traversing or visiting all the elements in the 2 dimensional array.

## Traversing a 2D Array in Row Major Order

In row-major order, we visit the elements of a 2D array row by row. Starting with the first row, we access all elements from left to right. Then, we move to the next row and repeat the process until all rows have been traversed. This is a natural way to traverse the array if the focus is on operations performed for each student (like calculating overall scores for each student).

Algorithm:

• Use two nested loops: the outer loop to iterate through rows, and the inner loop to iterate through columns within each row.
• The outer loop which iterates through each row starts from 0 (index of the first row) and ends at the index of the last row (number of rows - 1)
• The inner loop which iterates through each column starts from 0 (index of the first column) and ends at the index of the last column (number of columns - 1)
• Process Element: Inside the nested loops, access the current element using the current row and current column indices.

Code:

``````
# Initialize a 2D array containing student marks
students_marks = [
[81, 75, 92],  # Student-0's marks
[86, 94, 79],  # Student-1's marks
[84, 90, 75],  # Student-2's marks
[79, 75, 89]   # Student-3's marks
]

# Iterate through each row (student)
for row_index in range(len(students_marks)):
# Print that the following marks are for the current student
print(f"The following marks are for student-{row_index}:")

# Iterate through each column (subject)
for col_index in range(len(students_marks[row_index])):
# Get the score for the current subject
score = students_marks[row_index][col_index]

# Print the score for the current subject
print(f"The score for subject-{col_index} is {score}")
``````

## Traversing a 2D Array in Column Major Order

In column-major order, we traverse the 2D array by visiting elements column by column. Within each column, we move from the top element down to the bottom element. Once a column is traversed, we move to the next column and repeat the process. This method is helpful if your focus is on operations performed for each subject (like calculating the average score for a subject).

Algorithm:

• Use two nested loops: the outer loop to iterate through columns, and the inner loop to iterate through all rows for each column.
• The outer loop which iterates through each column starts from 0 (index of the first column) and ends at the index of the last column (number of columns - 1)
• The inner loop which iterates through each row starts from 0 (index of the first row) and ends at the index of the last row (number of rows - 1)
• Process Element: Inside the nested loops, access the current element using the current row and current column indices.

code:

``````
# Initialize a 2D array to store student marks
students_marks = [
[81, 75, 92],  # Student-0's marks
[86, 94, 79],  # Student-1's marks
[84, 90, 75],  # Student-2's marks
[79, 75, 89]   # Student-3's marks
]

# Iterate through the columns (subjects)
for col_index in range(len(students_marks[0])):
# Print the subject index
print(f"The following marks are for subject-{col_index}")

# Iterate through the rows (students)
for row_index in range(len(students_marks)):
# Get the score for the current student and subject
score = students_marks[row_index][col_index]

# Print the score for the current student
print(f"The score for student-{row_index} is {score}")
``````