EX.
NO: 6 STRUCTURED ARRAYS AND AGGREGATE FUNCTIONS
DATE: 07/04/2025
AIM:
To write a Python program to handle a structured array containing student details like
Name, Registration Number, and marks in 5 subjects. Implement aggregation
functions to:
1. Find the total and average marks of each student.
2. Identify the maximum and minimum marks in each subject.
3. Find the class topper.
4. Calculate the pass percentage for each subject.
5. Calculate the class pass percentage.
ALGORITHM:
Step 1: Define the student data structure using NumPy structured array with fields
for Name, Reg_no, and marks in 5 subjects.
Step 2: Initialize the sample data for students with their details and marks in each
subject.
Step 3: Convert the sample data into a structured NumPy array.
Step 4: Initialize an empty list total_marks to store each student's tmotal marks.
Step 5: Loop through each student, extract marks, calculate total and average
marks, append total marks to total_marks, and print the results.
Step 6: Convert total_marks list to a NumPy array.
Step 7: Calculate the class pass percentage based on the number of students who
passed all subjects.
Step 8: Calculate the maximum and minimum marks for each subject and the pass
percentage for each subject.
Step 9: Find the class topper by identifying the student with the highest total marks.
Step 10: End.
Register number: 2127240501011 Page: 11
SOURCE CODE:
import numpy as np
student_dtype = np.dtype([
('Name', 'U50'),
('Reg_no', 'U20'),
('DPSD', 'i4'),
('PDS', 'i4'),
('OOP', 'i4'),
('TD', 'i4'),
('MATHS', 'i4')
])
data = [
('Ananya', 'R123', 80, 90, 85, 75, 88),
('Aruna', 'R124', 70, 80, 78, 85, 72),
('Akshara', 'R125', 95, 85, 90, 92, 91),
('Roshini', 'R126', 30, 35, 40, 41, 68)
]
# Create structured array
students = np.array(data, dtype=student_dtype)
# Initialize an empty list to store total marks of each student
total_marks = []
# Passing mark threshold
passing_marks = 40
pass_all_subjects = 0 # Number of students who passed all
subjects – initialized to zero
print("Total and Average Marks of Each Student:")
Register number: 2127240501011 Page: 12
# Loop through each student
for student in students:
# Access marks for each subject
marks = np.array([
student['DPSD'],
student['PDS'],
student['OOP'],
student['TD'],
student['MATHS']
])
# Check if the student passed all subjects
if np.all(marks >= passing_marks):
pass_all_subjects += 1 # Increment count if student
passed all subjects
# Calculate total and average marks
total = np.sum(marks)
average = total / 5 # 5 subjects
total_marks.append(total)
# Print the result for each student
print(f"{student['Name']} (Reg. No: {student['Reg_no']})")
print(f"\t- Total Marks: {total},")
print(f"\t- Average Marks: {average:.2f}")
# Convert total_marks list to a NumPy array
total_marks = np.array(total_marks)
# Calculate the class pass percentage
Register number: 2127240501011 Page: 13
class_pass_percentage = (pass_all_subjects / len(students)) *
100
print(f"\nClass Pass Percentage: {class_pass_percentage:.2f}%")
# Maximum, Minimum Marks and Pass Percentage for each subject
print("\nMaximum, Minimum, pass % in Each Subject:")
subjects = ['DPSD', 'PDS', 'OOP', 'TD', 'MATHS']
for subject in subjects:
# Access subject marks directly using the field name
subject_marks = students[subject]
# Calculate maximum and minimum marks for the subject
max_marks = np.max(subject_marks)
min_marks = np.min(subject_marks)
# Calculate the pass percentage for the subject
pass_count = np.sum(subject_marks >= passing_marks) # Count
students who passed the subject
pass_percentage = (pass_count / len(students)) * 100 #
Calculate pass percentage
# Print the results
print(f"{subject}")
print(f"\t- Max: {max_marks},")
print(f"\t- Min: {min_marks}")
print(f"\t- Pass Percentage: {pass_percentage:.2f}%")
# Find the class topper by identifying the student with the
highest total marks
max_mark = np.max(total_marks)
index = np.argmax(total_marks) # Use np.argmax to find the
index of the maximum total marks
Register number: 2127240501011 Page: 14
# Print the class topper
print(f"\nClass Topper: {students[index]['Name']} - Total Marks:
{max_mark}")
Register number: 2127240501011 Page: 15
SAMPLE INPUT AND OUTPUT:
RESULT:
Thus, the module to handle structured arrays and implement aggregate
functions has been successfully implemented.
Register number: 2127240501011 Page: 16