0% found this document useful (0 votes)
281 views48 pages

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

This document discusses the topic of problem-solving and algorithm design in CSC121 Introduction to Algorithm Design and Development. It covers defining problems and examples, problem-solving, the program development life cycle including problem analysis, algorithm design, implementation, testing and debugging, and maintenance. Key aspects of each stage like determining required information and assumptions in problem analysis, and testing sample data in debugging are described.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
281 views48 pages

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

This document discusses the topic of problem-solving and algorithm design in CSC121 Introduction to Algorithm Design and Development. It covers defining problems and examples, problem-solving, the program development life cycle including problem analysis, algorithm design, implementation, testing and debugging, and maintenance. Key aspects of each stage like determining required information and assumptions in problem analysis, and testing sample data in debugging are described.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
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?

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.

4
WHAT IS A PROBLEM?

TYPES OF KNOWLEDGE-LEAN PROBLEMS


• Can be solved (though not always skillfully) by use of
PROBLEM instructions for the task and general problem solving skills
• Ex: finding a parking space in the mall, shampooing hair.

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”:

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. Choose a language
2 Decision Forecasting
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
DEVELOPMENT
LIFE CYCLE
PROGRAM DEVELOPMENT LIFE CYCLE
Problem analysis

Program
maintenance and Algorithm design
documentation

Program testing Algorithm


10
and debugging implementation
PROGRAM DEVELOPMENT LIFE CYCLE
1. Problem Analysis • Understanding the problem.
• Purpose: clearly analyse the problem.
• 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 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.
6. Determined formula to be used.
7. Design the expected screen appearance.
11
PROGRAM DEVELOPMENT LIFE CYCLE
TERM DEFINITION
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 screen Communication channel between the program built and the real
appearance user. It is used to allow users and computer program to
communicate to each other. Ex: response messages, the design of
user interface.
12
PROGRAM DEVELOPMENT LIFE CYCLE
2. 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.
• 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 program
might encounter and how the program will handle each
scenario.

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

Input Process Output


Data (Algorithm) Information

23
INPUT

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


• It is sent to a program for processing.

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

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
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

• 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 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

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