Chapter 2: Algorithm Discovery and Design: Invitation To Computer Science, C++ Version, Third Edition
Chapter 2: Algorithm Discovery and Design: Invitation To Computer Science, C++ Version, Third Edition
Representing algorithms
Searching lists
Matching patterns
Sequential
Conditional
Iterative
Computation operations
Example
Variable
Control operations
Conditional operations
Iterative operations
Iterative operations
Loop
Examples (syntax)
while j > 0 do
set s to s + aj
set j to j - 1
repeat do
print ak print ak
set k to k + 1 set k to k+1
until k > n while k ≤ n
Components of a loop
Continuation condition
Loop body
Infinite loop
The continuation condition never becomes false
An error
Invitation to Computer Science, C++ Version, Third Edition 19
Figure 2.5: Third Version of the Average Miles per Gallon Algorithm
(What type of operations are used ?)
While loop
Input
Processing
Output
Process Execution
Algorithmic operations followed until the desired result is
obtained.
Algorithm outline
Start with the first entry and check its name, then
repeat the process for all entries
Algorithm outline
Keep track of the largest value seen so far and its
location
Algorithm outline
Must also:
Ensure the algorithm is correct