Data Structures and Algorithms using Python
Copyright © 2023 by Rance Necaise
Table Of Contents
Data Structures and Algorithms using Python
Copyright © 2023
by Rance Necaise

4.2 Working With 2-D Arrays

To illustrate the use of a 2-D array, suppose we have a collection of exam grades stored in a text file for a group of students that we need to process. For example, we may want to compute the average exam grade for each student or the average grade for each exam, or both. A sample text file is illustrated below

7
3          
90    96    92          
85    91    89          
82    73    84  
69    82    86              
95    88    91          
78    64    84          
92    85    89

The file contains the grades for multiple students, each of whom have grades for multiple exams. The first line indicates the number of students for whom we have grades, and the second line indicates the number of exams for which each student has a grade. The remaining lines contain the actual exam grades. Each line contains the grade for an individual student, with the grades listed in exam order.

Since we have multiple grades for multiple students, we can store the grades in a 2-D array in which each row contains the grades for an individual student and each column contains the grades for a given exam. A 2-D array used to store the exam grades from the sample file is illustrated in Figure 4.2.1.

Figure 4.2.1: Exam grades from a text file stored in a 2-D array.

The source listing at the bottom of the page provides the implementation of a program that extracts the exam grades from the text file and stores them into a 2-D array. Notice that we create the array after extracting the first two values from the file. These values indicate the number of students and the number of exams that correspond to the number of rows and columns needed in the array.

After extracting the grades from the file and storing them into the 2-D array, we can now process the grades as needed. In this case, the program computes and displays each student's average exam grade.

Program Listing
Program: procgrades.py
  1. # Uses a 2-D array to processes a collection of exam grades stored in a text file.
  2.  
  3. from ezarray import Array2D
  4.  
  5. # Open the text file for reading.
  6. gradeFile = open(filename, "r")
  7.  
  8. # Extract the first two values which indicate the size of the array.
  9. numStudents = int(gradeFile.readline())
  10. numExams = int(gradeFile.readline())
  11.  
  12. # Create the 2-D array to store the grades.
  13. examGrades = Array2D(numStudents, numExams)
  14.  
  15. # Extract the grades from the remaining lines.
  16. i = 0
  17. for student in gradeFile :
  18.   grades = student.split()    
  19.   for j in range(numExams):
  20.     examGrades[i,j] = int(grades[j])      
  21.   i += 1      
  22.  
  23. # Close the text file.      
  24. gradeFile.close()  
  25.  
  26. # Compute each student's average exam grade.
  27. for i in range(numStudents) :
  28.    # Tally the exam grades for the ith student.
  29.   total = 0
  30.   for j in range(numExams) :
  31.     total += examGrades[i,j]
  32.    
  33.    # Compute average for the ith student.
  34.   examAvg = total / numExams
  35.   print("%2d:  %6.2f" % (i+1, examAvg))
Page last modified on August 25, 2023, at 01:59 PM