Phyu Phyu Aye Chan
Unit - 1 : Problem Solving
1. Understanding Algorithms
-
Algorithm → a set of instructions for solving a problem
Input - data from user
Output - data back to the user
** a computer program is an implementation of an algorithm; an algorithm is not a computer
program.
algorithms must be :
- unambiguous ( clear )
- step by step
Consider ( 3 ) points if it is successful algorithm or not
1. Accuracy - do you receive your expected outcome exactly?
2. Consistency - would the algorithm give the same result if you run repeatedly?
3. Efficiency - do you receive your outcome as fast as possible?
Why do programmers use algorithms?
1
Phyu Phyu Aye Chan
-
plan their approach to problems
choose right ways to solve
describe their solution to others, people who do not use same programming language as
you
keep record of logic to their solution
Ways to interpret algorithms
1.
2.
3.
4.
Written description
Flowcharts
Pseudocode
Program code
1. Written Descriptions
-
the simplest way of expressing an algorithm
Features of Written Descriptions
-
short sentences
should be easy to understand and have no unnecessary details
clear instructions
should include all the elements found when decomposing a problem
Advantages of WD
-
no need to worry about using correct syntax
more natural to use normal writing
Disadvantages of WD
-
temptation to use too many words
makes an algorithm descriptive rather than instructional
Example - Adding two numbers
1.
2.
3.
4.
Enter first number.
Enter second number.
Calculate total by adding first and second number.
Output total.
2. Flowchart
2
Phyu Phyu Aye Chan
-
a graphical representation of an algorithm
Start or End
- at the beginning and end of the algorithm
Input or Output
- for values that are put into or taken out from algorithm
Process
- for instructions and calculations
Decision
3
Phyu Phyu Aye Chan
-
for a question - often a “yes” or “no”
Logical flow ( line )
- connect boxes and show direction
- Represents the flow from one component to the next
Subroutine
- to reference other flowcharts
Advantages of FC
-
easy to see how a program flows
flowcharts follow an international standard — easy to pick up a diagram and understand
it
Disadvantages of FC
-
large problem → diagram can become huge and therefore difficult to follow
any charges to the design may mean a lot of diagram has to be redrawn
Example - Adding two numbers
4
Phyu Phyu Aye Chan
3. Pseudocode ( means “pretend code” )
-
a structured, code-like language that can be used to describe an algorithm
Features of Pseudocode
-
allows developer to concentrate on the logic and efficiency of the algorithm
good pseudocode are :
-
readable and easy to interpret
not too vague
structured like a piece of code
easy to convert into any language
Advantages of Pseudocode
-
can be quickly and easily converted into an actual programming language
easy to understand , even for non-programmers
does not matter if there is syntax errors - still obvious what is intended
changes to the design can be incorporated quite easily
Disadvantages of Pseudocode
-
hard to see how a program flows
can be time consuming to produce
Example - Adding two numbers
Important programming concepts in Pseudocode
5
Phyu Phyu Aye Chan
1. Numbers entered are stored in variables with the identifier such as firstNumber and
secondNumber
a. Identifier : a unique name given to a variable or constant
b. must use descriptive names → code is easier to read
2. Results as well are stored such as total
3. Text has to be displayed using single ‘ ‘ or double “ “ quotation marks
4. Variables are displayed without quotation marks
5. Arithmetic Operators
4. Program code
-
instructions given in a high-level language ( Python, C# , Java in Edexcel )
-
must be precise with syntax
should work if they were typed into your computers
6
Phyu Phyu Aye Chan
7