0% found this document useful (0 votes)
46 views

CSC121 - Topic 2 Introduction To Problem-Solving and Algorithm Design

The document discusses problem solving and algorithm design including defining a problem, problem solving, and the program development life cycle which consists of problem analysis, algorithm design, implementation, testing, debugging, and maintenance.

Uploaded by

muhaiminhaziq25
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

CSC121 - Topic 2 Introduction To Problem-Solving and Algorithm Design

The document discusses problem solving and algorithm design including defining a problem, problem solving, and the program development life cycle which consists of problem analysis, algorithm design, implementation, testing, debugging, and maintenance.

Uploaded by

muhaiminhaziq25
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 48

CSC121:

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?

DEFINITI A state of difficulty that needs to be resolved or.


ON A question raised for consideration or solution.
A difficulty: a matter about something difficult to decide
on what to do. A question to be answered or solve.

A problem, can be caused for different reasons, and


usually can be solved in a number of different ways.

4
WHAT IS A PROBLEM?

TYPES KNOWLEDGE-LEAN PROBLEMS


• Can be solved (though not always skillfully) by use of
OF instructions for the task and general problem solving skills
PROBLE • Ex: finding a parking space in the mall, shampooing hair.
M
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?
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”:

1. Put a teabag into a cup. 1. Go to mail.google.com


2. Pour boiled water into the 2. Enter email ID
cup. 3. Enter password
3. Add sugar and milk into the 4. Enter or click on login button.
cup.
4. Stir.
7
WHAT IS PROBLEM-SOLVING?
How to draw your money at ATM machine?
# PROBLEMS EXAMPLE
1 Transaction ATM Machine, Web 1. Insert ATM card in ATM machine
Application
2 Decision Forecasting 2. Choose a language
Making
3. Enter pin number of your ATM card
3 Control Traffic Controller
problems
4. Choose a withdrawal process
4 Searching Search Engines
problems 5. Enter the amount of money you want to
5 Sorting Transport Schedule withdraw
problems
6. Take your ATM card. Wait for your money
and transaction slip to come out.
8
PROGRAM
DEVELOPME
NT LIFE
CYCLE
PROGRAM DEVELOPMENT LIFE
CYCLE Problem analysis

Prm maintenance
Algorithm design
and documentation

Program testing Algorithm


10
and debugging implementation
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 this
phase.
Steps of Problem 1. Determine required information.
2. List all problem facts. Determine how the fact can be used in the
Analysis solution.
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 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.

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
18
error.
PROGRAM DEVELOPMENT LIFE
CYCLE
Steps of Testing and 4. After the program is thoroughly tested and debugged, put
the program into production.
Debugging
i. Data-entry training.
ii. End-user training.
iii. Data migration and conversion.

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

Input Process Output


Data (Algorithm) Information

23
INPUT

DEFINITI • Data that is entered into or received by a computer.


ON • It is sent to a program for processing.

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

A series of actions / A solution that cannot be


steps (algorithm) to solve reached through a direct
a problem. of steps and require
reasonable built on
knowledge and
experience. (AI)
32
THE BASIC
STRUCTURE /
SYMBOLS IN
PSEUDOCOD
E&
FLOWCHART
FLOWCHART
DEFINITI • Flowchart graphically shows the logic in solution
ON 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 statement.
39
FLOWCHART
SELECTION STRUCTURE

• The selection structure is used to


allow choices to be made.
• The program executes particular
statements depending on some
condition(s).
• C++ uses the if-else and switch
statement for making decision.
40
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.
41
FLOWCHART
EXAMPLE # 1: Compute the sum of
3 numbers

Input Process Output


Number 1 Sum = Number 1
Number 2 + Number 2 + Sum
Number 3 Number 3

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

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. Display the status of a student based on the mark below

46
EXERCISE # 2

1. Write a pseudocode to calculate and display the


price of a number of apples if the quantity n kg and
price per kg are given.
2. Write a pseudocode to compute the area of a square.

47
END OF
TOPIC

THANK YOU!

48

You might also like