Professional Documents
Culture Documents
IPC031 2017 Lecture 1-1
IPC031 2017 Lecture 1-1
week 1
1
Imperatief Programmeren
(IPC031)
Docenten:
Student-assistenten:
Job Doesburg Joris den Elzen Douwe Lars van Rhijn Martijn Timo Schrijvers Jesse van
Huysmans Schilpzand Son
2
Doel van de cursus
Leren op systematische wijze algoritmen
ontwerpen en implementeren
• Programmeren is leuk!
• C++ is middel, geen doel.
• (++ ... wij doen géén
object-oriëntatie)
3
Literatuur
• Problem Solving with C++
Walter Savitch / Kendrick Mock
– 9e druk (ISBN 978-1-29-201824-9)
Beschikbaar via studievereniging Thalia en
Study Store (Thomas van Aquinostraat 1a) en
Internet-aanbieders
• Boek is verplicht
• Stof: zie cursusplanning Blackboard
4
Opzet van de cursus
maandag dinsdag woensdag donderdag vrijdag
stof-toets
pract-toets
12:30 – 13:30
werkcollege
5
Practicum
• Programmeren leer je door zelf te doen
• Opgaven maken in tweetallen: zoek
iemand van gelijke sterkte
• Plagiaat wordt niet getolereerd
• Programmeren doe je niet achter de
PC! Vereist: goede voorbereiding
6
Opdrachten
• Bestaan uit twee onderdelen:
1. Verplicht, inleveren voor deadline op Blackboard (do 13:30u)
2. Bonus, naar eigen inzicht oplossen, inleveren voor deadline op Blackboard (ma
8:30u) (alleen v telt mee)
• Practicum heeft geen herkansing
• Beoordelingen:
– Je uitwerking wordt beoordeeld of deze wel / niet werkt
– Werkt wel: voldoende
– Werkt niet: onvoldoende
– NSI (Niet Serieus Ingeleverd): telt als niet ingeleverd
– Krijgt feedback van student-assistenten tijdens practicum
– Kunt tijdens practicum opdracht door student-assistent laten beoordelen
7
Tentamenregeling
• Twee toetsen:
– deeltoets (na eerste helft cursus)
– eindtoets (na tweede helft cursus)
• Practicumdeelname is voorwaarde voor toelating tot het toetsen:
– alle verplichte practicumonderdelen zijn beoordeeld met v / o
– je geeft een akkoord over de vastgestelde practicumresultaten (BB)
– bonus is aantal bonus-opdrachten met beoordeling v gedeeld door twee keer het
aantal practicumopdrachten
• Eindcijfer:
– als deeltoets ≥ 5 en eindtoets ≥ 5, dan gemiddelde van beide plus bonus
– als deeltoets < 5 of eindtoets < 5, dan maximum van beide plus bonus
– voldoende eindcijfers worden afgerond op halven, onvoldoende eindcijfers op helen
• Ouderejaars:
– practicumresultaten van vorige jaren zijn niet geldig voor dit jaar
– deeltoets is herkansing IPC014 en eindtoets is herkansing IPC015
• Studenten die van mening zijn dat voor hun individuele situatie
bovenstaande tentamenregeling ongeschikt is, kunnen met de
studieadviseur een alternatieve regeling proberen overeen te komen.
Die dient uiterlijk vóór 8 september 2017 te zijn afgesproken.
Communicatie
• Tijdens hoorcollege / werkcollege / practicum:
– stel vragen! vooral niet uitstellen!
• Blackboard
– college-slides, assignments, feedback
– forum: vraag en antwoord over C++, stof, opdrachten,
vinden van practicumpartner
9
Systematic design of algorithms
10
Concepts
• Algorithm:
precise description of what has to be done to arrive to a
solution of a specific problem
• Programming language:
formalism to denote algorithms in such a way that a
computer can ‘understand’ them
• Program:
an algorithm that can be executed by a computer
• Programming:
the manufacturing of a program
• Programming environment:
tool set to augment human intelligence (you) in
programming [Douglas Engelbart]
11
Algorithms in daily live
Instructions for use of ‘Cup a Soup’:
1) Put the content of the bag in a soup bowl
or mug.
2) Add ca. 175 ml of boiling water, stir up... ready!
Highlights?
• use of subdivision of algorithms
• sequence is important
• requires understanding of the processor
12
Recipe “Crescent Cookies” Highlights?
• Involves elementary
Ingredients:
1 cup cornstarch, 1 cup powdered sugar, 2
algorithms
cups flour, sifted, 3 sticks margarine or • Describes operations
butter, ½ cup finely ground nuts
on objects
Preparation: • Introduces new
Sift dry ingredients together.
Blend in margarine and nuts. algorithms with a name
Chill, then roll dough out to about 1/8 inch (abstraction)
thick.
Use your choice of cookie cutter, or shape by • Manipulates elementary
hand into crescents.
and compound objects
Baking:
Bake at 300 degrees for about 20…25
minutes until lightly browned.
Finishing:
Sprinkle with confectioners sugar.
13
Algorithms and Languages
Algorithms: Syntax:
• Elementary what are well-formed
• Compound programs?
Semantics:
Algorithms act [almost always] what is the effect of a
on one or more data objects
program?
Data objects
• Elementary
• Compound
14
Using a language
To communicate with a computer
natural languages are too complex:
1) Natural language is too rich
2) Mutual understanding is required
15
Challenge & problem
You must perform two aspects well:
We use a systematic
approach
16
Program Design Process (Savitch)
sometimes
you must go
back...
You only can be sure that your algorithm is
good, if the result is a correct working
17
implementation!
Systematic design of algorithms
• Express algorithms in C++
• Use only very small part of the language
• Control structure:
construct to assemble algorithms
such as:
• sequence
• choice
• repetition
• composition
• abstraction (⇒ procedures/functions)
18
Charles the Robot
Elementary algorithms:
• step ()
• turn_left ()
• turn_right ()
• get_ball ()
• put_ball ()
19
Charles the Robot
Testing algorithms:
• in_front_of_wall ()
• on_ball ()
• north ()
20
Algorithms as C++ functions
• void expressive_name ()
• Function body between { }
– control structures, Charles algorithms and tests
• (re-)use your algorithms
void swap_ball () void swap_2_balls ()
{ {
if (on_ball ()) swap_ball () ;
get_ball () ; swap_ball () ;
else
}
put_ball () ;
step () ;
}
22
Choice
One of two possibilities has to occur
void swap_ball ()
if and else are keywords
{
if ( on_ball () )
get_ball () ;
else
put_ball () ;
step () ;
}
24
How does this work?
void swap_2_balls ()
{
swap_ball () ;
swap_ball () ;
}
void swap_ball ()
{
if (on_ball ())
get_ball () ;
else
put_ball () ;
step () ;
25
}
How does this work?
void swap_2_balls ()
{
swap_ball ();
swap_ball ();
}
void swap_ball ()
{
if (on_ball ())
get_ball ();
else
put_ball ();
step ();
26
}
How does this work?
void swap_2_balls ()
{
swap_ball ();
swap_ball ();
}
void swap_ball ()
{
if (on_ball ())
get_ball ();
else
put_ball ();
step ();
27
}
How does this work?
void swap_2_balls ()
{
swap_ball ();
swap_ball ();
}
void swap_ball ()
{
if (on_ball ())
get_ball ();
else
put_ball ();
step ();
28
}
How does this work?
void swap_2_balls ()
{
swap_ball ();
swap_ball ();
}
void swap_ball ()
{
if (on_ball ())
get_ball ();
else
put_ball ();
step ();
29
}
How does this work?
void swap_2_balls ()
{
swap_ball ();
swap_ball ();
}
void swap_ball ()
{
if (on_ball ())
get_ball ();
else
put_ball ();
step ();
30
}
How does this work?
void swap_2_balls ()
{
swap_ball ();
swap_ball ();
}
void swap_ball ()
{
if (on_ball ())
get_ball ();
else
put_ball ();
step ();
31
}
How does this work?
void swap_2_balls ()
{
swap_ball ();
swap_ball ();
}
void swap_ball ()
{
if (on_ball ())
get_ball ();
else false
put_ball ();
step ();
32
}
How does this work?
void swap_2_balls ()
{
swap_ball ();
swap_ball ();
}
void swap_ball ()
{
if (on_ball ())
get_ball ();
else
put_ball ();
step ();
33
}
How does this work?
void swap_2_balls ()
{
swap_ball ();
swap_ball ();
}
void swap_ball ()
{
if (on_ball ())
get_ball ();
else
put_ball ();
step ();
34
}
How does this work?
void swap_2_balls ()
{
swap_ball ();
swap_ball ();
}
void swap_ball ()
{
if (on_ball ())
get_ball ();
else
put_ball ();
step ();
35
}
How does this work?
void swap_2_balls ()
{
swap_ball ();
swap_ball ();
}
void swap_ball ()
{
if (on_ball ())
get_ball ();
else
put_ball ();
step ();
36
}
How does this work?
void swap_2_balls ()
{
swap_ball ();
swap_ball ();
}
void swap_ball ()
{
if (on_ball ())
get_ball ();
else
put_ball ();
step ();
37
}
How does this work?
void swap_2_balls ()
{
swap_ball ();
swap_ball ();
}
void swap_ball ()
{
if (on_ball ())
get_ball ();
else
put_ball ();
step ();
38
}
How does this work?
void swap_2_balls ()
{
swap_ball ();
swap_ball ();
}
void swap_ball ()
{
if (on_ball ())
get_ball ();
else
put_ball ();
step ();
39
}
How does this work?
void swap_2_balls ()
{
swap_ball ();
swap_ball ();
}
void swap_ball ()
{
if (on_ball ())
get_ball ();
else
put_ball ();
step ();
40
}
How does this work?
void swap_2_balls ()
{
swap_ball ();
swap_ball ();
}
void swap_ball ()
{
if (on_ball ())
get_ball ();
else
put_ball ();
step ();
41
}
How does this work?
void swap_2_balls ()
{
swap_ball ();
swap_ball ();
}
void swap_ball ()
{
if (on_ball ())
get_ball ();
else
put_ball ();
step ();
42
}
How does this work?
void swap_2_balls ()
{
swap_ball ();
swap_ball ();
}
void swap_ball ()
{
if (on_ball ())
get_ball ();
else
put_ball ();
step ();
43
}
How does this work?
void swap_2_balls ()
{
swap_ball ();
swap_ball ();
}
void swap_ball ()
{
if (on_ball ())
get_ball ();
else
put_ball ();
step ();
44
}
How does this work?
void swap_2_balls ()
{
swap_ball ();
swap_ball ();
}
void swap_ball ()
{
if (on_ball ())
get_ball ();
else
put_ball ();
step ();
45
}
How does this work?
void swap_2_balls ()
{
swap_ball ();
swap_ball ();
}
void swap_ball ()
{
if (on_ball ())
get_ball ();
else
put_ball ();
step ();
46
}
How does this work?
void swap_2_balls ()
{
swap_ball ();
swap_ball ();
}
void swap_ball ()
{
if (on_ball ())
get_ball ();
else true
put_ball ();
step ();
47
}
How does this work?
void swap_2_balls ()
{
swap_ball ();
swap_ball ();
}
void swap_ball ()
{
if (on_ball ())
get_ball ();
else
put_ball ();
step ();
48
}
How does this work?
void swap_2_balls ()
{
swap_ball ();
swap_ball ();
}
void swap_ball ()
{
if (on_ball ())
get_ball ();
else
put_ball ();
step ();
49
}
How does this work?
void swap_2_balls ()
{
swap_ball ();
swap_ball ();
}
void swap_ball ()
{
if (on_ball ())
get_ball ();
else
put_ball ();
step ();
50
}
How does this work?
void swap_2_balls ()
{
swap_ball ();
swap_ball ();
}
void swap_ball ()
{
if (on_ball ())
get_ball ();
else
put_ball ();
step ();
51
}
How does this work?
void swap_2_balls ()
{
swap_ball ();
swap_ball ();
}
void swap_ball ()
{
if (on_ball ())
get_ball ();
else
put_ball ();
step ();
52
}
How does this work?
void swap_2_balls ()
{
swap_ball ();
swap_ball ();
}
void swap_ball ()
{
if (on_ball ())
get_ball ();
else
put_ball ();
step ();
53
}
How does this work?
void swap_2_balls ()
{
swap_ball ();
swap_ball ();
}
void swap_ball ()
{
if (on_ball ())
get_ball ();
else
put_ball ();
step ();
54
}
How does this work?
void swap_2_balls ()
{
swap_ball ();
swap_ball ();
}
void swap_ball ()
{
if (on_ball ())
get_ball ();
else
put_ball ();
step ();
55
}
How does this work?
void swap_2_balls ()
{
swap_ball ();
swap_ball ();
}
void swap_ball ()
{
if (on_ball ())
get_ball ();
else
put_ball ();
step ();
56
}
Boolean expressions (1)
• Conditions often need to be combined
• Example:
Charles places a ball on an empty location
void safe_put_ball ()
{
if ( !on_ball () )
put_ball () ;
}
Same effect? NO
61
Composition (2)
... Choice with composition
if (in_front_of_wall ())
{
turn_left () ;
safe_put_ball () ;
} Note: the effect of
... these fragments is
quite different!
Same effect? NO
... ...
while (in_front_of_wall ()) while (in_front_of_wall ())
{ turn_left () ;
turn_left () ; safe_put_ball () ;
safe_put_ball () ; ...
}
... 63
Composition (4)
... ...
if (in_front_of_wall ()) if (in_front_of_wall ());
{ {
turn_left () ; turn_left () ;
safe_put_ball () ; safe_put_ball () ;
} }
... ...
Same effect? NO
Same effect? NO
... ...
while (in_front_of_wall ()) while (in_front_of_wall ());
{ {
turn_left () ; turn_left () ;
safe_put_ball () ; safe_put_ball () ;
} }
... ... 64
What are you going to do?
Study Savitch: check ‘cursusplanning Blackboard’
Mandatory assignments for Charles the robot
– cleaning up a string of balls
– cleaning up chaos with balls
Bonus part
– walk around the block
65
Case study: find the ball
void find_ball ()
66
Program Design Process (Savitch)
67
Case study: find the ball
– problem definition –
Identify constraints / properties of the problem:
• Charles is at starting position, looking east
• There is at least one ball
• Charles stands still on the first found ball
• Charles has limited senses
68
Case study: find the ball
– algorithm design –
How to find the ball:
1. sweep lanes (to east and west) southbound to find ball
void find_ball ()
69
Case study: find the ball
– algorithm design –
1. How to sweep lanes:
1.1. As long as Charles is not on a ball:
1.2. sweep current lane to the east
1.3. at east, move one lane to south if not on a ball
1.4. sweep current lane to the west
1.5. at west, move one lane to south if not on a ball
void sweep_lanes ()
70
Case study: find the ball
– algorithm design –
1.2. How to sweep the current lane:
1.2.1. As long as Charles is not on a ball
and not in front of wall:
make Charles step
void sweep_lane ()
71
Case study: find the ball
– algorithm design –
1. How to sweep lanes:
1.1. As long as Charles is not on a ball:
1.2. sweep current lane to the east
1.3. at east, move one lane to south if not on a ball
1.4. sweep current lane to the west
1.5. at west, move one lane to south if not on a ball
void sweep_lanes ()
72
Case study: find the ball
– algorithm design –
1.3. How to move one lane south if not on ball at east:
1.3.1. If Charles is not on a ball:
make Charles turn right
make Charles step
make Charles turn right
void go_down_east_if_not_on_ball ()
73
Case study: find the ball
– algorithm design –
1.5. How to move one lane south if not on ball at west:
1.5.1. If Charles is not on a ball:
make Charles turn left
make Charles step
make Charles turn left
void go_down_west_if_not_on_ball ()
74
Case study: find the ball
– algorithm design –
1. How to sweep lanes:
1.1. As long as Charles is not on a ball:
1.2. sweep current lane to the east
1.3. at east, move one lane to south if not on a ball
1.4. sweep current lane to the west
1.5. at west, move one lane to south if not on a ball
void sweep_lanes ()
75
Case study: find the ball
– algorithm design –
Scenario how to find the ball:
1. sweep lanes (to east and west) southbound to find ball
76
Program Design Process (Savitch)
77
Case study: find the ball
– translating to C++ –
Scenario how to find the ball:
1. sweep lanes (to east and west) southbound to find ball
void find_ball ()
{
sweep_lanes () ;
}
78
Case study: find the ball
– translating to C++ –
1. How to sweep lanes:
1.1. As long as Charles is not on a ball:
1.2. sweep current lane to the east
1.3. at east, move one lane to south if not on a ball
1.4. sweep current lane to the west
1.5. at west, move one lane to south if not on a ball
void sweep_lanes ()
{ while (! on_ball () )
{ sweep_lane () ;
go_down_east_if_not_on_ball () ;
sweep_lane () ;
go_down_west_if_not_on_ball () ;
}
} 79
Case study: find the ball
– translating to C++ –
1.2. How to sweep the current lane:
1.2.1. As long as Charles is not on a ball
and not in front of wall:
make Charles step
void sweep_lane ()
{
while (! on_ball () && ! in_front_of_wall () )
step () ;
}
80
Case study: find the ball
– translating to C++ –
1.3. How to move one lane south if not on ball at east:
1.3.1. If Charles is not on a ball:
make Charles turn right
make Charles step
make Charles turn right
void go_down_east_if_not_on_ball ()
{ if (! on_ball ())
{
turn_right () ;
step () ;
turn_right () ;
}
} 81
Case study: find the ball
– translating to C++ –
1.5. How to move one lane south if not on ball at west:
1.5.1. If Charles is not on a ball:
make Charles turn left
make Charles step
make Charles turn left
void go_down_west_if_not_on_ball ()
{ if (! on_ball ())
{
turn_left () ;
step () ;
turn_left () ;
}
} 82
What have we done?
• C++:
sequence (;), choice (if-else), iteration (while),
composition ({ }), Boolean expressions (!, &&, ||)
• Charles:
step(), turn_left(), turn_right(),
get_ball(), put_ball(),