Problem solving
Outline of a computer-based
solution
Input Processing Output
Planning a solution
A basic rule:
The required output determines the
necessary input and processing
How fast is a car travelling if
it goes 50km in 2 hours?
Output Required? Speed
Input? Distance, time
Processing? Speed = distance / time
= 50km/2hr
= 25 km/hr
A planned solution will:
• have fewer errors and mistakes
• take less time to complete
• result in a better solution
Program development cycle
A six step cycle for developing an
effective software solution.
Analyse: Define the
problem
Have a clear understanding of:
•what the program should do
•what the output should be
•the nature and type of input data
•the relationship between input and output
Design: Plan the solution
Develop an algorithm that will solve the
problem
Algorithm:
A logical sequence of precise steps that will
solve the problem
Design
Break the problem into a sequence of small steps
that the computer can perform.
Use the following tools to do so:
• flow charts
• pseudo code
• Use representative input data and test the
solution by hand to verify that it works.
• Hierarchy charts
Design an interface:
An interface is a communication channel between
the computer and user.
Determine:
• how the required input will be obtained
• how the output will be presented
• how the user will control the program
Then build an interface appropriately.
Code:
Translate the algorithm into Matlab
Use the algorithm developed in the Design Phase and
your knowledge of Matlab to code the solution.
Test & debug: Locate and fix errors
Run the program with representative data to test for
run-time errors.
Manually verify the program output to test the
program logic.
Finalise documentation:
Documentation starts in step and continues
throughout.
Now complete any unfinished documentation and
add whatever is necessary so that:
• the user understands the software (Help info)
• other programmers can interpret your work
• the program can be modified later
Example: Class Average
• Develop an algorithm for computing the average mark of
the class for a single assessment.
Analyse: Define the
problem
•What should the program do?
•Calculate the average mark for 1
assessment
•What should the output should be?
• The average mark
•What is the nature and type of input data
•Class size, Each individual mark
•Class size should be a number
•Each Individual mark should be a number
Analyse: Define the
problem
•What is the relationship between input and
output?
i=class size
•Classmark = i=1
Σmarki / class size
Design: Plan the solution
Develop an algorithm that will solve the
problem
Algorithm:
A logical sequence of precise steps that will
solve the problem
Design
Break the problem into a sequence of small steps
that the computer can perform.
Use the following tools to do so:
• pseudo code
• flow charts
• Use representative input data and test the
solution by hand to verify that it works.
Pseudo Code - Calculate average mark
example
1 The number of students is stored in size
2 The sum of the marks is stored in sum
3 Set size = 0 and sum = 0
4 Get one students mark at a time
5 Add his/her mark to variable sum
6 Add 1 to variable size
7 Repeat until all the students are done
8 Size will contain the number of students
9 Sum will contain their total marks
10 Now divide sum by size to get the class average
Flow diagrams
start
Terminators
end
Input/output block
Processing block
Decision diamond
Flow diagram - Class average example
start
sum = 0
size = 0
More Y
students get size = size + 1 sum = sum + mark
? mark
N
average = sum / size
Display
average
end
Trace Tables – test the algorithm
Mark sum size average More
students
?
3 0 0
Sample
input:
4 3
3, 5, 2
5 1
6 3
7 Y
4 5
5 2
6 8
7 Y
4 2
5 3
6 10
7 N
10 3.3333
Hierarchy Charts
• Shows the overall program structure.
• Describes what each part, or module, of the program
does.
• Also, shows how each module relates to other modules
in the program Class average
Program
Compute Sum
Get Calculate Display
and Number of
Marks Average Average
Marks
Learning a Language
To learn a foreign language, you have to master the
grammar and syntax before you can begin to write
phrases and sentences.
In order to accomplish something significant,
sentences must be assembled into paragraphs and
essays.
Good writing style requires the use of an outline to
guide the development of the paragraphs and
sentences
Learning to Program
To learn a programming language, you have to master
the grammar and syntax before you can begin to write
simple program modules.
In order to solve real problems, simple program
modules must be assembled into larger and larger
modules.
Good, modular programming requires an architecture
in which to assemble your modules.
Programming Paradigms
Functional programming – every programming
operation is actually implemented as a function call with
no side effects
Procedural programming – the basic programs or
subprograms are sequences of operations on data items.
Object-oriented programming (OOP) – characterized
by the concept of encapsulating data items with the
methods or functions that manipulate those data items.