Benfeard Williams June 16th, 2016

Slides:



Advertisements
Similar presentations
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Advertisements

CS 100: Roadmap to Computing Fall 2014 Lecture 0.
Programming in Visual Basic
Lecture 2 Introduction to C Programming
Introduction to C Programming
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Chapter 2: Algorithm Discovery and Design
Program Design and Development
Chapter 2 The Algorithmic Foundations of Computer Science
Chapter 2: Algorithm Discovery and Design
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Introducing Java.
CIS Computer Programming Logic
1. Reference  2  Algorithm :- Outline the essence of a computational procedure, step by step instructions.  Program :- an.
Introduction to Python
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Invitation to Computer Science, Java Version, Second Edition.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Introduction to Computational Linguistics Programming I.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
How to Read Code Benfeard Williams 6/11/2015 Susie’s lecture notes are in the presenter’s notes, below the slides Disclaimer: Susie may have made errors.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
Getting Started With Python Brendan Routledge
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Algorithms and Pseudocode CS Principles Lesson Developed for CS4 Alabama Project Jim Morse.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 10 Programming Fundamentals with JavaScript
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2017
Introduction to Python
Chapter 2 - Introduction to C Programming
GC211Data Structure Lecture2 Sara Alhajjam.
Debugging and Random Numbers
Primitive Data, Variables, Loops (Maybe)
The Selection Structure
Variables, Expressions, and IO
Introduction To Flowcharting
Functions CIS 40 – Introduction to Programming in Python
Chapter 2 - Introduction to C Programming
CS 240 – Lecture 11 Pseudocode.
Lecture 4B More Repetition Richard Gesick
Perl for Bioinformatics
Computer Programming.
Arrays, For loop While loop Do while loop
Chapter 10 Programming Fundamentals with JavaScript
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
PHP.
CS 100: Roadmap to Computing
Algorithm Discovery and Design
Chapter 2 - Introduction to C Programming
Programming We have seen various examples of programming languages
Algorithm and Ambiguity
Computer Science Core Concepts
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Chapter 2 - Introduction to C Programming
Introduction to C Programming
CS 100: Roadmap to Computing
Class code for pythonroom.com cchsp2cs
Presentation transcript:

Benfeard Williams June 16th, 2016 How to Read Code Benfeard Williams June 16th, 2016 Learning the “grammar” of coding. Similar to a foreign language, reviewing the basics beneficial

Concepts You Will Learn Programming Skill, science, engineering, art, creativity Problem-solving How to solve problems using computer programming Impact of computer science Scale and automation Foundation for future work There are many different ways to use code to address a problem – learn a style and type that works best for you Use code as a tool to help you avoid manual labor that would take you much longer

Things to know whenever you are reading code Details to consider

Interpreted vs Compiled Code Read and executed by another program on the target machine Easy to implement Compiled Expressed specifically for the target machine Faster performance Important to know for sharing and running shared code. Python and R can be installed on a computer and used to run shared (interpreted) code. However, some programs have specified instructions for installation, indicating that the code behind that program are compiled for that machine

Computers Do What You Tell Them They are fast but not smart You need to plan exactly what the computer needs to do in order to solve a problem YOU need to know how to solve the problem in order for the computer to carry out that task for you. Even if you couldn’t in your lifetime perform a certain analysis, you can tell the computer exactly how to do it.

Declarative Knowledge Statements of fact “A good health care plan improves the quality of medical care while saving money” “y is the square root of x if and only if y*y = x” Aka, if we want to know if y is the square root of x, we need to “fulfill” this statement. Aka, statement must be true

Imperative Knowledge How to accomplish something (recipe) Start with a guess, g If g*g is close enough to x, then g is a good approximation of the square root of x Otherwise, create a new guess by averaging g and x/g. Using this new guess, go back to step 2 Get close Get closer (if g not close enough) Check

What will you see in code? What is in the code that you need to pay attention to in order to understand what it’s doing What will you see in code?

Building Blocks Variables Data types Operators Functions (subroutines) Name of a storage location Assign a value to a variable Data types Various classifications of data Operators Arithmetic: + - * / % ** Functions (subroutines) Sequence of instructions to perform a specific task Variable – where you are storing data in order to use later, with specific name (not a physical location)

Variables Names are unique and can be abstract What is http://205.251.242.103 What is http://www.amazon.com Declare a variable and assign a value Name and type Examples: int age = 18; age = 18 Example of assigning a name (age) to a piece of data (18) Can explicitly state data type (int age) or sometimes the computer will infer the type (age) – see next

Data Types Int (integer) Float (floating-point number) 4, 13, -7 Float (floating-point number) 3.33337 Work just like normal numbers ans = 2 * (7 + 4) – 1 ans = 21 Example: if we set some variable (ans) to a mathematical statement, the computer can run the math and set this as the variable

Data Types Char (characters) Str (string) Arithmetic manipulation “HELLO”, “Echo 123” Arithmetic manipulation phrase = “My name is ” + “Benfeard” phrase = “My name is Benfeard” Known that they are text because they are in quotes (so “3” can NOT be used as a number – has no numerical value) Can use either single or double quotes, but must be consistent Anything within the quotes are interpreted as a single unit (string) and can add these strings together Now will form a new string

Data Types Arrays/List Manipulation Indexing elements Collection of elements [ 1, 2, 3, 4, 5] [ ‘A’, ‘B’, ‘C’, ‘D’, ‘E’ ] Manipulation myArray = [1, 2, 3] myArray = myArray * 2 myArray = [1, 2, 3, 1, 2, 3] Indexing elements Want to have certain elements always together Careful with arithmetic manipulation, because might not act as you’d expect Whatever is on the RIGHT of the equals will be put together and the thing on the LEFT of the equals will have this value Computer knows the order of the things in you list, so you can use the index (list position) of each item to call it forward

Functions Allows you to easily recall a procedure or subroutine def sum(a, b): return a+b Parameters values in the call, that you pass to the function to use as input Output Return value Call Function answer = sum(7,3) answer = 10 Exact syntax will depend on the coding language Define a function (write the part of the code) Call a function (use this pre-written code) - need to “pass” it a set of parameters - like a mathematical function, need some input Computer will “return” a value Can then set a variable ot be the output of a certain function, if passed inputs

Functions Example function that returns a value def greet(name): print "Hello " + name Advantages Repeat code, call multiple times Flexible, call with different arguments Functions greet("Sue") Can pass the function a list of inputs to avoid typing the same thing repeteticly.

Booleans True or False Useful for comparisons Greater than, less than Is equal to, is not equal to Supports algebraic operations and, or, not A variable can have the VALUE of True or False Be careful using these words in code if not using as values Computer will understand value as true or false can then ask computer whether something is true or false “Looking at my set of data: is the concentration greater than 10 mM and color is yellow?”

If Statements and Loops If this statement is true, do something For loops For all values in an array, do something repeatedly While loops While a statement is true, do something repeatedly Way of asking a question to your computer – will evaluate data and make a decision for you Different types of loops for different reasons – use the best one to maximize the efficiency of your code

Understanding the code

Computers do what you tell them They are fast but not smart You need to plan exactly what the computer needs to do in order to solve a problem Outline the solution to your problem Pseudocoding (step by step walkthrough of code) The computer reads the code as written – syntax is important YOU NEED TO PSEUDOCOE to outline and understand the logic behind the problem solving Good way of double checking

Algorithms Describe in words how to solve a problem Like a recipe, must be detailed and precise How to make a Peanut Butter & Jelly Sandwich Ingredients: Two slices of bread Peanut Butter Jelly Spread peanut butter on one slice Spread jelly on the other slice Combine slices together Does this provide enough information for anyone to make the sandwich? Always ask yourself FIRST how to explain how to solve the problem in words Variables: ingredients Algorithm: instructions Broke code will often be missing a key logical part

How To Dissect Code Important information about function and usage Opening a romeo.txt file Counting something Looping through lines Example of ok code Need more descriptive title Need comments throughout Need better, more useful header (what program used, etc) Like how you can change the language of your Word document spell check This done in a text editor, which is why the code has colors, line numbers, and etc to help you read the code First variable: filename with the string value of “romeo.txt” fileToProcess with a value of the output of the function “open” on the variable “filename” Indentation at line 10 indicates that we are within some loop – can see that it is a for-loop If you don’t know part of a code, you can always Google part of the code, or certain terms, with the language of code you are using Aka google: strip() python == means “if equal to” So this will add 1 to the count, then restart the loop until the list of words is exhausted Splitting lines into words? Words of length 4? Increase counter Print count at the end

Computers Read In Order Because this code is out of logical order (the computer reads sequentially), the computer does not know what to do at line 9 because it does not recognized the non-declared variable fileToProcess

Important Resources Sakai webpage howtolearntocode.web.unc.edu HtLtC teachers Stackoverflow.com ITS Research Computing

Questions?