0% found this document useful (0 votes)
40 views6 pages

Python Structured Arrays Algorithm

The document outlines a Python program that manages a structured array of student details and implements various aggregation functions. It calculates total and average marks for each student, identifies maximum and minimum marks in each subject, determines the class topper, and computes pass percentages. The implementation uses NumPy for data handling and includes a detailed algorithm and source code for execution.

Uploaded by

AnanyaKannan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views6 pages

Python Structured Arrays Algorithm

The document outlines a Python program that manages a structured array of student details and implements various aggregation functions. It calculates total and average marks for each student, identifies maximum and minimum marks in each subject, determines the class topper, and computes pass percentages. The implementation uses NumPy for data handling and includes a detailed algorithm and source code for execution.

Uploaded by

AnanyaKannan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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 = [Link]([
('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 = [Link](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 = [Link]([
student['DPSD'],
student['PDS'],
student['OOP'],
student['TD'],
student['MATHS']
])

# Check if the student passed all subjects


if [Link](marks >= passing_marks):
pass_all_subjects += 1 # Increment count if student
passed all subjects

# Calculate total and average marks


total = [Link](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 = [Link](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 = [Link](subject_marks)
min_marks = [Link](subject_marks)

# Calculate the pass percentage for the subject


pass_count = [Link](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 = [Link](total_marks)
index = [Link](total_marks) # Use [Link] 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

Common questions

Powered by AI

The program uses a defined passing mark threshold to evaluate whether a student passes each subject. By checking if marks are greater than or equal to this threshold, it appropriately handles edge cases where students have marks exactly equal to the passing criteria. This ensures fair assessment without manual adjustments .

The program calculates the total marks for each student by summing up their marks in all five subjects using NumPy's sum function. The average is then computed by dividing the total marks by the number of subjects, which is five .

The pass percentage for each subject is calculated by counting how many students scored at or above the passing mark in that subject, dividing this count by the total number of students, and then multiplying by 100. This gives the percentage of students who passed that particular subject .

NumPy functions like sum, max, min, and argmax are optimized for performance and efficiency when handling numeric arrays. They can perform operations on entire arrays without explicit loops, reducing computational time and making the code concise and maintainable. These advantages are particularly useful in processing large data .

A student is considered to have passed all subjects if they receive marks greater than or equal to the passing mark threshold in each of the five subjects. This is checked using NumPy's all function to verify that all conditions are satisfied .

The program accesses each subject's marks directly using the field name in the structured array. It then utilizes NumPy's max function to determine the maximum marks and the min function to find the minimum marks for each subject .

The class pass percentage is calculated by dividing the count of students who passed all subjects by the total number of students, then multiplying by 100 to get a percentage. A student is considered to have passed if they score above the threshold in all subjects .

Structured arrays allow the storage of heterogeneous data types, which is ideal for student data that includes both numerical scores and string information like names and registration numbers. It supports easy access and manipulation of individual records using field names, thus making operations like aggregation straightforward .

The program determines the class topper by first finding the student with the highest total marks using NumPy's argmax function, which gives the index of the maximum value in the total marks array. The topper's details are then accessed using this index .

For significantly larger data sets, the program may face challenges such as increased computation time and memory usage, as NumPy operations on large arrays can become resource-intensive. Additionally, scalability in terms of managing structured array fields may necessitate more sophisticated data structures or external database management solutions to efficiently handle increases in both student counts and the number of subjects .

You might also like