Problem-Solving
Objectives:
By the end of the lesson, students will be able to:
1. Define a problem clearly.
2. Propose and evaluate multiple solutions.
3. Determine the most efficient/best solution.
4. Develop and represent an algorithm for the chosen solution.
Problem-solving process:
Problem-Solving The process of working through details of a problem to reach a solution
- Problem Definition: Clearly stating what the problem is.
- Propose Solutions: Coming up with different ways to solve the problem.
- Evaluate Solutions: Weighing the benefits and drawbacks of each proposed solution.
- Develop Algorithms: Develop and represent an algorithm for the chosen solution.
- Test and validate the solution
Breaking a Problem Into its Parts
- Input: The information or materials needed to start the process.
- Process: The steps taken to convert the input into output.
- Storage: Where information or materials are kept during the process.
- Output: The final result or product of the process.
Discussion: How do we make a sandwich? what steps are involved in making a sandwich?
- Input: Bread, lettuce, tomato, etc.
- Process: Layering the ingredients.
- Output: The completed sandwich.
- Storage: Keeping the sandwich in a container.
What are variables and constants
Variable: An area of storage whose value can change during processing.
- Constant: A value that never changes.
- Examples: Provide programming examples, such as:
- Variable: `score = 0` (the score can change)
- Constant: `PI = 3.14` (the value of PI never changes)
-Integer: A whole number (e.g., -1, 0, 5).
- Floating-point (decimal): A number that can have a decimal point (e.g., 3.14, 0.5).
- Character: A single letter, number, or symbol (e.g., 'A', '1', '$').
-String: A series of Characters
- Literal: A fixed value that is directly used in the code (e.g., 10, 3.14, 'B').
Algorithms
An algorithm is a set of instructions for solving a problem in a finite/limited amount of steps.
Characteristics of an Algorithms
- Finite Number of Steps: Algorithms must have a clear beginning and end.
- Precise: Each step must be clearly defined and unambiguous.
- Flow of Control: There should be a logical sequence that connects each step.
Problem-Solving
- Terminate: An algorithm must eventually come to an end after completing its steps.
Ways of Representing an Algorithm
(1) Pseudocode: Are English-like statements used to represent an algorithm.
(2) Flowchart: A graphical representation of an algorithm.
Example(Pseudocode)
START
READ NUM1,NUM2INPUT STATEMENT
SUM= NUM1+NUM2Assignment Statement
DISPLAY SUM OUTPUT Statement
STOP
Pseudocode key terms/ Commands
- Read/Input: To get user input.
- Store: To save values.
- Write/Output/Display: To show results.
- If-then/If-then-else/NESTEDIF: For conditional statements.
- For loop/While loop: For repeating actions.
-CASES: For different alternatives.
N.B Pseudocode uses both Arithmetic, Relational(<,>,=), Logical Operators (AND,OR,NOT)
FLOWCHART SYMBOLS Example(Flowchart)
START/END
START
Process
READ num1
Decision TRUE READ num2
Sum=num1+num2
NO
Flow
PRINT SUM
Lines
STOP
INPUT/OUTPUT