Unit2 Algorithmic Problem Solving
Unit2 Algorithmic Problem Solving
Acknowledgement
• The contents of these slides have origin from School of
Computing, National University of Singapore.
• We greatly appreciate support from Mr. Aaron Tan Tuck
Choy for kindly sharing these materials.
Programming Methodology Unit2 - 3
Recording of modifications
• Currently, there are no modification on these contents.
Programming Methodology Unit2 - 5
Iterative
Design process
Implementation
Testing
Programming Methodology Unit2 - 7
Iterative
Design process
Implementation
Testing
Programming Methodology Unit2 - 8
Write
Iterative
Design process
algorithm
Produce
code Implementation What is an
algorithm?
Check for
correctness and Testing
efficiency
Programming Methodology Unit2 - 9
Algorithm (1/3)
◼ An algorithm is a well-defined computational
procedure consisting of a set of instructions,
that takes some value or set of values as
input, and produces some value or set of
values as output.
Algorithm (2/3)
◼ An algorithm has these properties:
The
Each step
algorithm
must be exact.
(Or it will not be
must
precise.)
Exact Terminate terminate.
(Or no solution
will be obtained.)
The
algorithm The algorithm
must be must be
Effective General
effective. general.
(i.e. it must solve (Within the
the problem.) constraints of the
system/language.)
Programming Methodology Unit2 - 11
Algorithm (3/3)
◼ Ways of representing an algorithm:
Flowchart Pseudocode
lynda.com
Programming Methodology Unit2 - 12
Algorithm: Example #1
Programming Methodology Unit2 - 13
Yes end of
input?
No
Enter num
increment count
ave sum/count sum sum + num
No
end
Programming Methodology Unit2 - 14
Algorithm: Pseudocode
◼ We will write algorithms in pseudocode instead
of flowchart as the former is more succinct
◼ However, there are no standard rules on how
pseudocodes should look like
◼ General guidelines:
◼ Every step must be unambiguous, so that anybody is
able to hand trace the pseudocode and follow the
logic flow
◼ Use a combination of English (keep it succinct) and
commonly understood notations (such as for
assignment in our previous example)
Programming Methodology Unit2 - 16
Sequence • Default
True False
• Also called ?
Selection branching
Data Representation
◼ Internal representation: bits (binary digits) 0 and 1
◼ 1 byte = 8 bits
◼ In programming, we need variables to hold data. A
variable has an associated data type and occupies
memory space. In the following slides, variables are
shown as boxes.
◼ Some data types in C (list is not exhaustive)
◼ Integers: int, short, long (int is most common)
◼ Real numbers: float, double
◼ Characters: char
Variables used:
Another possible algorithm:
num1 num2 num3
enter values for num1, num2, num3
total ( num1 + num2 + num3 ) total
ave total / 3
print ave ave
int main(void) {
int num1, num2, num3;
float ave;
return 0;
}
Programming Methodology Unit2 - 21
Algorithm B:
enter values for num1, num2 Variables
used:
// Swap the values in the variables if necessary
if (num2 < num1) num1 num2
True False
Control Structures: Selection (3/3) ?
int main(void) {
int num1, num2, temp;
return 0;
}
Programming Methodology Unit2 - 24
False
// Display answer
print ans
Initialisation is
very important!
Programming Methodology Unit2 - 25
False
?
Control Structures: Repetition (2/3) True
False
▪ r could be obtained
by A modulo B (i.e.
remainder of A / B)
Summary
◼ In this unit, you have learned about
◼ The process of algorithmic problem solving
◼ The properties of an algorithm
◼ The three control structures
◼ How to write algorithms in pseudocode
◼ Tracing algorithms to verify their correctness
Programming Methodology Unit2 - 31
End of File