Csc121 - Topic 2 Introduction to Problem-solving and Algorithm Design (1)
Csc121 - Topic 2 Introduction to Problem-solving and Algorithm Design (1)
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
• Unlock the front door. payment.
• Switch on a fan or lamp. • Recommendation in online
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 Solving a problem of “log in
cup of tea”: Google email account”:
Program
maintenance Algorithm
and design
documentation
Program Algorithm
testing and implementatio
debugging n
10
PROGRAM DEVELOPMENT LIFE
1 Problem CYCLE
• Understanding the problem.
• Purpose: clearly analyse the problem.
. Analysis
• Try to break up the problem into
smaller meaningful workable pieces of
information.
• Solution: depends on the outcome from
Steps of 1. Determine required information.
2. this
List allphase.
problem facts. Determine how the fact can be
Problem used in the solution.
Analysis 3. Determine assumption to be used. Avoid irrelevant or
over assumptions.
4. Determine the data to be used.
5. Determine input data from users.
11
6. Determined formula to be used.
7. Design the expected screen appearance.
PROGRAM DEVELOPMENT LIFE
TERM DEFINITIONCYCLE
Information Meaningful interpretation of data
Data Raw material used to get the information.
Formula An expression that tells the computer what mathematical
operation to perform upon a specific value.
Expected Communication channel between the program built and
screen the real user. It is used to allow users and computer
appearance program to communicate to each other. Ex: response
messages, the design of user interface.
12
PROGRAM DEVELOPMENT LIFE
2 Algorithm CYCLE
• Develop and carry out the problem solving
. Design plan.
• 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
13
14
PROGRAM DEVELOPMENT LIFE
CYCLE
3 Algorithm • Choose particular languages. Some
. implementati have built-in capabilities that make
on 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
15 programmer prepares with proper
punctuation and the correct syntax
PROGRAM DEVELOPMENT LIFE
Steps of CYCLE
Algorithm
Implementatio
n
16
PROGRAM DEVELOPMENT LIFE
4 Program CYCLE
• Program testing is the process of
. testing and executing a program with the intent of
debugging 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
17
see whether the results are logically
PROGRAM DEVELOPMENT LIFE
Steps of CYCLE
1. Check for syntax error.
2. Execute the program with some sample data,
Testing and
to validate the logic of the program.
Debugging
19
PROGRAM DEVELOPMENT LIFE
5 Program CYCLEmake necessary changes
• Maintenance:
. maintenance on the completed program.
• Why? To sustain the capability of a
&
program to provide a service.
documentati • For example, new tax rates are
on 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
Steps of CYCLE
1. When existing programs are changed, the
development cycle is repeated.
maintenance
2. These elements must be understood:
and • the changes
documentation • 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
24
PROCESS
DEFINITI • A process or running process refers to a set
ON 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
Receive input is a
applications. process
Execute multiplication is a
process
Display the answer is a
25 process
OUTPUT
DEFINITI • The output of a computer or word processor
ON 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 Sum = Sum
1 Number 1 +
• Number Number 2 +
2 Number 3
Algorithm:
• Number
3
Input Number1, Number2,
Number3
Calculate Sum by
Sum = Number 1 +
27 Number 2 + Number 3
PROBLEM ANALYSIS: EXAMPLE
EXAMPLE # 2: Calculate and display the average
mark of three
Input
students
Process Output
• Mark A Average = Average
• Mark B (Mark A + Mark
• Mark C B + Mark 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
AND
FLOWCHART)
1. INPUT AND OUTPUT
• Algorithm receives input and produces the
output
2. UNAMBIGUOUS
• Steps in algorithm must be clear as to what
Characterist it is supposed to do and how many times it
ic of is expected to be executed.
3. CORRECT AND EFFICIENT
Algorithm • 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.
30
THE BASIC
STRUCTURE
/ SYMBOLS
IN
PSEUDOCOD
E&
FLOWCHART
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.
32
FLOWCHART
DEFINITI • Flowchart graphically shows the logic in
ON 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.
33
FLOWCHART
34
FLOWCHART
35
FLOWCHART
36
FLOWCHART
37
FLOWCHART
SEQUENTIAL
• The sequential structure has
STRUCTURE
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
38
single statement.
FLOWCHART
• The selection structure is
SELECTION
used toSTRUCTURE
allow choices to be
made.
• The program executes
particular statements
depending on some
condition(s).
• C++ uses the if-else and
switch statement for
39
making decision.
FLOWCHART
REPETITION / LOOP
STRUCTURE
• Statements are executed
repeatedly while certain
condition remains true.
• In C++, while, do-while and
for are the statements
commonly used within the
repetition structure.
40
ALGORITHM (PSEUDOCODE)
EXAMPLE # 1: Input Process Output
Numbe Sum =
Compute the r1 Number 1 Sum
sum of Numbe + Number 2 +
3 numbers r2 Number 3
Numbe
Begin r3
Input Number1, Number2, Number3
Sum = Number 1 + Number 2 + Number 3
Display Sum
41
End
ALGORITHM (FLOWCHART)
EXAMPLE # 1: Compute the
sum of 3 numbers
42
ALGORITHM (PSEUDOCODE)
EXAMPLE # 2: Input Process
Average = Output
Calculate and Mark A
Mark B (Mark A + Average
display the
Mark C Mark B +
average mark of Mark C)/3
three students
Begin
Input Mark A, Mark B, Mark C
Sum = Mark A + Mark B + Mark C
Average = Sum / 3
Display Average
43
End
ALGORITHM (FLOWCHART)
EXAMPLE # 2: Calculate and
display the average mark of
three students
Input Process
Average = Output
Mark A
Mark B (Mark A + Average
Mark C Mark B +
Mark C)/3
44
EXERCISE # 1
1. Draw a flowchart to calculate and display the
price of a number of apples if the quantity n kg
and price per kg are given.
2. Draw a flowchart to compute the area of a
square
3. Draw a flowchart to display the status of a
student based on the mark below
45
EXERCISE # 2
46
END OF
TOPIC
THANK YOU!
47