CA211 Fundamentals of Computer Programming

Document Sample
CA211 Fundamentals of Computer Programming Powered By Docstoc
					CA211 Fundamentals of
Computer Programming

Tuesday Week2
Lecture: More work on Algorithms


                  [   B.Sc. in Enterprise Computing ]
Reminder

      What is an algorithm?



      A set of steps or rules
      The steps involved in finding the
       solution to a problem or calculation

Tues, 4th October 2011   Sara Morrissey and Jennifer Foster   2
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
Tues, 4th October 2011   Sara Morrissey and Jennifer Foster   3
Remember…

         Don’t type until you’ve worked out the
          steps on paper
         No clear algorithm, no working
          program




Tues, 4th October 2011   Sara Morrissey and Jennifer Foster   4
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?


Tues, 4th October 2011   Sara Morrissey and Jennifer Foster   5
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?




Tues, 4th October 2011   Sara Morrissey and Jennifer Foster   6
Milk Jug solution in
algorithmic-style steps

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.
Tues, 4th October 2011   Sara Morrissey and Jennifer Foster   7
Problem Solving

Attempt the following logic puzzles
 For each you must
             Discuss and resolve the puzzle
             Write out the steps involved in solving the
              problem




Tues, 4th October 2011    Sara Morrissey and Jennifer Foster   8
Double Agents
      Three spies, suspected as double agents, speak as
       follows when questioned:

       Albert: "Bertie is a mole."
       Bertie: "Cedric is a mole."
       Cedric: "Bertie is lying."

       Assuming that moles lie, other agents tell the truth,
       and there is just one mole among the three,
       determine:

       Who is the mole?
Tues, 4th October 2011   Sara Morrissey and Jennifer Foster    9
Working it out

1.        Problem
2.        Known
3.        Steps to solution
         (comment)
         (code)



Tues, 4th October 2011   Sara Morrissey and Jennifer Foster   10
Working it out

      Problem:
              find mole
      Known:
              Moles lie
              Agents tell the truth
              There is only one mole
                    There are two telling truth (agents) and one
                     lying (mole)
Tues, 4th October 2011         Sara Morrissey and Jennifer Foster   11
High level - steps

         Steps
1.        Test hypothesis that A is a mole and
          B&C are telling the truth. If no
          contradictions A is answer
2.        Test hypothesis that B is a mole…
3.        Test hypothesis that C is a mole…


Tues, 4th October 2011   Sara Morrissey and Jennifer Foster   12
An alternative with more detail
         Steps
1.        Believe A is a liar/mole
2.        Test if B&C are telling truth and no
          contradiction
3.        If no answer found, continue OR if answer
          found, continue anyway to verify
4.        Believe B is a liar/mole
5.        Test if A&C are telling truth and no
          contradiction
6.        …
Tues, 4th October 2011   Sara Morrissey and Jennifer Foster   13
Working it out in detail

Test if A is mole/liar
 If A is lying,
               B is not a mole,
               B and C are telling the truth
         Test if B and C are telling the truth
               B says C is a mole, but C says B is lying
                – a contradiction

Tues, 4th October 2011     Sara Morrissey and Jennifer Foster   14
Working it out

      Test if B is a mole/liar
             If B is lying, C is not a mole
             If B is lying, A and C are telling the truth
      Test if A and C are telling the truth
             A says B is a mole
             C says B is lying when B says C is a mole,
              meaning C isn’t a mole
             A and C are in agreement
             This tells us B is lying and is the mole
Tues, 4th October 2011       Sara Morrissey and Jennifer Foster   15
Just in case – follow through!

      Test if C is a mole/liar
             If C is lying, B is telling the truth and C is
              the mole
             If C is lying, A and B are telling the truth
      Test if A and B are telling the truth
             A says B is a mole
             B says C is the mole
             A and B contradict each other

Tues, 4th October 2011     Sara Morrissey and Jennifer Foster   16
So..

      The only scenario that works out is
       where B is the mole.




Tues, 4th October 2011   Sara Morrissey and Jennifer Foster   17
Boat Trip
A man needs to cross a river in a canoe. With
  him, he has a cabbage, a goat, and a wolf.
He can only carry one of the three at a time in
  his small boat. If he leaves the cabbage
  and the goat, the goat will eat the
  cabbage. If he takes the cabbage, the wolf
  will eat the goat.

       How does he successfully cross the river
       with his load?
Tues, 4th October 2011   Sara Morrissey and Jennifer Foster   18
Preliminaries

1.        Problem: safe transport of goods
2.        Known:
       1.       3 things to move
       2.       Only move one at a time
       3.       Wolf eats goat
       4.       Goat eats cabbage
       5.       Wolf and cabbage are ok together

Tues, 4th October 2011    Sara Morrissey and Jennifer Foster   19
Methodical steps - example

1.        Try taking cabbage over first
               Wolf eats goat
2.        Try taking wolf over first
               Goat eats cabbage
3.        Try taking goat over first
               All is well, leave goat


Tues, 4th October 2011      Sara Morrissey and Jennifer Foster   20
Methodical steps – example

4.        Try taking wolf over next
               Wolf eats goat when we get over
5.        Try taking cabbage over next
               Goat eats cabbage when we get over
6.        Try leaving wolf but taking goat back again
          to separate them
7.        Try leaving goat, but bringing cabbage
8.        Return for goat
Tues, 4th October 2011      Sara Morrissey and Jennifer Foster   21
All is good


                                           Phew!




Tues, 4th October 2011   Sara Morrissey and Jennifer Foster   22
Solution in basic steps

      Take goat over
      Come back by self
      Take wolf over
      Bring back goat
      Take cabbage over
      Come back by self
      Take goat over
Tues, 4th October 2011   Sara Morrissey and Jennifer Foster   23
What’s the alternative?

      Take goat over
      Come back by self
      Take cabbage over
      Bring back goat
      Take wolf over
      Come back by self
      Take goat over
Tues, 4th October 2011   Sara Morrissey and Jennifer Foster   24

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:1
posted:6/3/2012
language:
pages:24