Beyond the Mouse A Short Course on Programming



Similar documents
Moving from CS 61A Scheme to CS 61B Java

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

The programming language C. sws1 1

Sources: On the Web: Slides will be available on:

Introduction to Python

The C Programming Language course syllabus associate level

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

CS 106 Introduction to Computer Science I

Informatica e Sistemi in Tempo Reale

Introduction to Python

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Computer Programming. Course Details An Introduction to Computational Tools. Prof. Mauro Gaspari:

Chapter 1 Java Program Design and Development

6.170 Tutorial 3 - Ruby Basics

1001ICT Introduction To Programming Lecture Notes

FEEG Applied Programming 5 - Tutorial Session

Install Java Development Kit (JDK) 1.8

PL / SQL Basics. Chapter 3

CE 504 Computational Hydrology Computational Environments and Tools Fritz R. Fiedler

#820 Computer Programming 1A

Introduction to Python

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

CSE 373: Data Structure & Algorithms Lecture 25: Programming Languages. Nicki Dell Spring 2014

Visual Basic Programming. An Introduction

Chapter 1. Dr. Chris Irwin Davis Phone: (972) Office: ECSS CS-4337 Organization of Programming Languages

Computer Programming Tutorial

Introduction to Object-Oriented Programming

Java Crash Course Part I

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

C++ INTERVIEW QUESTIONS

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

Programming Languages CIS 443

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

Introduction to Java

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

C++ Programming Language

Language Evaluation Criteria. Evaluation Criteria: Readability. Evaluation Criteria: Writability. ICOM 4036 Programming Languages

Introduction to programming

Computer Programming I

Java Programming Fundamentals

Lecture 5: Java Fundamentals III

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

Some Scanner Class Methods

Computer Programming I & II*

Semantic Analysis: Types and Type Checking

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Appendix: Tutorial Introduction to MATLAB

Tutorial on C Language Programming

Chapter 5 Names, Bindings, Type Checking, and Scopes

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

Crash Course in Java

Outline. hardware components programming environments. installing Python executing Python code. decimal and binary notations running Sage

CS 241 Data Organization Coding Standards

Computer Programming I

How To Write Portable Programs In C

Intro to scientific programming (with Python) Pietro Berkes, Brandeis University

Chapter 12 Programming Concepts and Languages

Programming Languages

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

PGR Computing Programming Skills

[1] Learned how to set up our computer for scripting with python et al. [3] Solved a simple data logistics problem using natural language/pseudocode.

Programming Languages & Tools

Programming in Java Course Technology, a part of Cengage Learning.

Java programming for C/C++ developers

Variables, Constants, and Data Types

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

1/20/2016 INTRODUCTION

Introduction to Data Structures

Programming and Data Structures with Java and JUnit. Rick Mercer

Object Oriented Software Design II

FOPR-I1O23 - Fundamentals of Programming

5 Arrays and Pointers

7 Why Use Perl for CGI?

Introduction to formal semantics -

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Programming Project 1: Lexical Analyzer (Scanner)

WESTMORELAND COUNTY PUBLIC SCHOOLS Integrated Instructional Pacing Guide and Checklist Computer Math

Java CPD (I) Frans Coenen Department of Computer Science

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

Semester Review. CSC 301, Fall 2015

VHDL Test Bench Tutorial

CSCE 110 Programming I Basics of Python: Variables, Expressions, and Input/Output

The Elective Part of the NSS ICT Curriculum D. Software Development

Advanced Bash Scripting. Joshua Malone

Transcription:

1 / 22 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)

Today s schedule... 2 / 22 1 How does (computer) programming work? 2 Variables and Datatypes

Today s schedule 3 / 22 1 How does (computer) programming work? 2 Variables and Datatypes

How does (computer) programming work? 4 / 22 Well, fist we should clearify terminology here! What is a programming language? What is a program?

5 / 22 Alright, what is it then? Definitions (broad sense) A programming language is an unambiguous artificial language that is made up of a set of symbols (vocabulary) and grammatical rules (syntax) to instruct a machine. A program 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 / 22 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)

6 / 22 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++).

Now, how does programming work? 7 / 22

Now, how does programming work? 7 / 22 1 open text editor (vi, notepad,..., not MS Word!) http://www.xkcd.com/378/

Now, how does programming work? 7 / 22 1 open text 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

Now, how does programming work? 7 / 22 1 open text 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)

Now, how does programming work? 7 / 22 1 open text 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)

Now, how does programming work? 7 / 22 1 open text 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!)

Now, how does programming work? 7 / 22 1 open text 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)

Don t even think that s as simple as it sounds... 8 / 22 Hello World in Matlab 1 >> dsp ( halo orld??? dsp ( halo o r l d 3 Error : Unexpected MATLAB expression. 5 >> dsp ( halo o r l d 7??? dsp ( halo o r l d 9 Error : A MATLAB s t r i n g constant i s not terminated properly. 11 >> dsp ( halo o r l d??? dsp ( halo o r l d 13 Error : Expression or statement i s incorrect possibly unbalanced (, {, or [. 15 >> dsp ( halo o r l d ) 17??? Undefined function or method dsp for input arguments of type char. 19 >> disp ( halo o r l d ) halo o r l d 21 % Sematically correct, i f you want to say hi to the world : 23 % >> disp ( h e l l o world ) 25 h e l l o world Listing 2.1: hello_world.log

Today s schedule 9 / 22 1 How does (computer) programming work? 2 Variables and Datatypes

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 s memory (runtime). The concept of a variable consists of: 10 / 22

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 s memory (runtime). The concept of a variable consists of: name type value 10 / 22

Memory interlude Depth, http://xkcd.com/485 (September 16, 2009) 11 / 22

Memory interlude Depth, http://xkcd.com/485 (September 16, 2009) 11 / 22

Variables (2) name 12 / 22 USE MEANINGFUL NAMES!

12 / 22 Variables (2) name USE MEANINGFUL NAMES! valid, follow programming language rules MATLAB variable names must begin with a letter, followed by any combination of letters, digits, and underscores. MATLAB distinguishes between uppercase and lowercase. No reserved keywords!

12 / 22 Variables (2) name USE MEANINGFUL NAMES! valid, follow programming language rules MATLAB variable names must begin 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:

12 / 22 Variables (2) name USE MEANINGFUL NAMES! valid, follow programming language rules MATLAB variable names must begin 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

12 / 22 Variables (2) name USE MEANINGFUL NAMES! valid, follow programming language rules MATLAB variable names must begin 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

12 / 22 Variables (2) name USE MEANINGFUL NAMES! valid, follow programming language rules MATLAB variable names must begin 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)!

Variables (3) type 13 / 22 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

13 / 22 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

13 / 22 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.)

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) 14 / 22

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 14 / 22

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; 14 / 22

15 / 22 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 a list of values linked to a chunk of memory (a sequence of boxes) access by index number MATLAB treats everything as a matrix Shells allow only vectors.

Memory interlude (2) Depth, http://xkcd.com/485 (September 16, 2009) 16 / 22

Memory interlude (2) Depth, http://xkcd.com/485 (September 16, 2009) 16 / 22

Memory interlude (2) Depth, http://xkcd.com/485 (September 16, 2009) 16 / 22

Memory interlude (2) Depth, http://xkcd.com/485 (September 16, 2009) 16 / 22

Advanced Variables: Vectors and Matrices (2) 17 / 22 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

Advanced Variables: Vectors and Matrices (2) 17 / 22 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!!!!

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 18 / 22

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 19 / 22

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 20 / 22

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 21 / 22

Example: Equinox marathon pacing table in Matlab 1 % UAF/ GI Beyond the mouse, f a l l 2010, Ronni Grapenthin % EXAMPLE: 2D matrix ( Table ), prints l i s t of times that can be used for optimal 3 % Equinox 2011 p r e p a r a t i o n % parameter : miles miles you ve run 5 function pace_table = pacing_table ( miles ) 7 % Set up pacing table : Give miles as numbers and times as strings ( requires a c e l l array, 9 % hence the curly braces ) pace_table = { 1 0:05:55 0:08:42 0:10:55 ; 11 5 0:30:01 0:44:06 0:55:21 ; 10 0:59:56 1:28:01 1:50:29 ; 13 15 1:35:01 2:19:33 2:55:05 ; 20 2:04:59 3:03:34 3:50:26 ; 15 26.2 2:40:00 3:55:00 4:55:00 } ; 17 % Since I m lazy and didn t want to type a l l the miles, a mile does not equal the index, 19 % hence we l l have to do some math. Index is rounded number of miles divided by 5. Since % Matlab indices s t a r t at 1, we have to add a 1. Otherwise everything smaller than 2.5 miles 21 % would r e s u l t i n an e r r o r i d x = round ( miles / 5 ) + 1 ; 23 % lame output 25 pace_table ( idx, : ) pause 27 % fancy output : 29 disp ( ) ; disp ( miles record well trained mildly trained ) ; 31 disp ( ) ; disp ( pace_table ( idx, : ) ) ; 33 end Listing 2.2: pacing_table.m 22 / 22