Professional Documents
Culture Documents
CS102 Computer Programming I: Algorithms
CS102 Computer Programming I: Algorithms
CS102 Computer Programming I: Algorithms
Computer Programming I
Lecture 2: ALGORITHMS
followed in a fixed order, and that, especially if given to a computer, will help to
calculate an answer to a mathematical problem
Broadly: a step-by-step method for accomplishing a task
Informal description
An ordered sequence of instructions that is guaranteed to solve a specific
problem
Finiteness
Pseudocode
Flowchart
– Symbols, pictorial representation
Sample Algorithm - How to change your
motor oil (Plain English)
1. Place the oil pan underneath the oil plug of your car.
2. Unscrew the oil plug.
3. Drain oil.
4. Replace the oil plug.
5. Remove the oil cap from the engine.
6. Pour in 4 quarts of oil.
7. Replace the oil cap.
Yet Another Algorithm – The
Swap
Create an algorithm to interchange the
value of the two cells below:
5 4
Cell A Cell B
Yet Another Algorithm – The Swap and
Its Temporary Solution
5 1 3
4
Cell A Temp Cell B
Algorithms and Humans
Algorithms are not a natural way of stating a
problem’s solution, because we do not normally
state our plan of action.
We tend to execute as we think about the problem.
Hence, there are inherent difficulties when writing
an algorithm.
We normally tailor our plans of action to the
particular problem at hand and not to a general
problem (i.e. a nearsighted approach to problem
solving)
We usually do not write out our plan, because we
are usually unaware of the basic ideas we use to
formulate the plan. We hardly think about it – we
just do it.
Algorithms and Humans
1. Sequential operations
2. Conditional operations
3. Iterative operations
Sequential operations
• A computer can compare two (2) pieces of information and select one of
two alternative actions.
Typical pseudocode e.g.
If number < 0 then
add 1 to neg_number
else
add one to positive number
end-if
SIMULATION