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?
KNOWLEDGE-RICH PROBLEMS
• Requires specific knowledge or skill to solve the problem
• Ex: calculus, computer-programming problems
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?
DEFINITION Solution: an action to solve a problem.
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”:
Program
maintenance and Algorithm design
documentation
13
PROGRAM DEVELOPMENT LIFE CYCLE
Steps of 1. Get 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 CYCLE
Steps of Algorithm
Implementation
16
PROGRAM DEVELOPMENT LIFE CYCLE
4. Program testing • 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.
Debugging 2. Execute the program with some sample data, to validate
the logic of the program.
If you execute the program, provide the value 2 as input to the program,
and the answer 4 is displayed, you have executed one successful test run
of the program. However, if the answer 40 is displayed, maybe the
program contains a logical error.
18
PROGRAM DEVELOPMENT LIFE CYCLE
Steps of Testing and 4. After the program is thoroughly tested and debugged,
Debugging put the program into production.
i. Data-entry training.
ii. End-user training.
iii. Data migration and conversion.
19
PROGRAM DEVELOPMENT LIFE CYCLE
5. Program • Maintenance: make necessary changes on the
maintenance & completed program.
documentation • Why? To sustain the capability of a program to
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
Debugging cycle is repeated.
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
DEFINITION During problem analysis, you should specify
requirement as below:
• Input
• Processing
• Output
23
INPUT
24
PROCESS
DEFINITION • A process or running process refers to a set of
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
DEFINITION • The output of a computer or word processor is the
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 C)/3
• Mark C
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
Average = Sum / 3
28
Display Average
BASIC
CONCEPTS OF
ALGORITHM
AND
ALGORITHM
PRESENTATION
(PSEUDOCODE)
PSEUDOCODE
DEFINITION • A step by step problem solving procedure.
• 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 SOLUTION HEURISTIC SOLUTION
32
THE BASIC
STRUCTURE /
SYMBOLS IN
PSEUDOCODE
& FLOWCHART
FLOWCHART
DEFINITION • Flowchart graphically shows the logic in solution
algorithm that produce in early 1960s.
• It shows the step by step solution using symbols which
represent a task.
• The symbols used consist of geometrical shapes that
are connected by flow lines.
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
39
statement.
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 B Average
Mark C + 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: Calculate Input Process
Average = Output
and display the average Mark A
Mark B (Mark A + Mark B Average
mark of three students
Mark C + Mark C)/3
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