RK

IGCSE Computer Science - Algorithms

Designing Algorithms

  • Definition of an Algorithm:

    • An algorithm is a precise set of rules or instructions to solve a specific problem or task.
  • Main Methods of Designing Algorithms:

    • Structure Diagrams
    • Flowcharts
    • Pseudocode

Structure Diagrams

  • Definition:

    • Structure diagrams illustrate hierarchical top-down design visually.
  • Functionality:

    • Problems are divided into sub-problems, and sub-problems can be further divided.
    • Each level of the diagram specifies tasks suitable for a single subroutine.
  • Example:

    • A structure diagram for a mobile application would show components such as user interface, database, and processing tasks.

Flowcharts

  • Definition:

    • Flowcharts are visual representations using shapes to describe an algorithm.
  • Components:

    • Inputs and outputs
    • Process steps
    • Decisions and repetitions
    • Lines connecting shapes indicate flow of control.

Example Flowchart Scenario

  • A casino application where the user inputs their first name to receive a greeting upon accessing the site.

Pseudocode

  • Definition:

    • Pseudocode is a text-based tool that describes an algorithm using simple English statements.
  • Advantages:

    • More structured than regular sentences, yet flexible for adjustments.
  • Example Scenario:

    • A casino program asking for the user's age:
    • Initial Pseudocode:
      INPUT Age IF Age >= 18 THEN OUTPUT "Welcome to the site" ELSE OUTPUT "Sorry, this site is for users 18 and over" ENDIF
    • Refined Version with user’s first name input as well:
      INPUT FName INPUT Age IF Age >= 18 THEN OUTPUT "Welcome to the site, " + FName ELSE OUTPUT "Sorry, this site is for users 18 and over" ENDIF

Explaining Algorithms

  • Purpose:

    • The goal of an algorithm is to solve a problem, enabling users unfamiliar with it to interpret its purpose.
  • Interpretation Tools:

    • Flowcharts
    • Pseudocode
    • High-level programming languages (e.g., Python)
  • Complex Algorithms:

    • Utilize comments in the code, consider context, and test the algorithm with various inputs to understand it better.
  • Example to Explain:

    • Pseudocode Example:
      Count ← 1 Number ← 0 Total ← 0 REPEAT INPUT Number Total ← Total + Number Count ← Count + 1 UNTIL Count > 10 OUTPUT Total
    • What it does:
    • Adds ten user-entered numbers and outputs their total.
    • Key Processes:
      • Initialize variables (Count, Number, Total)
      • Inputting user number
      • Adding to Total and Count.

Worked Example in Pseudocode

  • Purpose:

    • A teacher's algorithm for entering student marks and grading them.

    • Pseudocode:

    Count ← 0
    REPEAT
        INPUT Score[Count]
        IF Score[Count] >= 70 THEN
            Grade[Count] ← "A"
        ELSE  
            IF Score[Count] >= 60 THEN
                Grade[Count] ← "B"
            ELSE
                IF Score[Count] >= 50 THEN
                    Grade[Count] ← "C"
                ELSE
                    IF Score[Count] >= 40 THEN
                        Grade[Count] ← "D"
                    ELSE
                        IF Score[Count] >= 30 THEN
                            Grade[Count] ← "E"
                        ELSE
                            Grade[Count] ← "F"
                        ENDIF
                    ENDIF
                ENDIF
            ENDIF
        ENDIF
        Count ← Count + 1
    UNTIL Count = 30
    
  • Explanation of the Algorithm:

    • Inputs for 30 student marks stored in Score[].
    • Each mark checked against specific boundaries for grading.
    • Matching grades assigned and stored in Grade[] at the same index as input marks.
    • The process stops after all marks are inputted successfully.