• No results found

Beyond the Mouse A Short Course on Programming

N/A
N/A
Protected

Academic year: 2022

Share "Beyond the Mouse A Short Course on Programming"

Copied!
44
0
0

Loading.... (view fulltext now)

Full text

(1)

Beyond the Mouse – A Short Course on

Programming

2. Fundamental Programming Principles I:

Variables and Data Types

Ronni Grapenthin

Geophysical Institute, University of Alaska Fairbanks

September 19, 2011

“The Uncomfortable Truths Well”, http://xkcd.com/568 (April 13, 2009)

(2)

Today’s schedule . . .

1 How does (computer) programming work?

2 Variables and Datatypes

(3)

Today’s schedule

1 How does (computer) programming work?

2 Variables and Datatypes

(4)

How does (computer) programming work?

Well, fist we should clearify terminology here!

What is a programming language?

What is a program?

(5)

Alright, what is it then?

Definitions (broad sense)

Aprogramming language is an unambiguous artificial language that is made up of a set of symbols (vocabulary) and grammatical rules (syntax) to instruct a machine.

Aprogram is a set of instructions in one or multiple programming languages that specifies the behavior of a machine.

Compilation or interpretation is the verification of a program and its translation into machine readable instructions of a specific platform.

(6)

Programming languages . . . continued

Two broad families can be identified:

1 Interpreted languages

An interpreter program is necessary to take in commands, check syntax and translate to machine language at runtime (e.g., Matlab, Unix Shell)

2 Compiled languages

Programs are translated and saved in machine language. At runtime no additional program is necessary (e.g., C/C++).

(7)

Programming languages . . . continued

Two broad families can be identified:

1 Interpreted languages

An interpreter program is necessary to take in commands, check syntax and translate to machine language at runtime (e.g., Matlab, Unix Shell)

2 Compiled languages

Programs are translated and saved in machine language. At runtime no additional program is necessary (e.g., C/C++).

(8)

Now, how does programming work?

1 opentext editor (vi, notepad, . . . , not MS Word!)

http://www.xkcd.com/378/ 2 translate your (mental) flowchart into a set of instructions

according to the rules of an applicable programming language

3 test your program for syntactical correctness (ask interpreter/compiler)

4 if errors, fix them and go back to (3)

5 test your program for semantical errors (the fun part!)

6 if errors, fix them and go back to (3)

(9)

Now, how does programming work?

1 opentext editor (vi, notepad, . . . , not MS Word!)

http://www.xkcd.com/378/

2 translate your (mental) flowchart into a set of instructions according to the rules of an applicable programming language

3 test your program for syntactical correctness (ask interpreter/compiler)

4 if errors, fix them and go back to (3)

5 test your program for semantical errors (the fun part!)

6 if errors, fix them and go back to (3)

(10)

Now, how does programming work?

1 opentext editor (vi, notepad, . . . , not MS Word!)

http://www.xkcd.com/378/

2 translate your (mental) flowchart into a set of instructions according to the rules of an applicable programming language

3 test your program for syntactical correctness (ask interpreter/compiler)

4 if errors, fix them and go back to (3)

5 test your program for semantical errors (the fun part!)

6 if errors, fix them and go back to (3)

(11)

Now, how does programming work?

1 opentext editor (vi, notepad, . . . , not MS Word!)

http://www.xkcd.com/378/

2 translate your (mental) flowchart into a set of instructions according to the rules of an applicable programming language

3 test your program for syntactical correctness (ask interpreter/compiler)

4 if errors, fix them and go back to (3)

5 test your program for semantical errors (the fun part!)

6 if errors, fix them and go back to (3)

(12)

Now, how does programming work?

1 opentext editor (vi, notepad, . . . , not MS Word!)

http://www.xkcd.com/378/

2 translate your (mental) flowchart into a set of instructions according to the rules of an applicable programming language

3 test your program for syntactical correctness (ask interpreter/compiler)

4 if errors, fix them and go back to (3)

5 test your program for semantical errors (the fun part!)

6 if errors, fix them and go back to (3)

(13)

Now, how does programming work?

1 opentext editor (vi, notepad, . . . , not MS Word!)

http://www.xkcd.com/378/

2 translate your (mental) flowchart into a set of instructions according to the rules of an applicable programming language

3 test your program for syntactical correctness (ask interpreter/compiler)

4 if errors, fix them and go back to (3)

5 test your program for semantical errors (the fun part!)

6 if errors, fix them and go back to (3)

(14)

Now, how does programming work?

1 opentext editor (vi, notepad, . . . , not MS Word!)

http://www.xkcd.com/378/

2 translate your (mental) flowchart into a set of instructions according to the rules of an applicable programming language

3 test your program for syntactical correctness (ask interpreter/compiler)

4 if errors, fix them and go back to (3)

5 test your program for semantical errors (the fun part!)

6 if errors, fix them and go back to (3)

(15)

Don’t even think that’s as simple as it sounds . . .

‘Hello World’ in Matlab

1 >> dsp ( h a l o o r l d

??? dsp ( h a l o o r l d

3 |

E r r o r : Unexpected MATLAB e x p r e s s i o n . 5

>> dsp ( ’ h a l o o r l d 7 ??? dsp ( ’ h a l o o r l d

|

9 E r r o r : A MATLAB s t r i n g c o n s t a n t i s n o t t e r m i n a t e d p r o p e r l y .

11 >> dsp ( ’ h a l o o r l d ’

??? dsp ( ’ h a l o o r l d ’

13 |

E r r o r : E x p r e s s i o n o r s t a t e m e n t i s i n c o r r e c t −−p o s s i b l y unbalanced ( , { , o r [ . 15

>> dsp ( ’ h a l o o r l d ’ )

17 ??? Undefined f u n c t i o n o r method ’ dsp ’ f o r input arguments o f type ’ char ’ .

19 >> disp ( ’ h a l o o r l d ’ ) h a l o o r l d

21

% S e m a t i c a l l y c o r r e c t , i f you want t o say ’ h i ’ t o t h e w o r l d : 23 %

>> disp ( ’ h e l l o w o r l d ’ ) 25 h e l l o w o r l d

Listing 2.1: hello_world.log

(16)

Today’s schedule

1 How does (computer) programming work?

2 Variables and Datatypes

(17)

Variables (1)

Definitions – a selection

Donald Knuth: A quantity that may possess different values as a program is being executed.

Mehran Sahami: A box in which we stuff things – i.e. a box with variable content.

Wikipedia: User defined keyword that is linked to a value stored in computer’smemory (runtime).

The concept of avariable consists of:

name type value

(18)

Variables (1)

Definitions – a selection

Donald Knuth: A quantity that may possess different values as a program is being executed.

Mehran Sahami: A box in which we stuff things – i.e. a box with variable content.

Wikipedia: User defined keyword that is linked to a value stored in computer’smemory (runtime).

The concept of avariable consists of:

name type value

(19)

Memory interlude

“Depth”, http://xkcd.com/485 (September 16, 2009)

(20)

Memory interlude

“Depth”, http://xkcd.com/485 (September 16, 2009)

(21)

Variables (2) – name

USE MEANINGFUL NAMES!

valid, follow programming language rules – MATLAB variable names mustbegin with a letter, followed by any combination of letters, digits, and underscores. MATLAB distinguishes

between uppercase and lowercase.No reserved keywords! USE MEANINGFUL NAMES, i.e. names that speak:

‘lengthGlacier’ or ‘glacier_length’NOT NOT NOT ‘a’ – avoid ambiguity

use consistent formatting, i.e.: ‘my_cool_var’ vs. ‘myCoolVar’ – supports reading

a gazillion style guides exist – punchline: use meaningful names, be consistent (that’s hard enough)!

(22)

Variables (2) – name

USE MEANINGFUL NAMES!

valid, follow programming language rules – MATLAB variable names mustbegin with a letter, followed by any combination of letters, digits, and underscores. MATLAB distinguishes

between uppercase and lowercase.No reserved keywords!

USE MEANINGFUL NAMES, i.e. names that speak:

‘lengthGlacier’ or ‘glacier_length’NOT NOT NOT ‘a’ – avoid ambiguity

use consistent formatting, i.e.: ‘my_cool_var’ vs. ‘myCoolVar’ – supports reading

a gazillion style guides exist – punchline: use meaningful names, be consistent (that’s hard enough)!

(23)

Variables (2) – name

USE MEANINGFUL NAMES!

valid, follow programming language rules – MATLAB variable names mustbegin with a letter, followed by any combination of letters, digits, and underscores. MATLAB distinguishes

between uppercase and lowercase.No reserved keywords!

USE MEANINGFUL NAMES, i.e. names that speak:

‘lengthGlacier’ or ‘glacier_length’NOT NOT NOT ‘a’ – avoid ambiguity

use consistent formatting, i.e.: ‘my_cool_var’ vs. ‘myCoolVar’ – supports reading

a gazillion style guides exist – punchline: use meaningful names, be consistent (that’s hard enough)!

(24)

Variables (2) – name

USE MEANINGFUL NAMES!

valid, follow programming language rules – MATLAB variable names mustbegin with a letter, followed by any combination of letters, digits, and underscores. MATLAB distinguishes

between uppercase and lowercase.No reserved keywords!

USE MEANINGFUL NAMES, i.e. names that speak:

‘lengthGlacier’ or ‘glacier_length’NOT NOT NOT ‘a’ – avoid ambiguity

use consistent formatting, i.e.: ‘my_cool_var’ vs. ‘myCoolVar’ – supports reading

a gazillion style guides exist – punchline: use meaningful names, be consistent (that’s hard enough)!

(25)

Variables (2) – name

USE MEANINGFUL NAMES!

valid, follow programming language rules – MATLAB variable names mustbegin with a letter, followed by any combination of letters, digits, and underscores. MATLAB distinguishes

between uppercase and lowercase.No reserved keywords!

USE MEANINGFUL NAMES, i.e. names that speak:

‘lengthGlacier’ or ‘glacier_length’NOT NOT NOT ‘a’ – avoid ambiguity

use consistent formatting, i.e.: ‘my_cool_var’ vs. ‘myCoolVar’ – supports reading

a gazillion style guides exist – punchline: use meaningful names, be consistent (that’s hard enough)!

(26)

Variables (2) – name

USE MEANINGFUL NAMES!

valid, follow programming language rules – MATLAB variable names mustbegin with a letter, followed by any combination of letters, digits, and underscores. MATLAB distinguishes

between uppercase and lowercase.No reserved keywords!

USE MEANINGFUL NAMES, i.e. names that speak:

‘lengthGlacier’ or ‘glacier_length’NOT NOT NOT ‘a’ – avoid ambiguity

use consistent formatting, i.e.: ‘my_cool_var’ vs. ‘myCoolVar’ – supports reading

a gazillion style guides exist – punchline: use meaningful names, be consistent (that’s hard enough)!

(27)

Variables (3) – type

What is a type? – Think of sets of numbers in math: N, R, Z, . . . The type refers to how numbers are being represented in a computer’s memory, i.e. which bit has which meaning, and how many bits are necessary

Two kinds of Types

primitive, built in types – for MATLAB e.g.: ‘int32’, ‘double’,

‘boolean’ (important for *printf functions)

complex, home made types – (arrays,) structs, cell arrays (Matlab), classes

Types in Programming Languages

some languages, e.g. MATLAB, Shells, Perl are weakly typed: implicit type conversions (OR one type can be treated as another) this is nice at first, occasionally this leads to nasty/hard to fix problems (e.g. string interpreted as number, etc.)

(28)

Variables (3) – type

What is a type? – Think of sets of numbers in math: N, R, Z, . . . The type refers to how numbers are being represented in a computer’s memory, i.e. which bit has which meaning, and how many bits are necessary

Two kinds of Types

primitive, built in types – for MATLAB e.g.: ‘int32’, ‘double’,

‘boolean’ (important for *printf functions)

complex, home made types – (arrays,) structs, cell arrays (Matlab), classes

Types in Programming Languages

some languages, e.g. MATLAB, Shells, Perl are weakly typed: implicit type conversions (OR one type can be treated as another) this is nice at first, occasionally this leads to nasty/hard to fix problems (e.g. string interpreted as number, etc.)

(29)

Variables (3) – type

What is a type? – Think of sets of numbers in math: N, R, Z, . . . The type refers to how numbers are being represented in a computer’s memory, i.e. which bit has which meaning, and how many bits are necessary

Two kinds of Types

primitive, built in types – for MATLAB e.g.: ‘int32’, ‘double’,

‘boolean’ (important for *printf functions)

complex, home made types – (arrays,) structs, cell arrays (Matlab), classes

Types in Programming Languages

some languages, e.g. MATLAB, Shells, Perl are weakly typed:

implicit type conversions (OR one type can be treated as another) this is nice at first, occasionally this leads to nasty/hard to fix problems (e.g. string interpreted as number, etc.)

(30)

Variables (4) – value

Value

a value of the type of the variable: 23, 3.1415926..., false i.e., the thing we stuff in the box

can/should change during the runtime of the program, otherwise use a constant (if possible)

Declaring a variable and Assigning a value:

In General: (type) name = value; or (type) name = expression;

Matlab: myNewVar = 10; TC-Shell (differs) set myNewVar = 10; Access to the values (de-referencing):

Matlab: use myNewVar; TC-Shell (differs) use ‘$’: $myNewVar What’s that?

myNewVar = myNewVar + 1;

(31)

Variables (4) – value

Value

a value of the type of the variable: 23, 3.1415926..., false i.e., the thing we stuff in the box

can/should change during the runtime of the program, otherwise use a constant (if possible)

Declaring a variable and Assigning a value:

In General: (type) name = value; or (type) name = expression;

Matlab: myNewVar = 10; TC-Shell (differs) set myNewVar = 10;

Access to the values (de-referencing):

Matlab: use myNewVar; TC-Shell (differs) use ‘$’: $myNewVar

What’s that?

myNewVar = myNewVar + 1;

(32)

Variables (4) – value

Value

a value of the type of the variable: 23, 3.1415926..., false i.e., the thing we stuff in the box

can/should change during the runtime of the program, otherwise use a constant (if possible)

Declaring a variable and Assigning a value:

In General: (type) name = value; or (type) name = expression;

Matlab: myNewVar = 10; TC-Shell (differs) set myNewVar = 10;

Access to the values (de-referencing):

Matlab: use myNewVar; TC-Shell (differs) use ‘$’: $myNewVar What’s that?

myNewVar = myNewVar + 1;

(33)

Advanced Variables: Vectors and Matrices (1)

Array variables

are lists, vectors, matrices of data (1 to n dimensional – book keeping can become a hassle)

therefore instead of one value they hold alist of values linked to a chunk of memory (a sequence of boxes) access by index number

MATLAB treatseverything as a matrix Shells allow only vectors.

(34)

Memory interlude (2)

“Depth”, http://xkcd.com/485 (September 16, 2009)

(35)

Memory interlude (2)

“Depth”, http://xkcd.com/485 (September 16, 2009)

(36)

Memory interlude (2)

“Depth”, http://xkcd.com/485 (September 16, 2009)

(37)

Memory interlude (2)

“Depth”, http://xkcd.com/485 (September 16, 2009)

(38)

Advanced Variables: Vectors and Matrices (2)

Example: Numeric Vector

index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

vector: 12 23.3 23.3 nan nan 1 42 42.1 23 5 nan nan 0 0 0

Example: String

index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

sting: h e l l o w o r l d ! ! ! !

(39)

Advanced Variables: Vectors and Matrices (2)

Example: Numeric Vector

index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

vector: 12 23.3 23.3 nan nan 1 42 42.1 23 5 nan nan 0 0 0

Example: String

index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

sting: h e l l o w o r l d ! ! ! !

(40)

Advanced Variables: Vectors and Matrices (3)

Setting up a numeric Matrix: Equinox marathon pacing tables index Mile

1 1

2 5

3 10

4 15

5 20

6 25

7 26.2

(41)

Advanced Variables: Vectors and Matrices (3)

Setting up a numeric Matrix: Equinox marathon pacing tables index Mile record

1 1 0:05:55

2 5 0:30:01

3 10 0:59:56

4 15 1:35:01

5 20 2:04:59

6 25 2:32:19

7 26.2 2:40:00

(42)

Advanced Variables: Vectors and Matrices (3)

Setting up a numeric Matrix: Equinox marathon pacing tables index Mile record well trained

1 1 0:05:55 0:08:42

2 5 0:30:01 0:44:06

3 10 0:59:56 1:28:01

4 15 1:35:01 2:19:33

5 20 2:04:59 3:03:34

6 25 2:32:19 3:43:43

7 26.2 2:40:00 3:55:00

(43)

Advanced Variables: Vectors and Matrices (3)

Setting up a numeric Matrix: Equinox marathon pacing tables index Mile record well trained mildly trained

1 1 0:05:55 0:08:42 0:10:55

2 5 0:30:01 0:44:06 0:55:21

3 10 0:59:56 1:28:01 1:50:29

4 15 1:35:01 2:19:33 2:55:05

5 20 2:04:59 3:03:34 3:50:26

6 25 2:32:19 3:43:43 4:40:50

7 26.2 2:40:00 3:55:00 4:55:00

(44)

Example: Equinox marathon pacing table in Matlab

1 % UAF / GI Beyond t h e mouse , f a l l 2010 , Ronni G r a p e n t h i n

% EXAMPLE : 2D m a t r i x ( Table ) , p r i n t s l i s t o f t i m e s t h a t can be used f o r o p t i m a l 3 % Equinox 2011 p r e p a r a t i o n

% parameter : m i l e s −− m i l e s you ’ ve run 5

f u n c t i o n p a c e _ t a b l e = p a c i n g _ t a b l e ( m i l e s ) 7

% Set up p a c i n g t a b l e : Give m i l e s as numbers and t i m e s as s t r i n g s ( r e q u i r e s a c e l l a r r a y , 9 % hence t h e c u r l y braces )

p a c e _ t a b l e = { 1 ’ 0 : 0 5 : 5 5 ’ ’ 0 : 0 8 : 4 2 ’ ’ 0 : 1 0 : 5 5 ’ ; 11 5 ’ 0 : 3 0 : 0 1 ’ ’ 0 : 4 4 : 0 6 ’ ’ 0 : 5 5 : 2 1 ’ ; 10 ’ 0 : 5 9 : 5 6 ’ ’ 1 : 2 8 : 0 1 ’ ’ 1 : 5 0 : 2 9 ’ ; 13 15 ’ 1 : 3 5 : 0 1 ’ ’ 2 : 1 9 : 3 3 ’ ’ 2 : 5 5 : 0 5 ’ ; 20 ’ 2 : 0 4 : 5 9 ’ ’ 3 : 0 3 : 3 4 ’ ’ 3 : 5 0 : 2 6 ’ ; 15 26.2 ’ 2 : 4 0 : 0 0 ’ ’ 3 : 5 5 : 0 0 ’ ’ 4 : 5 5 : 0 0 ’ } ;

17

% Since I ’m l a z y and didn ’ t want t o t y p e a l l t h e m i l e s , a m i l e does n o t e qua l t h e index , 19 % hence we ’ l l have t o do some math . In dex i s rounded number o f m i l e s d i v i d e d by 5 . Since

% Matlab i n d i c e s s t a r t a t 1 , we have t o add a 1 . Otherwise e v e r y t h i n g s m a l l e r than 2 . 5 m i l e s 21 % would r e s u l t i n an e r r o r

i d x = round ( m i l e s / 5 ) + 1 ; 23

% lame o u t p u t 25 p a c e _ t a b l e ( i d x , : )

pause 27

% f a n c y o u t p u t : 29 disp ( ’ ’ ) ;

disp ( ’ m i l e s r e c o r d w e l l t r a i n e d m i l d l y t r a i n e d ’ ) ; 31 disp ( ’ −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−’ ) ;

disp ( p a c e _ t a b l e ( i d x , : ) ) ;

33 end Listing 2.2: pacing_table.m

References

Related documents

Community Discourse Organizational Learning About Organizational Learning by Doing Know-what Know-why Know-how Know-what Know-why Know-how 1 5 6 2 4 3 Knowledge flows addressed by

The author also consulted the official documents of the United Nations Economic Commission for Africa, specifically those of the Sub-Regional Office for West Africa for 2012,

• Cost Estimating: Society of Cost Estimating and Analysis; Cost and Software Data Reporting (CSDR) Focus Group and Space Systems Cost Analysis

In one case we learned of a program whose government coun- terparts were active participants in the various software sprint and release reviews and acting as product owners, while

Aplikacija ponuja naloge, kot so sestavljanje besed za popolne zaˇ cetnike, kjer je poleg slike prisoten tudi pravilen zapis besede, sestavljanje besed samo iz ˇ crk be- sede,

• Construction – partition walls, panelling & cladding DIAB’s extensive composite know-how places us in a perfect position to supply a range of off-the-shelf composite

• Hard, dense coatings with good abrasion, erosion and sliding wear resistance • Smooth coatings with fine microstructure and high bond strengths • Low oxidation and

Samuels, Amy Jo; Samuels, Gregory L.; and Haas, Brandon (2017) "The Revolution Will Be Live: Examining Educational (In)Justice through the Lens of Black Lives Matter,"