Problem-solving and Program
Design
Problem solving-General
• Define the problem
• Analyse the problem
• Suggest possible solutions
• Evaluate and choose the best solution
• Implement and review
Problem-solving using computers
• Define the problem
• Analyse the problem
• Suggest possible solutions and choose the best solution
• Create algorithm
• Test and validate the algorithm
• Implement the algorithm in a programming language to
become a program
• Run the program
• Document and maintain the program
Problem Solving Cnt’d
• Analysing the problem involves breaking down
the problem into inputs, processing, storage
needed and outputs.
• An IPO diagram can be used to break down
the problem
Algorithms
• Step-by-step definition of a task or problem
• The characteristics of algorithms are: precise,
unambiguous, finite steps, terminate.
• The ways to represent algorithms are:
narrative, pseudocode and flowchart
Ways to Represent Algorithms
• Narrative – instructions are written in plain
English.
• Pseudocode – instructions resemble
programming language instructions
• Flowchart – diagrammatic representation of
algorithms using special symbols
Ways to Represent Algorithms Cnt’d
• Words/phrases used in pseudocode:
• for accepting data use read and input
• for storage use store and
• for output use write, output, display
• Symbols used in flowcharts:
– Oval – input/output or terminal symbol
– Rectangle – processing
– Rhombus or diamond – decision
– Arrows – flow of control
• Small circles – connectors for sections of flowchart
Example
• Write an algorithm that displays the area of a
rectangle by accepting length and width from
the user.
IPO Chart-Example Cnt’d
Input Processing Output
Accept length, width
length, width Calculate area Area
Store results in area
Display area
Pseudocode-Example Cnt’d
• Step 1: start
• Step 2: read length, width
• Step 3: area length * width
• Step 4: write area
• Step 5: stop
Narrative-Example Cnt’d
• Step 1: Start
• Step 2: Get length and Width
• Step 3: Calculate area by using the formula
length* width and store it in area
• Step 4: Display the area
• Step 5: Stop
Flowchart-Example Cnt’d
c. Program to accept the length and width of a rectangle and calculate its area.
start
read length, width
area = length * width
write area
stop
Flowchart Symbols
Top down Design
• Computer programmers use a divide and conquer
approach to problem solving:
a problem is broken into parts
those parts are solved individually
the smaller solutions are assembled into a big
solution
• These techniques are known as top-down design
and
modular development.
Top down Design
• Top-down design is the process of designing a
solution to a problem by systematically
breaking a problem into smaller, more
manageable parts.
Top down Design
Program Constructs or Control Structures
• Three types
– sequencing,
– selection
– iteration.
Program Constructs or Control Structures
• Sequencing is putting instructions in the order it should
take place
• Selection is making a choice between two or more options
by using the decision making capabilities of computer
• Selection statements used in pseudocode are if-then, if-
then-else
• Repetition or iteration is used to repeat a certain process
a number of times
• Iteration or loop statements used in pseudocode are for-
endfor and while-endwhile
Variables, Constants, Literals and Data Types
• Variables are identifiers of storage locations in
memory that can store any value.
• Constants are fixed values.
• Literals are constants that are written literally in
a program
• Data types determine the type of data a
variable can store
• The data types are: integers, floating point,
characters and string.
• Express an algorithm to get two numbers from
the user (dividend and divisor), testing to
make sure that the divisor number is not zero,
and displaying their quotient using
pseudocode