A Brief Programming Overview - Indico · 7 Orienting the Objects Simplicity beneath complexity...
Transcript of A Brief Programming Overview - Indico · 7 Orienting the Objects Simplicity beneath complexity...
1
A Brief Programming OverviewA Brief Programming OverviewBasic algorithmic thinking Basic algorithmic thinking (within C/C++)(within C/C++)
TargetWhat does a developer wantwant ?What does a developer dodo ?
SequentialSequential programingPhone-book application
Orienting the objectsOrienting the objectsSimplicity beneath complexityRectangle and Triangle derived from Polygon
ArrowArrow class derived from LineLine class Line class implementationArrow class implementationUsageUsage within user codeUsageUsage within user code as pointeras pointer
Code, Heap, StackMemoryMemory regions applications use
What do we mean by “thinking” ?Flow Flow charts and UML UML diagrams
Fundamental programing phrasesLoopsLoops & conditionalsconditionalsApproximating π by random numbersrandom numbersπ approximator implementations in different languages with flow chart
Compilation and usage of librarieslibraries Compiling line and arrow classes into libLineAndArrow.so shared libraryshared library
Inte
rnati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eIn
tern
ati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eA
Bri
ef
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
A B
rief
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
Graphical programingDrag'n'drop commandsDrag'n'drop commands in ScratchGet input from a dedicated HW - PicoBoard
Bonus TrackBonus Track
2
TargetTargetWhat could a developer What could a developer wantwant ? ?
Are there more importantmore important things thanthan a high performanceperformance software ?
Modularity, user-friendliness, correctnessprogrammer time, maintainability, simplicity,
functionality, extensibility,robustness,
reliability???
Inte
rnati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eIn
tern
ati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eA
Bri
ef
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
A B
rief
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
** Translated from Turkish original by Selçuk Erdem
Europeanside
Anatolianside
3
FundamentallyFundamentallyWhat does a developer What does a developer dodo ? ?
What ?
The designdesign problem...
is divideddivided into piecespieces andis understoodunderstood piece piece by piecepiece
StudyStudy the piecespieces in detail andUniteUnite your detailed understanding
= solutionsolution
How ?
Chose...
proceduralprocedural programingobject-orientedobject-oriented programingaspect-orientedaspect-oriented programing
...
Inte
rnati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eIn
tern
ati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eA
Bri
ef
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
A B
rief
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
4
Fundamentally Fundamentally What does a developer What does a developer dodo ? ?
(I guess)(I guess)Before starting coding:
ThinksThinks and creates a flow chartflow chart on a paperThinksThinks and makes sure that the flow works on the paperworks on the paper
ThinksThinks and then startsstarts coding
(Subjective)(Subjective)Programing = Thinking = Design (ElegantElegant)
Coding = execution (OrdinaryOrdinary)
Language independentindependent (Türkçe/François/C/C++/Perl etc.)Least error-prone technique
Larger gets the SW, exponentially increasing maintenance loadmaintenance load
(Absolute truth)(Absolute truth)
NoneNone of the languages is the bestbest solution for everything !!
Inte
rnati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eIn
tern
ati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eA
Bri
ef
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
A B
rief
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
5
Make the right decision at the beginningMake the right decision at the beginningExponentially increasing maintenance load means...Exponentially increasing maintenance load means...
Can I come with Can I come with you ? you ? I promiseI promise I'll I'll
stay stay this sizethis size..
Non-hierarchical software !..
Inte
rnati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eIn
tern
ati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eA
Bri
ef
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
A B
rief
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
6
Sequential ProgramingSequential ProgramingExample: Phone book app.Example: Phone book app.
Main function:Depending on what the user selected, make make the function callsthe function callsIf Quit is selected (case 7 = Selection 7), exit the app.exit the app. by returning a zero to the OS.
Implementation of load function:Create the pointerspointers (di and al)Create the variablesvariables (number, k anf I)Is the file readable file readable (phoneBook.dat)
If yes, go to the end and measure the measure the sizesize
Divide this measure size by the size of size of RECORDRECORD struct (how many records in file)Create a loop based on this number to read all recordsread all recordsAdd all records just read into the dynamically linked list (via utilizing addNodeaddNode function)
*** Example taken from Rifat Çölkesen's book “işte C programlama”, Dynamically Linked List (DLL) section.
***
Inte
rnati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eIn
tern
ati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eA
Bri
ef
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
A B
rief
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
7
Orienting the ObjectsOrienting the ObjectsSimplicity beneath complexitySimplicity beneath complexity
Complex architecturesComplex architectures can easilyeasily be constructedconstructed by hierarchical object-oriented programing practicesThis is the most effectivemost effective (the least head-ache prone) programing practice
?If one wants to create an arrowarrow, the only thing to be programed is a lineline; an arrow can be constructed by utilizing three line objectsthree line objectslin
e B
line Cline A
Class line
A(x0,y
0)
B(x1,y
1)
Class arrow
(0,0)(10,0)
(9,1)
(9,-1)
Inte
rnati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eIn
tern
ati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eA
Bri
ef
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
A B
rief
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
8
ExampleExampleTriangleTriangle and and rectanglerectangle derived from a derived from a polygonpolygon
All polygonspolygons have a highthight and a widthwidth
TriangleTriangle & rectanglerectangle are polygonspolygons
Establish class hierarchy:create polygon classpolygon class which will hold widthwidth and heightheight informationderivederive triangle and rectangle classesTriangle and rectangle classes inheritinheritfrom polygon base classbase class
Create a triangle and a rectangle objectobject with width-height couple of 4 and 5 respectively
Derived classes do not have setValue()setValue()member functionmember function however the base class from which they inherit has one
Area() member functionArea() member function of the derived classes does not exist in the base class
Inte
rnati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eIn
tern
ati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eA
Bri
ef
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
A B
rief
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
Baseclass
Derivedclasses
9
ExampleExampleLineLine class header class header
A line can be defineddefinedin different ways:
Two pointsPoint, direction, lengthVectorEtc.
Let us use two pointstwo points
Inte
rnati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eIn
tern
ati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eA
Bri
ef
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
A B
rief
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
Class line
A(x0,y
0)
B(x1,y
1)
10
ExampleExampleArrowArrow class header class header
An arrow can be made of 3 lines
A main linemain line2 auxiliary linesauxiliary linesforming the arrow head
Size and angle of the auxiliary lines with respect to the size of the main line will be hold by the member member variable variable arrowWidtharrowWidth
Inte
rnati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eIn
tern
ati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eA
Bri
ef
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
A B
rief
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
line
B
line C
line A
Class arrow
(0,0)(10,0)
(9,1)
(9,-1)
11
ExampleExampleLineLine class classimplementationimplementation
All member functionsmember functions(or methods) have access to all of the private memberprivate membervariables
assignValue()length()show()
Inte
rnati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eIn
tern
ati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eA
Bri
ef
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
A B
rief
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
Class line
A(x0,y
0)
B(x1,y
1)
12
ExampleExampleArrowArrow class classimplementationimplementation
Instead of building an arrow from scratch, use objects of line class: assignValue()assignValue()Instead of calculating all the points, ask each of the line objects show() &show() &length()length()Methods overriddenoverridden
Inte
rnati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eIn
tern
ati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eA
Bri
ef
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
A B
rief
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
line
B
line C
line A
Class arrow
(0,0)(10,0)
(9,1)
(9,-1)
13
ExampleExampleArrowArrow class user class userapplicationapplication
After having the base class, we will use it in our applicationapplication:
main.cxx
We want to createcreate an arrow between (0,5)(0,5) and (10,5)(10,5), readread the coordinatescoordinates forming the arrow and get the total total lengthlength of the line used to form the arrow
We reach member functions of MyBelovedArrowMyBelovedArrow object via “.” “.” operator
MyBelovedArrow is created in the stackstackIn
tern
ati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eIn
tern
ati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eA
Bri
ef
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
A B
rief
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
14
SameSame library (arrow class) and samesameuser request
The object is created as a pointerpointer via newnew operator (Line 6)
Since MyBelovedArrow is now of pointer typepointer type, its member functions are reached via ““->->”” operator instead of ““..”” operator. (Lines 14, 17 and 20)
Object is being deleted after usagedeleted after usageso that the memorymemory returns back to the operating system to be used by other programs (Line 24); in case this fails, a memory leakmemory leak occurs: memory is allocatedallocated but not usednot used by any process
MyBelovedArrow object is created on the heapheap
Inte
rnati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eIn
tern
ati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eA
Bri
ef
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
A B
rief
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
ExampleExampleArrowArrow class user class userapplicationapplication
15
ExampleExampleArrow class user application outputArrow class user application output
CompileCompile your librarylibrary and the user applicationuser application as follows:g++ main.cxx line.cxx arrow.cxx o MyExecutableand executeexecute the program as follows:
oc@olmak2:~/ISOTDAQ/workDir/arrow$ ./MyExecutable Main line:1st Coordinate = (0.000000, 5.000000) 2nd Coordinate = (10.000000, 5.000000) Upper auxiliary line:1st Coordinate = (9.000000, 4.000000) 2nd Coordinate = (10.000000, 5.000000) Bottom auxiliary line:1st Coordinate = (9.000000, 6.000000) 2nd Coordinate = (10.000000, 5.000000) Total length = 12.828426 oc@olmak2:~/ISOTDAQ/workDir/arrow$ _
Inte
rnati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eIn
tern
ati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eA
Bri
ef
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
A B
rief
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
White characters form the actual output corresponding to the implemented example here. Gray characters are added for formatting purposes. They could also be added to the class implementation, though.
16
Code, Heap and StackCode, Heap and StackReminder on the memory types applications useReminder on the memory types applications use
When invoked, applications use three memory segments: texttext or codecode, heapheap and stackstackCode segmentCode segment hosts the machine representation of compiled executable. It is the list of actions to be performed by the computer when the app. starts. All user and system functions reside in this segment.
● StackStack and heapheap are system memory regions that the compiler uses to store data such as variables and functions. Although having similar definitions, they differ in the following manners:
StackStack, hosts ordinary variables and functions. Once the execution exits the coverage or name-space in which those variables and functions are defined or the program itself ends, this memory memory isis released released. It has a Last In First Out (LIFOLIFO) structure. Its control is left to the compiler.HeapHeap, does the same thing as stack; however, unless user wants, allocated memory memory isis notnot released released; its control is left to the user. Therefore heap is more suitable to store data. It is widely used in object-oriented programing. When not released properly by the user, memory leakmemory leak occurs. Many languages do not have garbage collectorgarbage collector functionality which releases leaking memory segments.
To create an object in the stackstack or on the heapheap does make practical difference.
main()
Top
variables
main()Top
function()
main()Top
HeapHeap working principle
Inte
rnati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eIn
tern
ati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eA
Bri
ef
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
A B
rief
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
17
A Brief Programming OverviewA Brief Programming OverviewBasic algorithmic thinking Basic algorithmic thinking (within C/C++)(within C/C++)
TargetWhat does a developer wantwant ?What does a developer dodo ?
SequentialSequential programingPhone-book application
Orienting the objectsOrienting the objectsSimplicity beneath complexityRectangle and Triangle derived from Polygon
ArrowArrow class derived from LineLine class Line class implementationArrow class implementationUsageUsage within user codeUsageUsage within user code as pointeras pointer
Code, Heap, StackMemoryMemory regions applications use
What do we mean by “thinking” ?Flow Flow charts and UML UML diagrams
Fundamental programing phrasesLoopsLoops & conditionalsconditionalsApproximating π by random numbersrandom numbersπ approximator implementations in different languages with flow chart
Compilation and usage of librarieslibraries Compiling line and arrow classes into libLineAndArrow.so shared libraryshared library
Graphical programingDrag'n'drop commandsDrag'n'drop commands in ScratchGet input from a dedicated HW - PicoBoard
Bonus TrackBonus Track
Inte
rnati
on
al Sch
ool on T
rigg
er
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
20
10
, A
nka
ra, Tü
rkiy
eIn
tern
ati
on
al Sch
ool on T
rigg
er
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
20
10
, A
nka
ra, Tü
rkiy
eA
Bri
ef
Pro
gra
mm
ing O
verv
iew
– Ö
zgür
Çob
an
oğlu
A B
rief
Pro
gra
mm
ing O
verv
iew
– Ö
zgür
Çob
an
oğlu
18
By thinking, we mean...By thinking, we mean...Flow charts and UML diagramsFlow charts and UML diagrams
Flow chartFlow chart in sequentialsequential programingUMLUML (unified modeling language) diagrams in object-orientedobject-oriented programing
Flow chartFlow chart again, this time for the member functionsmember functions
OnceOnce a UML diagramUML diagram is created, object-oriented class hierarchy is automaticallyautomatically generated in C++ or others
OROROnceOnce we have a class hierarchyclass hierarchy, UML diagram of the design can be generated automatically to have an idea on the designed architecture
line.harrow.h
Source code automatically generated from UML diagram
Morecomplex
Inte
rnati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eIn
tern
ati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eA
Bri
ef
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
A B
rief
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
Short-handflow chart
Actualcode
UML diagram ofthe arrow class
19
Reconstructed tracksbased onA(x,y) of the TOPAndB(x,y) of the BOTTOM
Cosmic particlespassing throughthe detector
Experimental setup Event reconstruction Saturated event display
Fundamental PhrasesFundamental PhrasesLoops and conditionalsLoops and conditionals
trackTable(trackTable(xx11,y,y11,,xx22,y,y22)) is of boolbooltype 4-dimension array which holds “yes”“yes” or “no”“no” information depending on whether there is already a track between A(A(xx11,y,y11))and B(B(xx22,y,y22)) which correspond to particle-hit coordinates of two parallel strip detectorsparallel strip detectors. The looploopis used to resetreset this table.
This table is used (not shown in the code) not to create redundantredundant linelineobjectsobjects in case it already exists
Inte
rnati
on
al Sch
ool on T
rigg
er
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
20
10
, A
nka
ra, Tü
rkiy
eIn
tern
ati
on
al Sch
ool on T
rigg
er
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
20
10
, A
nka
ra, Tü
rkiy
eA
Bri
ef
Pro
gra
mm
ing O
verv
iew
– Ö
zgür
Çob
an
oğlu
A B
rief
Pro
gra
mm
ing O
verv
iew
– Ö
zgür
Çob
an
oğlu
20
Fundamental PhrasesFundamental PhrasesLoops and conditionalsLoops and conditionals
IfIf check4data()check4data() reports failure, that is, it returns a non-zeronon-zero value, log this event (Line 596); otherwiseotherwise try to read the data via readDataFile()readDataFile() function and depending on whetherwhether it succeeds, log corresponding message (Lines 599 or 601).
Inte
rnati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eIn
tern
ati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eA
Bri
ef
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
A B
rief
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
Pyt
ho
nP
yth
on
C/C
++
C/C
++
21
Fundamental PhrasesFundamental PhrasesLoops and conditionalsLoops and conditionals
ShowsShows user a menu of functions and returnsreturns the selection to caller (return c):Menu item printing looploop (Line 27)ExpectExpect input only between 1 and 7 (Line 31)When validWhen valid input, input is returnedinput is returned to the caller (Line 32)
Inte
rnati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eIn
tern
ati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eA
Bri
ef
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
A B
rief
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
Pyt
ho
nP
yth
on
C/C
++
C/C
++
22
Fundamental PhrasesFundamental PhrasesLoops and conditionalsLoops and conditionals
Inte
rnati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eIn
tern
ati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eA
Bri
ef
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
A B
rief
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
In an infinite loopinfinite loop –note that the condition is always true: while(1)while(1), ask user for his/her choice and call functions accordingly (Line 244)ConditionalConditional on an integer variable “selection”variable “selection”:
IfIf selection is 11, call add()add() function (Line 248)IfIf selection is 77, exitexit the application (Line 266)
Pyt
ho
nP
yth
on
C/C
++
C/C
++
23
Fundamental PhrasesFundamental PhrasesApproximating Approximating ππ via random numbers via random numbers
Consider a square wallsquare wall with a circular circular holehole in it:
Throw stonesThrow stones into the square wallOr two random numbersrandom numbers as xx and yy
CountCount the stones passing through the hole to the other size and the ones which can notThe ratioratio should be proportional to ππ as follows:
Inte
rnati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eIn
tern
ati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eA
Bri
ef
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
A B
rief
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
x
yr
Wall
Hole
Stone
Dia
met
er
Asquare = r²πAcircle = 4r²Asquare/Acircle = /4π
Start
maxIteration=1K
Throw a stone
Inside ?
Calculate andreport result
Asquare
+ 1A
circle + 1
Asquare
+ 1
# of stones + 1
maxItarationreached ?
Yes
No
Yes
No
End
Flow-chart of the algorithm
24
Fundamental PhrasesFundamental PhrasesIn
tern
ati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eIn
tern
ati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eA
Bri
ef
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
A B
rief
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
Approximating Approximating ππ via random numbers via random numbers
Pyt
ho
nP
yth
on
C/C
++
C/C
++
ForTranForTran
Procedural implementationProcedural implementation examples of the algorithm in C/ForTran/Python languages
25
Compiling and Using LibrariesCompiling and Using LibrariesCompiling Compiling lineline and and arrowarrow classes into classes into libLineAndArrow.solibLineAndArrow.so library and its usage library and its usage
> g++ c fPIC line.cxx> g++ c fPIC arrow.cxx> g++ shared arrow.o line.o o libLineAndArrow.so> sudo cp libLineAndArrow.so /usr/lib/> g++ MyExecutable.cxx o MyExecutable lLineAndArrow> ./MyExectableAna cizgi:1. Nokta = (0.000000, 5.0000000.000000, 5.000000) 2. Nokta = (10.000000, 5.00000010.000000, 5.000000) Ust cizgi:1. Nokta = (9.000000, 4.0000009.000000, 4.000000) 2. Nokta = (10.000000, 5.00000010.000000, 5.000000) Alt cizgi:1. Nokta = (9.000000, 6.0000009.000000, 6.000000) 2. Nokta = (10.000000, 5.00000010.000000, 5.000000) Length = 12.828426Length = 12.828426 > _
Compile each class with g++ switch “-c-c” which creates object files “*.o*.o” for each class
The components (“*.o”“*.o”) of the final library (“*.so”“*.so”) is compiled with the switch “-sharedshared” which generates libLineAndArrow.so shared object library.
Copy newly generate shared object or library to /usr/lib so that every user can have access
Compile user programs with “-lLineAndArrow” switch
Note that the user does notnot need to have source codesource code of the classes forming the library, however he/she needs to know the names and names and signaturessignatures of the functions
Inte
rnati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eIn
tern
ati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eA
Bri
ef
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
A B
rief
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
26
A Brief Programming OverviewA Brief Programming OverviewBasic algorithmic thinking Basic algorithmic thinking (within C/C++)(within C/C++)
TargetWhat does a developer wantwant ?What does a developer dodo ?
SequentialSequential programingPhone-book application
Orienting the objectsOrienting the objectsSimplicity beneath complexityRectangle and Triangle derived from Polygon
ArrowArrow class derived from LineLine class Line class implementationArrow class implementationUsageUsage within user codeUsageUsage within user code as pointeras pointer
Code, Heap, StackMemoryMemory regions applications use
What do we mean by “thinking” ?Flow Flow charts and UML UML diagrams
Fundamental programing phrasesLoopsLoops & conditionalsconditionalsApproximating π by random numbersrandom numbersπ approximator implementations in different languages with flow chart
Compilation and usage of librarieslibraries Compiling line and arrow classes into libLineAndArrow.so shared libraryshared library
Inte
rnati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eIn
tern
ati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eA
Bri
ef
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
A B
rief
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
Graphical programingDrag'n'drop commandsDrag'n'drop commands in ScratchGet input from a dedicated HW - PicoBoard
Bonus TrackBonus Track
Inte
rnat
iona
l Sch
ool o
n T
rigge
r an
d D
ata
Acq
uisi
tion,
1-7
Feb
rua
ry 2
010,
An
kara
, Tür
kiye
Inte
rnat
iona
l Sch
ool o
n T
rigge
r an
d D
ata
Acq
uisi
tion,
1-7
Feb
rua
ry 2
010,
An
kara
, Tür
kiye
Pic
oB
oar
d &
Sc
ratc
hP
ico
Bo
ard
& S
cra
tch
– Ö
zgür
Çob
ano
– Ö
zgür
Çob
ano ğ
luğlu
Scratch & PicoBoardScratch & PicoBoardA brief introductionA brief introduction
Scratch (SW)(SW)The environmentGraphical programing languageEx. calculation of π via random numbers
PicoBoard (HW) (HW)A simplified DAQ system
Joining the forces (SW + HW)(SW + HW)Combining Scratch with PicoBoardEx. start/stop the previous example by clapping hands
Inte
rnat
iona
l Sch
ool o
n T
rigge
r an
d D
ata
Acq
uisi
tion,
1-7
Feb
rua
ry 2
010,
An
kara
, Tür
kiye
Inte
rnat
iona
l Sch
ool o
n T
rigge
r an
d D
ata
Acq
uisi
tion,
1-7
Feb
rua
ry 2
010,
An
kara
, Tür
kiye
Pic
oB
oar
d &
Sc
ratc
hP
ico
Bo
ard
& S
cra
tch
– Ö
zgür
Çob
ano
– Ö
zgür
Çob
ano ğ
luğlu
ScratchScratchA graphical programing environmentA graphical programing environment
Scratch is a new graphical programing languagegraphical programing language
IntuitiveIntuitive; it has a reference guide, though.
Writing computer programs doesn’t require special expertise, but clear and careful thinkingthinking
That is one of the reasonsone of the reasons to chose it as the environment
ThreadsThreads - “hat” symbols start independent execution blocks which run parallely
GUI EventsGUI Events – boolean operations, variable usage,
conditionals, GUI event handling, interaction with
users (below)
LoopsLoops – Finite and forever loops can be implemented
Inte
rnat
iona
l Sch
ool o
n T
rigge
r an
d D
ata
Acq
uisi
tion,
1-7
Feb
rua
ry 2
010,
An
kara
, Tür
kiye
Inte
rnat
iona
l Sch
ool o
n T
rigge
r an
d D
ata
Acq
uisi
tion,
1-7
Feb
rua
ry 2
010,
An
kara
, Tür
kiye
Pic
oB
oar
d &
Sc
ratc
hP
ico
Bo
ard
& S
cra
tch
– Ö
zgür
Çob
ano
– Ö
zgür
Çob
ano ğ
luğlu
ScratchScratchWhat is available / unavailable ?What is available / unavailable ?
AvailableAvailableSequential programingLoopsLoopsConditionalsConditionalsVariablesVariablesLists/arraysarraysEventEvent handlingThreadsThreads/parallel executionSignal-slotSignal-slot mechanismKeyboard inputRandom numberRandom number generatorBooleanBoolean logicDynamic interactionUser interface designFile access
UnavailableUnavailableProcedures and functionsfunctionsParameterParameter passing and returnreturn valuesRecursionRecursionObject-orientationObject-orientation and inheritanceExceptionException handling
Available “statements”
The “code”
Resource manager
Output
Start
maxIteration=1K
Throw a stone
Inside ?
Calculate andreport result
Asquare
+ 1A
circle + 1
Asquare
+ 1
# of stones + 1
maxItarationreached ?
Yes
No
Yes
No
End
Asquare = r²πAcircle = 4r²Asquare/Acircle = /4π
Inte
rnat
iona
l Sch
ool o
n T
rigge
r an
d D
ata
Acq
uisi
tion,
1-7
Feb
rua
ry 2
010,
An
kara
, Tür
kiye
Inte
rnat
iona
l Sch
ool o
n T
rigge
r an
d D
ata
Acq
uisi
tion,
1-7
Feb
rua
ry 2
010,
An
kara
, Tür
kiye
Pic
oB
oar
d &
Sc
ratc
hP
ico
Bo
ard
& S
cra
tch
– Ö
zgür
Çob
ano
– Ö
zgür
Çob
ano ğ
luğlu
Algorithm ImplementationAlgorithm ImplementationCalculatingCalculatingππ via Monte Carlovia Monte Carlo
x
yr
Wall
Hole
Stone
Diameter
ThrowThrow stones into the holeor take 2 random numbers as (x,y) 2 random numbers as (x,y) coordinates
Pass throughPass through the hole or notor not ?
Inte
rnat
iona
l Sch
ool o
n T
rigge
r an
d D
ata
Acq
uisi
tion,
1-7
Feb
rua
ry 2
010,
An
kara
, Tür
kiye
Inte
rnat
iona
l Sch
ool o
n T
rigge
r an
d D
ata
Acq
uisi
tion,
1-7
Feb
rua
ry 2
010,
An
kara
, Tür
kiye
Pic
oB
oar
d &
Sc
ratc
hP
ico
Bo
ard
& S
cra
tch
– Ö
zgür
Çob
ano
– Ö
zgür
Çob
ano ğ
luğlu
Algorithm ImplementationAlgorithm ImplementationCalculatingCalculatingππ via Monte Carlovia Monte Carlo
EasyEasy algorithm implementation
No forgottenNo forgotten curly parentheses :}:}
Translation
Start
maxIteration=1K
Throw a stone
Inside ?
Calculate andreport result
Asquare
+ 1A
circle + 1
Asquare
+ 1
# of stones + 1
maxItarationreached ?
Yes
No
Yes
No
End
Inte
rnat
iona
l Sch
ool o
n T
rigge
r an
d D
ata
Acq
uisi
tion,
1-7
Feb
rua
ry 2
010,
An
kara
, Tür
kiye
Inte
rnat
iona
l Sch
ool o
n T
rigge
r an
d D
ata
Acq
uisi
tion,
1-7
Feb
rua
ry 2
010,
An
kara
, Tür
kiye
Pic
oB
oar
d &
Sc
ratc
hP
ico
Bo
ard
& S
cra
tch
– Ö
zgür
Çob
ano
– Ö
zgür
Çob
ano ğ
luğlu
Scratch & PicoBoardScratch & PicoBoardA brief introductionA brief introduction
Scratch (SW)(SW)The environmentGraphical programing languageEx. calculation of π via random numbers
PicoBoard (HW) (HW)A simplified DAQ system
Joining the forces (SW + HW) (SW + HW)Combining Scratch with PicoBoardEx. start/stop the previous example by clapping hands
Introduction to PicoBoardIntroduction to PicoBoardA simplified DAQ hardwareA simplified DAQ hardware
The Scratch Sensor Board (aka PicoBoard) allows inputs from real-world sensorsallows inputs from real-world sensors such as switchesswitches, sliderssliders, lightlight, or soundsound sensors to control Scratch programscontrol Scratch programs.
It does this by converting sensor inputsconverting sensor inputs into numbers and communicating themcommunicating them to a computer running Scratchcomputer running Scratch over a serial dataover a serial data connection with an open protocolopen protocol.
Scratch is freelyfreely available and PicoBoard is an open hardware.open hardware.
Inte
rnat
iona
l Sch
ool o
n T
rigge
r an
d D
ata
Acq
uisi
tion,
1-7
Feb
rua
ry 2
010,
An
kara
, Tür
kiye
Inte
rnat
iona
l Sch
ool o
n T
rigge
r an
d D
ata
Acq
uisi
tion,
1-7
Feb
rua
ry 2
010,
An
kara
, Tür
kiye
Pic
oB
oar
d &
Sc
ratc
hP
ico
Bo
ard
& S
cra
tch
– Ö
zgür
Çob
ano
– Ö
zgür
Çob
ano ğ
luğlu
Schematic, firmware and communication protocol are all openopen
Introduction to PicoBoardIntroduction to PicoBoardA simplified DAQ hardwareA simplified DAQ hardware
It does this by converting sensor inputsconverting sensor inputs …PicoBoard has a micro-controllermicro-controller which performs all the functions; it is a tiny tiny computercomputer with all the needed peripheralsperipherals A micro-controllermicro-controller is a small computercomputer dedicated to a specific area on a single integrated circuit consisting of a simple CPUsimple CPU, RAMRAM, various timerstimers, digital and analog I/OI/O etc.
Inte
rnat
iona
l Sch
ool o
n T
rigge
r an
d D
ata
Acq
uisi
tion,
1-7
Feb
rua
ry 2
010,
An
kara
, Tür
kiye
Inte
rnat
iona
l Sch
ool o
n T
rigge
r an
d D
ata
Acq
uisi
tion,
1-7
Feb
rua
ry 2
010,
An
kara
, Tür
kiye
Pic
oB
oar
d &
Sc
ratc
hP
ico
Bo
ard
& S
cra
tch
– Ö
zgür
Çob
ano
– Ö
zgür
Çob
ano ğ
luğlu
Processor core, runs your C programSRAM, is the memory to store dataEEPROM/Flash, is the memory to store your programCounter/Timer, are counting units (e.g. # of seconds elapsed etc.)Digital I/O, is for sending/receiving digital informationSerial Interface, is to communicate with others via various protocols (e.g. USB, RS232, etc)Analog Module, to deal with analog signals, A/D converters insideInterrupt Controller, a mechanism to detect whether an important thing is happening “outside”
A possiblepossible micro-controller architecture
Introduction to PicoBoardIntroduction to PicoBoardA simplified DAQ hardwareA simplified DAQ hardware
Micro-controllerMicro-controller is not a micro-processormicro-processorMicro-processors need other components to work such assuch as memoriesmemories, I/OI/O, etcetc.
Micro-controller is an all-in-oneall-in-one deviceSaving timetime and spacespace to implement functions
Micro-controllers are less powerful compared to PCs; however they are:SmallSmall – 1 cm x 2-3 cmToughTough – high/low temperatures, rough environmentLow powerLow power consumersIntegratedIntegrated with other low-level deviceslow-level devices easily such assuch as sensorssensors, switchesswitches, etcetc.InexpensiveInexpensive – usually
Therefore they are everywhereeverywhere:Probably you already carry a few of them all the time in your pocketin your pocket
Inte
rnat
iona
l Sch
ool o
n T
rigge
r an
d D
ata
Acq
uisi
tion,
1-7
Feb
rua
ry 2
010,
An
kara
, Tür
kiye
Inte
rnat
iona
l Sch
ool o
n T
rigge
r an
d D
ata
Acq
uisi
tion,
1-7
Feb
rua
ry 2
010,
An
kara
, Tür
kiye
Pic
oB
oar
d &
Sc
ratc
hP
ico
Bo
ard
& S
cra
tch
– Ö
zgür
Çob
ano
– Ö
zgür
Çob
ano ğ
luğlu
Inte
rnat
iona
l Sch
ool o
n T
rigge
r an
d D
ata
Acq
uisi
tion,
1-7
Feb
rua
ry 2
010,
An
kara
, Tür
kiye
Inte
rnat
iona
l Sch
ool o
n T
rigge
r an
d D
ata
Acq
uisi
tion,
1-7
Feb
rua
ry 2
010,
An
kara
, Tür
kiye
Pic
oB
oar
d &
Sc
ratc
hP
ico
Bo
ard
& S
cra
tch
– Ö
zgür
Çob
ano
– Ö
zgür
Çob
ano ğ
luğlu
Scratch & PicoBoardScratch & PicoBoardA brief introductionA brief introduction
Scratch (SW) (SW)The environmentGraphical programing languageEx. calculation of π via random numbers
PicoBoard (HW) (HW)A simplified DAQ system
Joining the forces (SW + HW) (SW + HW)Combining Scratch with PicoBoardEx. start/stop the previous example by clapping hands
Scratch & PicoBoardScratch & PicoBoardJoint forces... Joint forces...
Inte
rnat
iona
l Sch
ool o
n T
rigge
r an
d D
ata
Acq
uisi
tion,
1-7
Feb
rua
ry 2
010,
An
kara
, Tür
kiye
Inte
rnat
iona
l Sch
ool o
n T
rigge
r an
d D
ata
Acq
uisi
tion,
1-7
Feb
rua
ry 2
010,
An
kara
, Tür
kiye
Pic
oB
oar
d &
Sc
ratc
hP
ico
Bo
ard
& S
cra
tch
– Ö
zgür
Çob
ano
– Ö
zgür
Çob
ano ğ
luğlu
The Scratch Sensor Board (aka PicoBoard) allows allows inputs from real-world sensorsinputs from real-world sensors such as switchesswitches, sliderssliders, lightlight, or soundsound to control Scratch programscontrol Scratch programsas follows:
Reads a booleanboolean from the switchswitch
Reads a realreal from the light sensorlight sensor between 0 and 1000 and 100
Reads a realreal from the microphonemicrophone between 0 and 1000 and 100
Reads a realreal from the potentiometerpotentiometer between 0 and 1000 and 100
USB cable
Clap-Hand Start/StopClap-Hand Start/StopControlling theControlling theππ finder loopfinder loop
We would like to start start and stopstop the ππ finder loop by claps claps of our hands hands
Reading the “loudnessloudness” amount to toggletoggle the valuevalue of an additional control variablecontrol variable is needed (?)
Different loop type
Togglinglogic
Additional control variable
Inte
rnat
iona
l Sch
ool o
n T
rigge
r an
d D
ata
Acq
uisi
tion,
1-7
Feb
rua
ry 2
010,
An
kara
, Tür
kiye
Inte
rnat
iona
l Sch
ool o
n T
rigge
r an
d D
ata
Acq
uisi
tion,
1-7
Feb
rua
ry 2
010,
An
kara
, Tür
kiye
Pic
oB
oar
d &
Sc
ratc
hP
ico
Bo
ard
& S
cra
tch
– Ö
zgür
Çob
ano
– Ö
zgür
Çob
ano ğ
luğlu
Inte
rnat
iona
l Sch
ool o
n T
rigge
r an
d D
ata
Acq
uisi
tion,
1-7
Feb
rua
ry 2
010,
An
kara
, Tür
kiye
Inte
rnat
iona
l Sch
ool o
n T
rigge
r an
d D
ata
Acq
uisi
tion,
1-7
Feb
rua
ry 2
010,
An
kara
, Tür
kiye
Pic
oB
oar
d &
Sc
ratc
hP
ico
Bo
ard
& S
cra
tch
– Ö
zgür
Çob
ano
– Ö
zgür
Çob
ano ğ
luğlu
Scratch & PicoBoardScratch & PicoBoardA brief introductionA brief introduction
Scratch (SW) (SW)The environmentGraphical programing languageEx. calculation of π via random numbers
PicoBoard (HW)(HW)A simplified DAQ system
Joining the forces (SW + HW) (SW + HW)Combining Scratch with PicoBoardEx. start/stop the previous example by clapping hands
40
A Brief Programming OverviewA Brief Programming OverviewBasic algorithmic thinking Basic algorithmic thinking (within C/C++)(within C/C++)
TargetWhat does a developer wantwant ?What does a developer dodo ?
SequentialSequential programingPhone-book application
Orienting the objectsOrienting the objectsSimplicity beneath complexityRectangle and Triangle derived from Polygon
ArrowArrow class derived from LineLine class Line class implementationArrow class implementationUsageUsage within user codeUsageUsage within user code as pointeras pointer
Code, Heap, StackMemoryMemory regions applications use
What do we mean by “thinking” ?Flow Flow charts and UML UML diagrams
Fundamental programing phrasesLoopsLoops & conditionalsconditionalsApproximating π by random numbersrandom numbersπ approximator implementations in different languages with flow chart
Compilation and usage of librarieslibraries Compiling line and arrow classes into libLineAndArrow.so shared libraryshared library
Inte
rnati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eIn
tern
ati
onal S
chool on
Tri
gger
an
d D
ata
Acq
uis
itio
n, 1
-7 F
eb
ruary
201
0, A
nka
ra, Tü
rkiy
eA
Bri
ef
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
A B
rief
Pro
gra
mm
ing
Overv
iew
– Ö
zgü
r Ç
ob
anoğlu
Graphical programingDrag'n'drop commandsDrag'n'drop commands in ScratchGet input from a dedicated HW - PicoBoard
Bonus TrackBonus Track