Chapter_1_Problem_Solving
Chapter_1_Problem_Solving
INTRODUCTION TO
PROBLEM SOLVING
Chapter1 1
Introduction
• Algorithm
– A sequence of precise instructions that
leads to a solution
• Program
– An algorithm expressed in a language the
computer can understand
Chapter1 2
Creating Computer Solutions to
Problems (continued)
3
Analyzing the Problem
• Analyze a problem to:
– Determine the goal of solving it
• Output
– Determine the items needed to achieve that goal
• Input
– PROCESS
• Always search first for the output
4
Introduction (cont.)
Example:
Chapter1 5
Introduction (cont.)
• Programming is a creative process
– No complete set of rules for creating a program
Chapter1 6
Programming Process
Chapter1 7
Programming Process (cont.)
• Develop the algorithm before implementation
– Experience shows this saves time in getting your
program to run.
– Test the algorithm for correctness
Chapter1 8
Programming Process (cont.)
Implementation Phase
• Translate the algorithm into a programming
language
• Compile the source code
– Locates errors in using the programming language
Chapter1 9
Programming Process (cont.)
Chapter1 10
Programming Process (cont.)
Example:
• STEP 1: PROBLEM ANALYSIS
– Purpose:
• To describe in details a solution to a problem by
providing the needed information for the problem.
– How?
• First, understand & study the problem.
– Identify:
• The input to the problem.
• The required output.
• The relevant process. For example, scientific formula
or appropriate theories if any.
Chapter1 11
Programming Process (cont.)
Problem : Write a program that get 3 numbers as
input from user. Find the average and display the
numbers and the average.
Problem Analysis:
Input: 3 numbers.
Process: 1. Add the numbers
2. Divide the total with 3
Output: The 3 numbers and average
Chapter1 12
Programming Process (cont.)
Chapter1 13
Programming Process -
Representation Method
• STEP 2: PROGRAM DESIGN
• Definition: It defines the framework or the
flow for the problem solution
1. Algorithm
– Algorithm is a sequence of instructions to solve a
problem written in human language and problem
can be solved if follow the correct procedure
– A programmer writes the solution in the form of an
algorithm before coding it into computer language.
Chapter1 14
Programming Process -
Representation Method (cont.)
Example of algorithm to calculate the average of 3
numbers:
Chapter1 16
Programming Process -
Representation Method (cont.)
START
INPUT first number, second number, third number
Total = first number + second number + third
number
average = Total / 3
OUTPUT first number, second number, third
number, average
END
Chapter1 17
Programming Process -
Representation Method (cont.)
3. Flowchart
• A graphical representation of data, information and
process or an orderly step-by-step solution to a
problem.
Chapter1 18
Planning the Algorithm (continued)
• Flowcharts are also used to plan an algorithm
– Use standardized symbols
– Symbols connected with flowlines
– Oval: start/stop symbol
– Rectangle: process symbol
• Represents tasks such as calculations
– Parallelogram: input/output symbol
• Represents I/O tasks
19
Programming Process -
Representation Method (cont.)
Start
Example:
Input a, b, c
Total = a + b + c
average = Total / 3
Display a, b, c
Display average
End
Chapter1 20
Programming Process -
Representation Method (cont.)
STEPS 3: PROGRAM CODING
• Definition: Write a solution into a specific
programming language such as C, C++, COBOL
and etc.
• Solution: Instructions before it is coded into
programming language.
• Purpose: To produce a program to develop a
system.
Chapter1 21
Creating Computer Solutions to
Problems
• Analysis tools: IPO charts, pseudocode, flowcharts
• To desk-check or hand-trace, use pencil, paper,
and sample data to walk through algorithm
• A coded algorithm is called a program
22
Analyzing the Problem (continued)
23
IPO Charts
• Use an IPO chart to organize and summarize the
results of a problem analysis
– IPO: Input, Processing, and Output
24
IPO Charts (continued)
25
IPO Charts (continued)
26
Analyzing the Problem (continued)
• First, reduce the amount of information you need to
consider in your analysis:
27
Analyzing the Problem (continued)
• Worse than having too much information is not
having enough information to solve problem:
28
Analyzing the Problem (continued)
• Distinguish between information that is missing and
information that is implied:
29
Planning the Algorithm
• Algorithm: set of instructions that will transform the
problem’s input into its output
– Record it in the Processing column of the IPO chart
• Processing item: intermediate value used by
algorithm when processing input into output
• Pseudocode is a tool programmers use to help
them plan an algorithm
– Short English statements
30
Planning the Algorithm (continued)
31
Planning the Algorithm (continued)
32
Planning the Algorithm (continued)
• A problem can have more than one solution:
33
Hints for Writing Algorithms
34
Hints for Writing Algorithms
(continued)
You may use a portion of a previous solution to solve current problem
35
Desk-Checking the Algorithm
36
Desk-Checking the Algorithm
(continued)
37
Desk-Checking the Algorithm
(continued)
• Valid data is data that the programmer is
expecting the user to enter
• Invalid data is data that he or she is not expecting
the user to enter
• You should test an algorithm with invalid data
– Users may make mistakes when entering data
38
The Gas Mileage Problem
39
The Gas Mileage Problem (continued)
• After planning the algorithm, you desk-check it:
40
Summary
• Problem-solving typically involves analyzing the
problem, and then planning, reviewing, implementing,
evaluating, and modifying (if necessary) the solution
• Programmers use tools (IPO charts, pseudocode,
flowcharts) to help them analyze problems and
develop algorithms
– During analysis, you determine the output and input
– During planning, you write the steps that will transform
the input into the output
41
Summary (continued)
• After the analysis and planning, you desk-check
the algorithm
– Follow each of the steps in algorithm by hand
• Coding refers to translating the algorithm into a
language that the computer can understand
• Before writing an algorithm, consider whether you
have already solved a similar problem
42