Skip to content

2.1.3 Thinking procedurally

Paper 2 · Algorithms & Programming · Section 2.1 · 12 flashcards · 9 questions

Problem decomposition into sub-problems, using sub-procedures, interface design, parameter passing

2.1.3 Thinking Procedurally

2.1.3(a) Identify the Components of a Problem

Definition
Problem Decomposition is the process of breaking down complex problems into smaller, more manageable components. This approach simplifies the design and implementation of solutions.

Top-Down Design / Stepwise Refinement

Key Point
StepWhat to do
1Identify major components of the problem
2Break each component into subcomponents
3Continue refining until tasks are small enough to implement
4Repeat/iterate (top-down design / stepwise refinement)
Worked Example
Worked Example: Adventure Game Decomposition

Overall problem: Create an adventure game.
LevelComponentSubcomponents/examples
1CharactersPlayer, NPCs, enemies
1EnvironmentsLevel design, backgrounds, interactive objects, collision
1Game mechanicsCombat, scoring, quests, progression
1User interfaceMenus, HUD, inventory screen
2 (Characters)Player characterAppearance, stats, inventory
2 (Characters)NPCsDialogue, quest logic
2 (Characters)EnemiesAI behaviour, combat stats
This hierarchical breakdown lets developers focus on one manageable part at a time.

2.1.3(b) Identify the Components of a Solution to a Problem

Definition
Once the problem has been decomposed, the next step is to determine how each component will be solved. This involves specifying the methods, functions, and procedures needed to address each subproblem.
Worked Example
Worked Example: Library Book Reservation System
ComponentPurposeExample routine
User identificationConfirm who is making reservationgetUserName()
Book selectionFind the target book recordgetBookISBN(bookTitle)
Availability checkingEnsure copies are in stockcheckAvailability(ISBN)
Collection locationLet user choose pickup branchselectLocation()
Reservation confirmationSave reservation and confirmconfirmReservation(userID, ISBN, location)
text
function getUserName():
    if userIsSignedIn() then
        return getNameFromDatabase()
    else
        promptLoginOrRegister()
        return null
    end if
end function

function getBookISBN(bookTitle):
    bookRecord = searchDatabase(bookTitle)
    return bookRecord.ISBN
end function

function checkAvailability(ISBN):
    stockCount = queryDatabase("SELECT stock FROM books WHERE isbn = " + ISBN)
    return stockCount > 0
end function

function selectLocation():
    displayMenu(["Main Branch", "North Branch", "East Branch"])
    return getUserSelection()
end function

procedure confirmReservation(userID, ISBN, location):
    writeToDatabase(userID, ISBN, location, currentDate())
    displayConfirmation()
end procedure

2.1.3(c) Determine the Order of the Steps Needed to Solve a Problem

Definition
After identifying components, it is crucial to determine the sequence in which steps should be executed, considering dependencies between subprocedures.
Key Point
Sequencing ruleWhy it matters
Respect dependenciesSome tasks require earlier outputs (e.g. ISBN before stock check)
Parallelise where possibleIndependent tasks can run simultaneously
Validate input earlyPrevents invalid data flowing through later stages
Authenticate before privileged actionsProtects security and user-specific operations
Worked Example
Worked Example: Book Reservation System Flow
text
// Sequential steps (dependencies exist)
procedure makeReservation():
    // Step 1: User must sign in first
    userID = authenticateUser()
    if userID is null then
        print("Please sign in")
        return
    end if
    
    // Step 2: Get book details
    bookTitle = input("Enter book title: ")
    ISBN = getBookISBN(bookTitle)
    
    // Step 3: Check availability (depends on ISBN)
    if NOT checkAvailability(ISBN) then
        print("Book not available")
        return
    end if
    
    // Step 4: Select location (depends on book being available)
    location = selectLocation()
    
    // Step 5: Confirm (depends on all previous steps)
    confirmReservation(userID, ISBN, location)
end procedure
Step Dependencies:
Step orderDependency reason
User auth → Book selectionMust know who is reserving before creating reservation
Book selection → Availability checkNeed ISBN/book first to check stock
Availability check → LocationOnly ask for pickup location if book is available
Location → ConfirmationFinal record requires all previous values
Wrong-order examples:
• Checking availability before selecting a book fails
• Confirming before authentication creates security risks

2.1.3(d) Identify Sub-Procedures Necessary to Solve a Problem

Definition
Sub-procedures are the building blocks of a program - small, well-defined tasks that can be reused and tested independently. Identifying and creating these is essential for efficient, modular program design.

Benefits of Sub-Procedures

Key Point
AdvantagePractical benefit
ModularityComponents can be built independently
ReusabilitySame routine can be reused across features
TestabilityEach routine can be unit tested in isolation
MaintainabilityFix once in one procedure, benefit everywhere
ReadabilitySmaller named routines make code easier to follow
Team collaborationDifferent developers can own different procedures
Worked Example
Worked Example: Library System Sub-Procedures
text
// Sub-procedure 1: Get user name
function getName():
    if isSignedIn() then
        return currentUser.name
    else
        return null
    end if
end function

// Sub-procedure 2: Get book ISBN
function getISBN(bookTitle):
    book = database.findBook(bookTitle)
    return book.ISBN
end function

// Sub-procedure 3: Check availability
function checkAvailability(ISBN):
    return database.getStock(ISBN) > 0
end function

// Sub-procedure 4: Select location
function selectLocation():
    return displayLocationMenu()
end function

// Sub-procedure 5: Confirm reservation
procedure confirmReservation(userID, ISBN, location):
    database.createReservation(userID, ISBN, location)
end procedure

// Main procedure combining all sub-procedures
procedure main():
    name = getName()
    ISBN = getISBN(input("Book title: "))
    if checkAvailability(ISBN) then
        location = selectLocation()
        confirmReservation(name, ISBN, location)
    end if
end procedure
Each sub-procedure can be developed, tested, and debugged independently before integration.
Exam Tip
Exam Tips for Thinking Procedurally:
• Be able to decompose a problem into components
• Identify appropriate sub-procedures for given scenarios
• Understand the order/sequence of steps
• Recognise dependencies between procedures
• Explain benefits of decomposition (modularity, testing, teamwork)
• Write pseudocode for sub-procedures with clear inputs/outputs