CA211 Fundamentals of Computer Programming
Document Sample


CA211 Fundamentals of
Computer Programming
Wednesday Week1
Lecture: Algorithms
[ B.Sc. in Enterprise Computing ]
Intro
What is an algorithm?
Wed, 28th September 2011 Sara Morrissey and Jennifer Foster 2
Algorithm definition
A set of steps or rules
The steps involved in finding the
solution to a problem or calculation
Problem solving steps/instructions!
Wed, 28th September 2011 Sara Morrissey and Jennifer Foster 3
In programming…
Instructions that a human could do
But computers do faster and don’t get
bored!
Programming is finding ways for
computers to do the boring, repetitive
or difficult work for us
Wed, 28th September 2011 Sara Morrissey and Jennifer Foster 4
In programming…
What could you use an algorithm for?
or
Give an example of an algorithm.
Wed, 28th September 2011 Sara Morrissey and Jennifer Foster 5
Algorithms so far
You have already come across
algorithms!
Kind of like the ∑ sum function in a
spreadsheet
Finding your perfect match online!
Wed, 28th September 2011 Sara Morrissey and Jennifer Foster 6
Preparing for programming
Fail to prepare = prepare to fail
Preparing = working out the steps
Meaning… working out the algorithm
This is very important!
Wed, 28th September 2011 Sara Morrissey and Jennifer Foster 7
Problem Solving and
Programming
1. Identify the problem(s) to be solved
2. Note down what you already know
3. Outline the steps for solving the
problem on paper in English
4. Write comments for each step in your
file in order
5. Write the code, compile, run and
debug
Wed, 28th September 2011 Sara Morrissey and Jennifer Foster 8
Problem Solving and
Programming
Each solution must be
Unambiguous
Executable
Terminating
Wed, 28th September 2011 Sara Morrissey and Jennifer Foster 9
Unambiguous
Precise instructions
No guesswork
No creativity
Wed, 28th September 2011 Sara Morrissey and Jennifer Foster 10
Executable
If you don’t give the human enough
information he/she won’t be able to
figure it out
Likewise…
If you don’t give the computer enough
information…
It doesn’t have real world knowledge!
Wed, 28th September 2011 Sara Morrissey and Jennifer Foster 11
Terminating
Take care your program won’t get
stuck in an infinite loop
Wed, 28th September 2011 Sara Morrissey and Jennifer Foster 12
Remember…
Never start to write a Java program,
before you have worked out (on paper
or in your head) exactly what the
program should do
Wed, 28th September 2011 Sara Morrissey and Jennifer Foster 13
Remember…
If you cannot find or write an algorithm,
you cannot use the computer to solve
your problem
Wed, 28th September 2011 Sara Morrissey and Jennifer Foster 14
Remember…
The more time you spend working out
the algorithm for a problem, the better
the program you write will be
Wed, 28th September 2011 Sara Morrissey and Jennifer Foster 15
Problem Solving
Get into groups
Attempt the following logic puzzles
For each you must
Discuss and resolve the puzzle
Write out the steps involved in solving the
problem
Wed, 28th September 2011 Sara Morrissey and Jennifer Foster 16
1a: Truth
A. The number of false statements here is one.
B. The number of false statements here is two.
C. The number of false statements here is three.
D. The number of false statements here is four.
Which of the above statements is true?
Wed, 28th September 2011 Sara Morrissey and Jennifer Foster 17
1a: Solution
C
Wed, 28th September 2011 Sara Morrissey and Jennifer Foster 18
Working it out in steps
1. Assume statement A is true. If this is
true, then there is only one false
statement. This means that there are
three true statements. This cannot be
the case because if A is true, B, C or D
cannot be true.
Wed, 28th September 2011 Sara Morrissey and Jennifer Foster 19
Working it out in steps
2. Assume statement B is true. If so,
then there is one other true
statement apart from B itself. This
cannot be the case because A, C
and D contradict B.
Wed, 28th September 2011 Sara Morrissey and Jennifer Foster 20
Working it out in steps
3. Assume statement C is true. If so,
then there is only one statement
which is true which is C. A, B and D
contradict C which means they
must be false which makes C true.
Wed, 28th September 2011 Sara Morrissey and Jennifer Foster 21
Working it out in steps
4. Assume statement D is true. If so,
then there are no true statements. But
this cannot be the case if D is true.
Wed, 28th September 2011 Sara Morrissey and Jennifer Foster 22
Working it out in steps
5. Only one of the following
assumptions (C is true) does not lead
to a contradiction. Therefore C must
be true.
Wed, 28th September 2011 Sara Morrissey and Jennifer Foster 23
Milking it
A milkman has two empty jugs: a three
gallon jug and a five gallon jug. How
can he measure exactly one gallon
without wasting any milk?
Wed, 28th September 2011 Sara Morrissey and Jennifer Foster 24
Working it out
Wed, 28th September 2011 Sara Morrissey and Jennifer Foster 25
Working it out in an algorithm
1. Fill the 3 gallon jug.
2. Pour the liquid from the 3 gallon jug
into the 5 gallon jug.
3. Fill the 3 gallon jug again.
4. Pour as much liquid as will fit into the
5 gallon jug. What's left in the 3 gallon
jug is one gallon.
Wed, 28th September 2011 Sara Morrissey and Jennifer Foster 26
Get documents about "