0% found this document useful (0 votes)
3 views7 pages

TD1 ProblemSolving ModularPrograming Solution

The document outlines a series of algorithms for various problem-solving scenarios in programming, focusing on modular programming principles. It includes detailed descriptions of algorithms for parcel delivery, library book borrowing, online order processing, secure login systems, and hospital patient triage, each with defined inputs, outputs, and sub-algorithms. The document emphasizes the benefits of modular design, such as improved clarity, reusability, and easier maintenance.

Uploaded by

brahimsalemfati
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)
3 views7 pages

TD1 ProblemSolving ModularPrograming Solution

The document outlines a series of algorithms for various problem-solving scenarios in programming, focusing on modular programming principles. It includes detailed descriptions of algorithms for parcel delivery, library book borrowing, online order processing, secure login systems, and hospital patient triage, each with defined inputs, outputs, and sub-algorithms. The document emphasizes the benefits of modular design, such as improved clarity, reusability, and easier maintenance.

Uploaded by

brahimsalemfati
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

University of Setif 01 Ferhat Abbas Faculty of sciences, Computer Science Department

Algorithmics and Data Structure 02 First year: Computer sciences, Mathematics,


Programming 02 Engineering, MIAGE, and MDBD
2025/2026 [Link]

Directed Work (DW 1) – Introduction to Modular Programing (Problem Solving)

Part 01: Understand the use of Problem decomposition

Problem Description: A delivery company wants a small program to manage parcel delivery
decisions where: For each parcel, the system must receive the parcel weight and the destination zone.
Using this information, the system determines whether the parcel can be delivered by standard service
(weight ≤ 20 AND zone = "Local") or requires special handling. Finally, the system displays the delivery
decision to the operator.

1- Read and analyse this problem-solving strategy

Algorithm Parcel_Delivery_Decision

Inputs: weight, and zone

Outputs: the type of service

Processing:

Sub-Algorithm:

Sub-Algorithm Read_Parcel_Data

Read (weight)

Read (zone)

Sub-Algorithm Determine_Service

If weight ≤ 20 AND zone = "Local" Then

service ← "Standard Delivery"

Else

service ← "Special Handling Required"

EndIf

Sub-Algorithm Display_Decision

Write (service)

Main Algorithm:

Read_Parcel_Data

Determine_Service

Display_Decision
Questions:

1- How many sub-algorithms can you identify in this program?


Three sub-algorithms:
• Read_Parcel_Data
• Determine_Service
• Display_Decision

2- What task is assigned to each sub-algorithm?

Sub-Algorithm Task

Read_Parcel_Data Collect input data (weight and zone)

Determine_Service Apply decision rule

Display_Decision Present result to the user

3- What is the role of the main algorithm?

The main algorithm coordinates execution of sub-algorithms by calling each each one in the
correct order. It acts as: The controller of the program.

4- Why is reading parcel data separated from deciding the delivery service?
Because:
• Input management and separation logic are different responsibilities.
• Improves clarity.
• Makes modification easier.
• Supports reuse.
5- Why does the main algorithm contain very few instructions?

• It only organizes calls.


• All instructions are already done in the sub parts
• Makes the program easier to read.

6- If the delivery rule changes (for example, the weight limit becomes 25 kg), which sub-algorithm must
be modified?
Modify: Determine_Service
Only the decision rule changes. (If weight ≤ 25 AND zone = "Local" Then)
7- If the way the decision is displayed changes, which sub-algorithm is affected?
Modify: Display_Decision
Instead of directly printing service we can display → Write (“type of delivery service:”, service)
8- Which sub-algorithm could be reused in another delivery-related system?
Read_Parcel_Data
It could be reused in: Shipping systems (Here we can reuse the subprograms is whole new programs
that can be totally different.)
9- What advantages does this organization provide compared to a single long algorithm?
a. Easier to read
University of Setif 01 Ferhat Abbas Faculty of sciences, Computer Science Department
Algorithmics and Data Structure 02 First year: Computer sciences, Mathematics,
Programming 02 Engineering, MIAGE, and MDBD
2025/2026 [Link]

b. Reusable components
c. Better organization
d. Reduces logical complexity

10- How does this decomposition make the program easier to understand and maintain?
• Each module can be changed independently.
• Errors are isolated.
• Testing becomes easier.
• Future updates are safer.

Part 01: Problem Solving (Training scenarios)

For the following scenarios: analyze an extract the inputs, outputs, processing step including sub,
and main parts

This part is dedicated to understand how to apply modular programing to solve different problems
(it is not the final correct structure and standard writing, all this will be seen in the upcoming
sheets)

Scenario A: Library Book Borrowing:

A library wants to manage the borrowing of books by its members. When a member requests a book, the
system must identify the member and the book. It must then verify whether the book is available and
whether the member is allowed to borrow it. If all conditions are satisfied, the book is assigned to the
member and the operation is recorded. Otherwise, an appropriate message is displayed.
Solution: (book, and member can be used as structures)

Algorithm Library_Book_Borrowing
Inputs: member_id, book_id , book_available, member_allowed
Outputs: borrowing_result

Processing:

Sub-Algorithm:

Sub-Algorithm Read_Request
Read(member_id)
Read(book_id)

Sub-Algorithm Verify_Conditions
If book_Available AND member_allowed Then
status ← "Approved"
Else
status ← "Rejected"
EndIf

Sub-Algorithm Assign_Book
If status = "Approved" Then
Assign book_id to member_id
Record borrowing operation
EndIf

Sub-Algorithm Display_Result
If status = "Approved" Then
borrowing_result ← "Book successfully borrowed"
Else
borrowing_result ← "Borrowing not allowed"
EndIf
Write(borrowing_result)

Main Algorithm:
Read_Request
Verify_Conditions
Assign_Book
Display_Result

Scenario B: Online Order Processing:


University of Setif 01 Ferhat Abbas Faculty of sciences, Computer Science Department
Algorithmics and Data Structure 02 First year: Computer sciences, Mathematics,
Programming 02 Engineering, MIAGE, and MDBD
2025/2026 [Link]

An online store wants to process customer orders. For each order, the system receives a list (we can use
arrays) of products and their quantities. It must calculate the total price of the order and verify that
payment conditions are satisfied. Once the order is validated, a confirmation is produced for the customer.

Solution:

Algorithm Online_Order_Processing
Inputs: products[], quantities[], payment
Outputs: order_confirmation

Processing:

Sub-Algorithm:

Sub-Algorithm Read_Order
Read(products[])
Read(quantities[])
Read(payment_info)

Sub-Algorithm Calculate_Total
total ← 0
For i ← 1 to length(products[]) Do
total ← total + (Price(products[i]) × quantities[i])
EndFor

Sub-Algorithm Verify_Payment
If Payment = total Then
payment_status ← "Valid"
Else
payment_status ← "Invalid"
EndIf

Sub-Algorithm Generate_Confirmation
If payment_status = "Valid" Then
order_confirmation ← "Order Confirmed, Total = " + total // (Operation on strings )
Else
order_confirmation ← "Payment Failed. Order Cancelled"
EndIf
Write(order_confirmation)

Main Algorithm:
Read_Order
Calculate_Total
Verify_Payment
Generate_Confirmation
Scenario C: Secure Login System

A digital platform must control access to its services. When a user tries to log in, identification
information is provided. The system must verify the correctness of this information and limit the number
of attempts. Access is granted only when the verification succeeds.

Algorithm Secure_Login_System
Inputs: username, password
Outputs: access_message

Processing:

Sub-Algorithm:

Sub-Algorithm Initialize_System
attempts ← 0
max_attempts ← 3
access_granted ← False

Sub-Algorithm Read_Credentials
Read(username)
Read(password)

Sub-Algorithm Verify_Credentials
If username = Stored_Username AND password = Stored_Password Then
access_granted ← True
Else
attempts ← attempts + 1
EndIf

Sub-Algorithm Display_Access_Status
If access_granted = True Then
access_message ← "Access Granted"
Else
access_message ← "Access Denied"
EndIf
Write(access_message)

Main Algorithm:
Initialize_System

While attempts < max_attempts AND access_granted = False Do


Read_Credentials
Verify_Credentials
EndWhile
Display_Access_Status
University of Setif 01 Ferhat Abbas Faculty of sciences, Computer Science Department
Algorithmics and Data Structure 02 First year: Computer sciences, Mathematics,
Programming 02 Engineering, MIAGE, and MDBD
2025/2026 [Link]

Scenario D: Hospital Patient Triage (Same information as scenario 01 in Data structure DW)

In a hospital emergency unit, patients arrive continuously and must be evaluated quickly. For each patient,
personal information (id, name, Age) and medical symptoms (diagnosis, stress-level) are recorded. Based
on the severity of the condition, a priority is assigned. The system must then present patients and thei
priority.

Algorithm Hospital_Patient_Triage
Inputs: id, name, age, diagnosis, stress_level
Outputs: patient_priority

Processing:

Sub-Algorithm:

Sub-Algorithm Read_Patient_Data
Read(id)
Read(name)
Read(age)
Read(diagnosis)
Read(stress_level)

Sub-Algorithm Determine_Priority
If stress_level ≥ 8 Then
priority ← "High"
Else If stress_level ≥ 5 Then
priority ← "Medium"
Else
priority ← "Low"
EndIf

Sub-Algorithm Store_Patient
Save(id, name, age, diagnosis, stress_level, priority)

Sub-Algorithm Display_Patient_Info
patient_priority ← "Patient " + name + " - Priority: " + priority // (Operation on strings )
Write(patient_priority)

Main Algorithm:
Read_Patient_Data
Determine_Priority
Store_Patient
Display_Patient_Info

You might also like