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

Chap 02 - Intro To Problem-Solving and Algorithm Design

Uploaded by

Hilman Syahiran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views

Chap 02 - Intro To Problem-Solving and Algorithm Design

Uploaded by

Hilman Syahiran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Topic 2 :

Introduction to
Problem-Solving and
Algorithm Design
CSC121 – Introduction to Algorithm Design and Development

Nor Hasnul Azirah Abdul Hamid


Table of Contents

01 What is a Problem and 04 Details of Problem Analysis :


Examples of Problems Input, Process and Output

02 05 Basic Concepts of Algorithm


What is Problem Solving?
and Algorithm Presentation

03 Program Development Life


Cycle
01
What is a Problem and
Examples of Problems
1.0 What is a Problem?

DEFINITION :
● A state of difficulty that needs to be resolved or.
● 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.
1.1 Types of Problems

Types of Problems :

KNOWLEDGE-LEAN KNOWLEDGE-RICH
PROBLEMS PROBLEMS

• Can be solved (though not • Requires specific knowledge


always skillfully) by use of or skill to solve the problem
instructions for the task and • Ex: calculus, computer-
general problem solving programming problems
skills
• Ex: finding a parking space
in the mall, shampooing hair.
1.3 Examples of Problems?

Examples of Problems :

SIMPLE PROBLEM COMPLEX PROBLEM

• Traffic light control


• Make a cup of tea
• Public transport schedule
• Cook a pot of rice
• Online transaction payment
• Log in the email account
• Recommendation in online
• Unlock the front door
shopping application
• Switch on a fan or lamp
• Automatic washing machine
1.3 Examples of Problems?

Typical Real Life World Problems :

Transaction Decision Making


• ATM Machine • Forecasting
• Web Application

Control Problem Searching problems Sorting Problem

• Traffic Controller • Search Engines • Transport Schedule


02
What is Problem Solving?…
2.0 What is Problem Solving?

DEFINITION :
● Solution: an action to solve a problem.
● Solving a problem: making the problem go away, so that it does not
exist any longer.

Solving a problem of “making a cup Solving a problem of “log in Google


of tea”: email account”:

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


2. Pour boiled water into the cup. 2. Enter email ID
3. Add sugar and milk into the cup. 3. Enter password
4. Stir. 4. Enter or click on login button.
2.1 Problem Solving Strategies

Two problem solving strategies:

Algorithmic Solutions Heuristic Solutions

 Solutions that can be  Solutions that cannot be


reached through a direct
reached in a series of
set of steps
steps
 Guaranteed to give the  Involves intelligent trial
and error or reasoning
correct answer
based on knowledge and
experience
2.2 Problem Solving with Computers

● In computing, use algorithmic solutions where solutions are


expressed as algorithms.
● The algorithm used can create a solution that give the correct
result.

● Algorithms
○ A step by step procedures that accepts data, manipulate them
based on the prescribed steps, to achieve the desired value(s)
or tasks.
03
Program Development Life Cycle
3.0 Program Development Life Cycle

05
Program
Maintenance &
Documentation 01
Problem
Analysis

04
Program
Testing &
Debugging
02
Algorithm
Design
03
Algorithm
Implementation
3.1 Program Development Life Cycle :
Problem Analysis

● Understanding the problem.


● Purpose : clearly analyze the problem.
● Try to break up the problem into smaller
meaningful workable pieces of information.
● Solution : depends on the outcome from this
phase.
3.1 Program Development Life Cycle :
Problem Analysis
1) Determine required information.
2) List all problem facts. Determine how the fact can be used in the solution.
Steps of 3) Determine assumption to be used. Avoid irrelevant or over assumptions.
4) Determine the data to be used.
Problem
5) Determine input data from users.
Analysis
6) Determined formula to be used.
7) Design the expected screen appearance.

Information Data Formula Expected screen appearance

Meaningful Raw material An expression that • Communication channel between the


interpretation of used to get the tells the computer program built and the real user.
data information what mathematical • To allow users and computer program to
operation to perform communicate to each other.
upon a specific value • Ex: response messages, the design of
user interface
3.2 Program Development Life Cycle :
Algorithm Design

● Develop and carry out the problem solving 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.
● Should not 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
program might encounter and how the program will
handle each scenario.
3.2 Program Development Life Cycle :
Algorithm Design

1) Get information from phase 1.


2) Sketch the plan of problem solving.
3) Integrate the logical solution algorithm in the form of
Steps of pseudocode or flowchart.
Algorithm 4) If the problem is too complex, break the problem into sub-
Design problems.
5) Find solutions for each of the sub-problem.
6) Combine the solution for the entire problem.
3.3 Program Development Life Cycle :
Algorithm Implementation

● Choose particular languages. Some have built-in


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).
3.3 Program Development Life Cycle :
Algorithm Implementation

Steps of
Algorithm
Implementation
3.4 Program Development Life Cycle :
Program Testing and Debugging

● Program testing is the process of executing a 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.
3.4 Program Development Life Cycle :
Program Testing and Debugging
1) Check for syntax error.
Steps of 2) Execute the program with some sample data, to validate the
Program
logic of the program.
Testing and
Debugging 3) After the program is thoroughly tested and debugged, put the
program into production.

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.
3.5 Program Development Life Cycle :
Program Maintenance and Documentation

● Maintenance : make necessary changes on the


completed program.
● 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.
3.5 Program Development Life Cycle :
Program Maintenance and Documentation
1) When existing programs are changed, the development
cycle is repeated.
2) These elements must be understood :
Steps of ○ the changes
Program ○ then plan
Maintenance ○ code
and 3) Then translate and test the program before putting them
Documentation 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.
04
DETAILS OF PROBLEM ANALYSIS:
INPUT, PROCESS AND OUTPUT
4.0 Details of Problem Analysis

● During problem analysis, you should specify


requirement as below:
○ Input
○ Process
○ Output

INPUT PROCESS OUTPUT

Data Algorithm Information


4.1 Details of Problem Analysis : Input

• Data that is entered into or


received by a computer.
INPUT
• It is sent to a program for
processing.

Input
Devices
4.2 Details of Problem Analysis : Process

• A process or running process refers to a set of


instructions currently being processed by the computer
processor.
PROCESS • 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
Example process
 Display the answer is a
process
4.3 Details of Problem Analysis : Output

• The output of a computer or word processor is the


information that it displays on a screen or prints on
OUTPUT paper as a result of a particular program.
• There are four basic types of output: audio output,
graphics output, text output, and video output.

Output
Devices
4.4 Details of Problem Analysis
Example

EXAMPLE 1 INPUT PROCESS OUTPUT

Compute the Sum =


● Number 1
sum of 3 ● Number 2
Number 1 +
Number 2 + Sum
numbers ● Number 3 Number 3
4.4 Details of Problem Analysis
Example

EXAMPLE 2 INPUT PROCESS OUTPUT

Calculate and
display the ● Mark A Average =
(Mark A +
average mark of ● Mark B Average
Mark B +
three students ● Mark C Mark C) /3
05
Basic Concepts of Algorithm
and Algorithm Presentation
5.0 Basic Concepts of Algorithm

● Algorithm is a sequence of steps that tells how to solve a


particular problem.
● An algorithm must :
○ Step-by-step method for solving problem.
○ Must be a well defined (clear, unambiguous) and well
ordered sequence of instructions
○ Must produce a result.
○ Must have a finite number of steps
5.0 Basic Concepts of Algorithm
Example

EXAMPLE 1 INPUT PROCESS OUTPUT

● Number 1 Sum =
Number 1 +
● Number 2 Sum
Number 2 +
Compute the ● Number 3 Number 3
sum of 3
numbers Input Number1, Number2, Number3
Calculate Sum by
ALGORITHM
Sum = Number 1 + Number 2 + Number 3
Display Sum
5.0 Basic Concepts of Algorithm
Example
EXAMPLE 2 INPUT PROCESS OUTPUT

● Mark A Average =
(Mark A +
● Mark B
Mark B + Average
Calculate and ● Mark C Mark C) /3
display the
Input Mark A, Mark B, Mark C
average mark Calculate Average by
of three Adding the numbers and
ALGORITHM Sum = Mark A + Mark B + Mark C
students
Dividing the sum by 3
Average = Sum / 3
Display Average
5.1 Algorithm Presentation

● Algorithm can be represented graphically using flowchart or


textual form using pseudocode.

PSEUDOCODE FLOWCHART

● Flowchart is a graphical or symbolic


● Pseudocode consist representation present steps in an algorithm.
of words to describe ● Each symbol has a specific meaning
the activities to solve containing a short description of a step.
a problem. ● The flowchart symbols are linked together
with arrows showing flow direction.
5.2 Algorithm Presentation : Pseudocode
• Uses a mixture of English phrases and indention.
• There are no grammar rules in pseudocode.
• Pseudocode is not case sensitive.
PSEUDOCODE • Criteria of a good pseudocode are:
o Easy to understand, precise and clear.
o Gives the correct solution in all cases.
o Eventually ends.

Start
Read number1
Compute Read number2
Example the sum of Sum = number1 + number2
2 numbers Display “ The sum is ”
Display Sum
End
5.3 Algorithm Presentation : Flowchart

• Flowcharts are used to Example


design and document
simple processes or
programs using certain
FLOWCHART symbols that represents
a workflow or process.
• Like other types of
diagrams, they help
visualize the process.
5.4 The Basic Symbols in Flowchart

BASIC SYMBOL NAME DESCRIPTION

Terminal Indicates the beginning and


( Begin / End ) end points of an algorithm.

Instructions that transform


Process input into output.

Indicates the logical


Flow of Activities sequence of execution
steps in the algorithms.
5.4 The Basic Symbols in Flowchart

BASIC SYMBOL NAME DESCRIPTION

Input / Output Input or output operation.

Selection process or
Selection / Decision condition that determined a
specific path to follow.

Indicates additional
Annotation
information about a step in
(Comment) the algorithm.
5.4 The Basic Symbols in Flowchart

BASIC SYMBOL NAME DESCRIPTION

Process containing a series


Predefined
of program steps specified
Process elsewhere.

Entry from or exit to another


On-page Connector part of the flowchart on the
same page.

Entry from or exit to another


Off-page Connector part of the flowchart on
another page.
5.4 The Basic Symbols in Flowchart
BASIC SYMBOL NAME DESCRIPTION

Indicates the storage of the


Storage I/O
data involved in the
(Data File or Database) algorithm.

Indicates a step where data


Stored Data gets stored.

Represents a single printed


document or multiple
Document / File printed documents in the
Single Multiple algorithm.
5.4 The Basic Structure in 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.
5.4 The Basic Structure in 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.
5.4 The Basic Structure in 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.
5.5 The Basic Structure / Symbols in Flowchart
Example
5.5 The Basic Structure / Symbols in Flowchart
Example
Thanks!
Do you have any
questions?

You might also like