Initializing a 2D matrix and accessing, modifying Elements in it

In this article we will go through code examples in different languages to create and initialize a 2 dimensional matrix and to access/modify individual elements.

Consider a use-case where 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

The first row in the above 2D array/matrix corresponds to marks obtained in 3 different subjects by student-0.Similarly the second row corresponds to marks obtained by 3 different subjects by student-1 and so on.

Similarly for each of the students marks, the first element corresponds to marks obtained in subject-0, the second element corresponds marks obtained in subject-1 and so on.

Initializing, Accessing, Modifying elements in 2D Array

Let’s look at code in different languages which does the below:

  • Initialize a multi-dimensional array containing marks of all the students in all the subjects.
  • Print marks obtained by student-2 in each of the subjects.
  • Modify marks obtained by Student-2 in subject-1 to 88

# Declare and initialize a 2D list (4x3 matrix) representing student marks
student_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

# Access and print the marks of a specific student (e.g., student-2)
student_id = 2
print(f"Printing Marks for Student {student_id}:")
print(f"Subject 1: {student_marks[student_id][0]}")
print(f"Subject 2: {student_marks[student_id][1]}")
print(f"Subject 3: {student_marks[student_id][2]}")

# Modify a student's mark 
# e.g., change marks of Student-2 for subject with id 1 to 88
student_id = 2
subject_id = 1
new_mark = 88
student_marks[student_id][subject_id] = new_mark

# Access and print the modified mark
print(f"Modified mark for Student 2 in Subject 1 to {student_marks[student_id][subject_id]}")

If you wish to access marks in subject-1 for student-3, you need to use an expression like student_marks[3][1] to get the marks. You can also use a similar expression to set a specific value: student_marks[3][1] = 92.

Initializing and Filling a 2D array from terminal input

In the previous section, we looked at a scenario where all the values of 2D array are known beforehand and so the 2D array is directly created in code with exact values. But in most of the scenarios, the creation of 2D array will be dynamic and has to be done at runtime based on the number of rows, columns and values provided as input by the user.

The code below helps achieving that by asking the user the number of rows (number of students) and columns (number of subjects) the 2D array should have. It then initializes and resizes the array if required and asks user to fill in contents present in each row until all the rows are filled.

# Declare variables to store number of rows and columns
num_rows, num_cols = 0, 0

# Declare 2D array to store student marks
student_marks = []

# Get number of students
print("Enter the number of students: ")
num_rows = int(input())

# Get number of subjects  
print("Enter the number of subjects: ")
num_cols = int(input())

# Initialize the array
for i in range(num_rows):
  print(f"Filling marks in all subjects for student {i}")
  row = []
  for j in range(num_cols):
    print(f"Enter marks for student {i} in subject {j}: ")
    marks = int(input())
  print(f"Done filling marks in all subjects for student {i}")

In the next section we will go through how to traverse elements in a 2D array in different variations.