Problems Solving with Programming
• What is programming ?
• Algorithms : Properties
• Algorithms : Specification
• Algorithms : Design
• Algorithms : Construction
• Algorithms : Description
Problems Solving with Programming
• Programming means solving problems
with the help of a computer;
• PROBLEM COMPUTER
SOLUTION
Problems Solving with Programming
• Designing an algorithm
• Writing the problem
• Feeding the program and data into the
computer
Problems Solving with Programming
• Algorithms are instructions for the solving
of problems
– Examples from everyday life : -cooking
recipes ; calculation of formulas ; rules of a
game.
– An instruction is called an „algorithm“, when
the following applies :
Problems Solving with Programming
• Condition 1 : The description is of finite
length.
• Condition 2 : The process will come to
an end after the execution of a finite
number of operations. (Counterexample :
Repeat the steps 1 to 3 until you have won 1 Million : (1)
Fill in a lottery coupon ; (2) hand it in at a lottery
agency ; (3) wait for the next draw and collect your
winnings).
Problems Solving with Programming
• Condition 3 : The sequence in which the
operations have to be applied must be
determined unambiguously.
– „ … Prepare the soup according to the instructions on the
can. At the end add some asparagus pieces. Cut the ham in
strips…“ (It is not quite clear when the operation „Put in some
pieces of esparagus“ has to be executed.
Problems Solving with Programming
• Condition 4 : The effect of each operation
has been defined unambiguously.
• Otherwise there could be unwelcome results.
• Important properties of an algorithm : - Finite
description , - Efficiency , - Definite effect , -
Termination.
Problems with Solving Programming
• The inner structure of an algorithm can be
described by listing the steps that have to be
done :
– ALGORITHM : Inner structure : Do this ; then do
that ;
• A so-called outer behaviour of an algorithm
takes only the effect of algorithm into account. It
is also called the specification of an algorithm.
Problems Solving with Programming
• Algorithm :
1. Input Data (Outer behaviour) –
2. Do this ; data do that (Inner structure) – BLACK BOX
3. Output Data (Outer behaviour)
• The outer behaviour of an algorithm has to be
described before the inner structure.
• The relation between input and output data is
important for the specification. There are two
conditions for the specification : precondition V and
postcondition N.
Problems Solving with Programming
• Consider the following example of a specification :
– (LIST is a list of 100 numbers) : Outer behaviour
– SORT
– (for all I between 1 and 99 : LIST[I] <= LIST[I+1]) :
Outer behaviour .
– SORT : The specification indicates that the algorithm
SORT – no matter what is looks like – will sort a list of
100 numbers into ascending order.
Problems Solving with Programming
• General form of specification : {V} A {N} with:
– V precondition
– A desired algorithm
– N postcondition
Problems Solving with Programming
• Algorithms : Design ::: Of all the steps of
programming, the first is the most difficult.
1.Designing an algorithm
2.Writing the program
3.Feeding the program and
data into the computer.
Problems Solving with Programming
• How do you find an algorithm for a given
problem ?
– Phase 1 : A problem is reduced to simple
subproblems.
– Phase 2 : Searching for algorithms for the simple
problems and putting them together to form the whole
algorithm.
– Example for the reduction of problems : You Have invited
guests for dinner and are planning a menu.
– The first question : What is the menu to consist of ?
– Menu : appetizers --- main course --- dessert.
» appetizers : grapefruit cocktail ; salmon sandwiches.
Problems Solving with Programming
• Algorithms : design
– Counterpart for the Top-Down-Strategy is called Bottom-Up-
Strategy.
– The Bottom-Up-Strategy deals with putting smaller problems
together:
Problems Solving with Programming
• Algorithms are usually made up of three constructs of
smaller partial algorithms :
– Composition : sequential execution ;
– Selection : alternative execution ;
– Iteration : repeated execution .
• By nesting these constructs even complicated algorithms
can be represented.
Problems Solving with Programming
• Composition : A1 and A2 are two algorithms.
With composition, A1 is started first. When A1 is
finished, A2 is them started.
– It is important that A1 terminates (finishes) after a
finite amount of time, otherwise A2 cannot ever be
started.
• Selection : (alternative execution)
– Selection : A1, A2 and A3 are algorithms. A1 is
executed first, depending on the result either A2 or A3
is started.
Problems Solving with Programming
• Iteration : repeated execution.
• A1 and A2 are algorithms. A1, A2 are repeated
until A1 becomes FALSE.
Problems Solving with Programming
• Algorithm description : An algorithm can be
presented:
1. In colloquial form ;
2. As a graphical representation ;
3. Using programming languages .
1. Colloquial language is not quite suitable for the
presentation of algorithms, because of the following
problems:
1. Ambiguities
2. Incompleteness
3. inaccuracy
Problems Solving with Programming
2. Graphical presentation by means of :
– Flows charts : are nets of individual symbols
with definite meanings;
– Structograms : consists of nestings of
individual symbols with definite meanings.
3. If algorithms are to be executed by
computers they have to be
represented in a certain programming
language.