ITA 120
Introduction to Programming
Algorithms
Algorithm
Definitions
A precise specification of a behaviour
intended to solve a well-defined problem
A finite set of well-defined instructions for
accomplishing some task
An ordered set of unambiguous, executable
steps that defines a terminating process.
Properties of algorithms
An algorithm must:
Have a well established structure.
Consist of executable steps. To make a list of all
positive integers is not executable, as the list is infinite.
Be unambiguous. The actions required by each step
should be unique and complete.
Be correct. If the input conditions are satisfied and the
steps of the algorithm executed then the desired
outcome should be produced.
Lead to an end. The algorithm should terminate after a
finite number of steps.
Related Terms
Process
Routine
Technique
Procedure
Pattern
Recipe
Ways of Representing Algorithms
Narrative - the set of simple instructions
that combine to accomplish a task.
Flowchart a graphical representation for
the solution of a problem. Symbols are
used to represent beginning, end, data flow.
Pseudo code description of a solution that
combines English and a programming
language.
[Link] [Link]/[Link]
Example - Narrative
Obtain a basket of unshelled peas and an
empty bowl.
As long as there are unshelled peas in the
basket, continue to execute the following
steps.
Take a pea from the basket
Break open the pea pod
Dump the peas from the pod into the bowl
Discard the pod
Example - Narrative
An algorithm for buying a ticket for a
movie
Go to the theatre
Walk to the ticket counter
Select a movie
Pay the price
Receive the ticket
Example - Flowchart
[Link]
Example - Pseudocode
CalculateAverageMark
Start
1. Prompt for FirstMark
2. Get FirstMark
3. Prompt for SecondMark
4. Get SecondMark
5. Average = (FirstMark + SecondMark) / 2
6. Print Average
Stop
Exercises
Write narratives for the following:
Find the average for four numbers
Change the time in seconds to minutes
Find the perimeter of a rectangle
Find the perimeter of a circle
Find the area of a circle