CSC121 - Topic 2 Introduction To Problem-Solving and Algorithm Design
CSC121 - Topic 2 Introduction To Problem-Solving and Algorithm Design
INTRODUCTION
TO ALGORITHM
DESIGN AND
DEVELOPMENT
TOPIC 2: INTRODUCTION
TO PROBLEM-SOLVING
AND ALGORITHM DESIGN
1
COURSE OUTLINE
TOPIC 2
● What is a problem and examples of problems?
- Simple Real-world problem/transaction
● What is problem-solving?
● Program development life cycle:
- Problem analysis, Algorithm design, Algorithm implementation, Program
testing and debugging, Program maintenance and documentation
● Details of problem analysis: Input, Process and Output
● Basic concepts of algorithm and algorithm presentation (pseudocode and
flowchart).
● The basic structure/symbols in Pseudocode and flowchart.
WHAT IS A
PROBLEM
AND
EXAMPLES
OF
PROBLEMS?
WHAT IS A PROBLEM?
4
WHAT IS A PROBLEM?
5
EXAMPLES OF PROBLEMS
SIMPLE PROBLEM COMPLEX PROBLEM
• Make a cup of tea. • Traffic light control.
• Cook a pot of rice. • Public transport schedule.
• Log in the email account. • Online transaction payment.
• Unlock the front door. • Recommendation in online
• Switch on a fan or lamp. shopping application.
• Automatic washing machine.
6
WHAT IS PROBLEM-SOLVING?
DEFINITI Solution: an action to solve a problem.
ON Solving a problem: making the problem go away, so that
the it does not exist any longer.
Solving a problem of “making a cup of Solving a problem of “log in Google
tea”: email account”:
Prm maintenance
Algorithm design
and documentation
12
PROGRAM DEVELOPMENT LIFE
2. Algorithm CYCLE
• Develop and carry out the problem solving plan.
Design • Focus on logical solution of the problem.
• Developing an algorithm. An algorithm is the
sequence of steps or rules you follow to solve a
problem.
• Shouldn’t worry about the syntax of any particular
language, but focus on figuring out what sequence of
events will lead from the available input to the
desired output.
• Think carefully about all the possible data values a
13
program might encounter and how the program will
handle each scenario.
PROGRAM DEVELOPMENT LIFE
Steps of 1. Get
CYCLE
information from phase 1.
2. Sketch the plan of problem solving.
Algorithm Design 3. Integrate the logical solution algorithm in the form of
pseudocode or flowchart.
4. If the problem is too complex, break the problem into sub-
problems.
5. Find solutions for each of the sub-problem.
6. Combine the solution for the entire problem.
14
PROGRAM DEVELOPMENT LIFE
CYCLE
3. Algorithm • Choose particular languages. Some have built-in
implementation capabilities that make them more efficient than
others at handling certain types of operations.
• Programming language can handle input
operations, arithmetic processing, output
operations, and other standard functions.
• After choosing a language, the programmer
prepares with proper punctuation and the correct
syntax (spelling of commands).
15
PROGRAM DEVELOPMENT LIFE
Steps of Algorithm CYCLE
Implementation
16
PROGRAM DEVELOPMENT LIFE
4. Program testing CYCLE
• Program testing is the process of executing a
and debugging program with the intent of finding errors.
• A good test is one that has a high probability of
finding an error.
• A program that is free of syntax errors is not
necessarily free of logical errors.
• Once a program is free of syntax errors, the
programmer can test it—that is, execute it with
some sample data to see whether the results are
logically correct.
17
PROGRAM DEVELOPMENT LIFE
CYCLE
Steps of Testing and 1. Check for syntax error.
2. Execute the program with some sample data, to validate
Debugging
the logic of the program.
19
PROGRAM DEVELOPMENT LIFE
5. Program CYCLE
• Maintenance: make necessary changes on the
maintenance & completed program.
• Why? To sustain the capability of a program to
documentation
provide a service.
• For example, new tax rates are legislated, the
format of an input file is altered, or the end user
requires additional information not included in
the original output specifications.
20
PROGRAM DEVELOPMENT LIFE
CYCLE
Steps of Testing and 1. When existing programs are changed, the development
cycle is repeated.
Debugging
2. These elements must be understood:
• the changes
• then plan
• code
3. Then translate and test the program before putting them
into production.
4. If a substantial number of program changes are required,
the original program might be retired, and the program
development cycle might be started for a new program.
21
DETAILS OF
PROBLEM
ANALYSIS:
INPUT,
PROCESS
AND OUTPUT
PROBLEM ANALYSIS
DEFINITI During problem analysis, you should specify
ON requirement as below:
• Input
• Processing
• Output
23
INPUT
24
PROCESS
DEFINITI • A process or running process refers to a set of
ON instructions currently being processed by the computer
processor.
• A process runs in a computer. This can be anything
from a small background task, such as a spell-checker
or system events handler to a full-blown application like
internet browser or word processor applications.
Receive input is a
process
Execute multiplication is a
process
Display the answer is a
25 process
OUTPUT
DEFINITI • The output of a computer or word processor is the
ON information that it displays on a screen or prints on
paper as a result of a particular program.
• There are four basic types of output: audio output,
graphics output, text output, and video output.
26
PROBLEM ANALYSIS: EXAMPLE
EXAMPLE # 1: Compute the sum of 3
numbers
Input Process Output
• Number 1 Sum = Number 1 + Sum
• Number 2 Number 2 +
• Number 3 Number 3
Algorithm:
Input Number1, Number2, Number3
Calculate Sum by
Sum = Number 1 + Number 2 +
Number 3
27 Display Sum
PROBLEM ANALYSIS: EXAMPLE
EXAMPLE # 2: Calculate and display the average mark of three
students Input Process Output
• Mark A Average = (Mark A Average
• Mark B + Mark B + Mark
• Mark C C)/3
Algorithm:
Input Mark A, Mark B, Mark C
Calculate Average by
Adding the numbers and
Sum = Mark A + Mark B + Mark
C
Dividing the sum by 3
28
Average = Sum / 3
Display Average
BASIC
CONCEPTS OF
ALGORITHM
AND
ALGORITHM
PRESENTATIO
N
(PSEUDOCODE
)
PSEUDOCODE
DEFINITI • A step by step problem solving procedure.
ON • Pseudocode is a tool programmers use to help them plan
an algorithm. It is used English like phrases to
described the processing process. It is not standardized
since every programmer has his own way of planning
the algorithm. Criteria of a good pseudocode are:
i. Easy to understand, precise and clear.
ii. Gives the correct solution in all cases.
iii. Eventually ends.
30
PSEUDOCOCE - ALGORITHM
Characteristic of 1. INPUT AND OUTPUT
Algorithm • Algorithm receives input and produces the output
2. UNAMBIGUOUS
• Steps in algorithm must be clear as to what it is
supposed to do and how many times it is expected to
be executed.
3. CORRECT AND EFFICIENT
• An algorithm should produce the correct and efficient
output values for different set of input values.
4. FINITE
• It must execute its instruction and terminate in a finite
time.
31
PSEUDOCOCE - ALGORITHM
Algorithm Solution vs.
Heuristic Solution
ALGORITHM HEURISTIC
SOLUTION SOLUTION
34
FLOWCHART
35
FLOWCHART
36
FLOWCHART
37
FLOWCHART
38
FLOWCHART
SEQUENTIAL
STRUCTURE
• The sequential structure has one
entry point and one exit point.
• No choices are made and no
repetition.
• Statements are executed in
sequence, one after another without
leaving out any single statement.
39
FLOWCHART
SELECTION STRUCTURE
42
FLOWCHART
EXAMPLE # 2: Calculate and display the
average mark of three students
Input Process
Average = Output
Mark A
Mark B (Mark A + Mark Average
Mark C B + Mark C)/3
43
ALGORITHM
EXAMPLE # 1: Input Process Output
Number 1 Sum = Number 1
Compute the sum of Number 2 + Number 2 + Sum
3 numbers Number 3 Number 3
Begin
Input Number1, Number2, Number3
Sum = Number 1 + Number 2 +
Number 3
Display Sum
44
End
FLOWCHART
EXAMPLE # 2: Input Process
Average = Output
Calculate and display Mark A
Mark B (Mark A + Mark Average
the average mark of
Mark C B + Mark C)/3
three students
Begin
Input Mark A, Mark B, Mark C
Sum = Mark A + Mark B + Mark
C
Average = Sum / 3
45 Display Average
End
EXERCISE # 1
46
EXERCISE # 2
47
END OF
TOPIC
THANK YOU!
48