Lecture 9: Algorithm Design and Problem-solving I
Abstraction
- Definition: Abstraction is the process of removing unnecessary details to simplify a system or concept.
- Primary Purpose: It hides unnecessary complexity, displaying only the relevant aspects of a concept or system. This allows users or developers to focus strictly on what they need for a specific task.
- Abstraction in Programming: It allows programmers to focus on what a specific unit of code does rather than how it performs that action.
Evolution of Abstraction in High-Level Programming
- First Generation Programs: These were written using machine code, which is the primary language understood by the computer hardware.
- In machine code, instructions are represented using binary digits (s and s).
- Writing in machine code is characterized as being extremely tedious and time-consuming.
- Second Generation Programs: To address the difficulties of machine code, assembly language was introduced.
- In assembly language, instructions are represented using mnemonic codes, which are easier for humans to remember than binary sequences.
- However, assembly is still considered complex because programs are hardware-specific and need to be rewritten to work on different types of computers.
- Third Generation Programs: These utilize high-level languages, which began emerging in the s with languages like BASIC and FORTRAN.
- These languages use algebraic statements such as instead of mnemonics or binary.
- This level of abstraction removes unnecessary details, such as the specific memory locations for variables ( and ) and the internal mechanical details of how the computer carries out addition.
Data Abstraction
- Definition: This involves hiding the intricate details of how data is actually represented within the computer structure.
- Basic Data Types: Programmers use types like
charfor characters orbooleanfor logicalTRUE/FALSEvalues without needing to understand their low-level binary representation. - Abstract Data Types (ADTs): High-level programming enables the use of logical structures like queues and stacks.
- Logical Description: ADTs describe how a programmer views the data and what operations can be performed on it.
- Example (Queue): A programmer can logically add elements to the rear of a queue, remove elements from the front, or specify a maximum size.
- Implementation Independence: The programmer performs operations using functions such as
AddToQueueorRemoveFromQueuewithout needing to know the underlying code implementation.
Problem Decomposition: Top-Down Design
- Necessity of Decomposition: Most non-trivial computational problems must be broken down into smaller sub-problems before they can be effectively solved.
- Top-Down Design Methodology:
- A large problem is first broken down into its major tasks.
- Each major task is further divided into separate subtasks.
- This process continues until every subtask is simple enough to be implemented as a self-contained module or subroutine.
- Menu-Driven Example: In a system presenting a user with a menu, each choice represents a separate, independent module.
- Scalability: While essential for large programs containing millions of lines of code, top-down design is also highly useful for small programs to keep tasks manageable.
Advantages of Problem Decomposition
- Ease of Writing: It makes the actual task of writing program code much easier by focusing on small components.
- Simpler Testing and Maintenance: It simplifies the debugging and upkeep phases of the software development lifecycle.
- Modularity: Because each module is independent, it can be modified or updated without having unintended negative effects on other parts of the overall program.
Algorithms
- Definition: An algorithm is a set of rules or a sequence of defined steps used to solve a problem.
- Real-World Examples of Algorithms:
- A recipe for baking a chocolate cake.
- A knitting pattern used to create a sweater.
- A set of paths or directions to travel between two geographic locations.
- Core Components: Every algorithm involves Input, Processing, and Output.
- Execution Pipeline: In computer science, an algorithm is converted into program code, which is then translated into machine code for execution by the computer.
Pseudocode: Representation and Logic
- Nature of Pseudocode: It is a tool used for developing algorithms that acts as a link between natural language (English) and formal program statements.
- Syntax Rules: There are no concrete rules or rigid syntax for writing pseudocode; it can be written in various forms.
- Standardized Approach: Following a standard approach (as used in this course) allows for easy translation into formal languages like Python.
- Input and Output Examples:
print ("What is your name?")// Displays text on the screen.myname = input ()// Waits for user input and assigns it to the variablemyname.print("Hello, ", myname)// Displays a greeting incorporating the stored variable.
- Combined Statements: Input and print prompts can be combined:
myname = input ("What is your name?"). This displays the prompt and waits for the user to press the ENTER key after typing.
Data Types and Arithmetic in Pseudocode
- Common Data Types:
- integer: Whole numbers (e.g., , , , ).
- real/float: Numbers containing a fractional part (e.g., , , ).
- boolean: Values that are either
TRUEorFALSE. - character: Single letters, numbers, or special symbols often represented in ASCII.
- string: Any sequence enclosed in quotation marks (e.g.,
"Peter","123","This is a string").
- Arithmetic Operators:
- Addition:
+ - Subtraction:
- - Multiplication:
* - Division:
/
- Addition:
- Calculation Example (Restaurant Bill):
- If a bill totals pounds ():
- Shared among friends:
Billshared4 = bill/4results in . - Shared among friends:
Billshared3 = bill/3results in .
Variables and Identifiers
- Variable Definition: An identifier or name assigned to a memory location whose content may change during program execution.
- Naming Standards:
- Names should typically start with a letter or an underscore (
_). - The remaining characters can be letters, numbers, or underscores.
- Variables must not contain spaces or special characters.
- Use meaningful names (e.g.,
studentName) instead of generic labels like , , and to ensure the program is understandable. - Multi-person programming teams benefit from adopting a unified naming standard.
- Names should typically start with a letter or an underscore (
Identifier Table
An identifier table helps document the variables used in a program. Below is an example based on student records:
- Identifier name:
studentName| Data type:String| Description: Student’s name - Identifier name:
studentAge| Data type:integer| Description: Student’s age - Identifier name:
mathScore| Data type:float| Description: Student’s score in math
Reference Material
- This lecture aligns with chapters , , and of the OCR AS and A Level Computer Science Textbook.