Comp 11 Lectures. Dr. Mike Shah. August 9, Tufts University. Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, / 34

Similar documents
Comp 11 Lectures. Mike Shah. July 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures July 26, / 45

Comp 11 Lectures. Mike Shah. July 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures July 26, / 40

Comp 11 Lectures. Mike Shah. July 12, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures July 12, / 33

Python & Numpy A tutorial

Lecture 3: Finite Automata

Lecture 3: Finite Automata

Comp 11 Lectures. Mike Shah. June 20, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures June 20, / 38

Outline. policies for the first part. with some potential answers... MCS 260 Lecture 10.0 Introduction to Computer Science Jan Verschelde, 9 July 2014

Announcements. Problem Set 6 due next Monday, February 25, at 12:50PM. Midterm graded, will be returned at end of lecture.

CS429: Computer Organization and Architecture

Lecture 4: Finite Automata

Unit 1 A.CED.1 One step Inequalities September 22, 2015

ECEN 651: Microprogrammed Control of Digital Systems Department of Electrical and Computer Engineering Texas A&M University

CS187 - Science Gateway Seminar for CS and Math

Turing Machines Part II

Students will read supported and shared informational materials, including social

Scripting Languages Fast development, extensible programs

Homework 2: MDPs and Search

Today s Menu. Administrativia Two Problems Cutting a Pizza Lighting Rooms

How to write maths (well)

Homework. Turing Machines. Announcements. Plan for today. Now our picture looks like. Languages

1 SIMPLE PENDULUM 1 L (1.1)

CSE370: Introduction to Digital Design

Lab 10: DC RC circuits

Theory of Computation. Theory of Computation

Python. chrysn

Lab 2 Worksheet. Problems. Problem 1: Geometry and Linear Equations

Theory of Computer Science

Selection and Adversary Arguments. COMP 215 Lecture 19

Theory of Computer Science. Theory of Computer Science. E1.1 Motivation. E1.2 How to Measure Runtime? E1.3 Decision Problems. E1.

Physics 351 Wednesday, January 10, 2018

Turing Machines Part II

Prealgebra. Edition 5

Parts 3-6 are EXAMPLES for cse634

CSC321 Lecture 9 Recurrent neural nets

SAMPLE. Read the information below and follow the instructions.

The P-vs-NP problem. Andrés E. Caicedo. September 10, 2011

- Why aren t there more quantum algorithms? - Quantum Programming Languages. By : Amanda Cieslak and Ahmana Tarin

Computer Science Introductory Course MSc - Introduction to Java

Material Covered on the Final

Tutorial Three: Loops and Conditionals

Fourth Grade News Flash

How to Make or Plot a Graph or Chart in Excel

Convolutional Coding LECTURE Overview

Deterministic Finite Automata

Announcements. Final exam review session tomorrow in Gates 104 from 2:15PM 4:15PM Final Friday Four Square of the quarter!

COMP424 Computer Security

10 - Celestron Telescope II: Operation

5th Grade. Slide 1 / 191. Slide 2 / 191. Slide 3 / 191. Algebraic Concepts. Table of Contents What is Algebra?

MATH36061 Convex Optimization

Algorithms for Data Science

Satellite project, AST 1100

Building a Computer Adder

Kurt Schmidt. June 5, 2017

Lecture 11: Information theory THURSDAY, FEBRUARY 21, 2019

CSE 105 THEORY OF COMPUTATION

Chemical Applications of Symmetry and Group Theory Prof. Manabendra Chandra Department of Chemistry Indian Institute of Technology, Kanpur

CHAPTER 1 LINEAR EQUATIONS

Queues. Principles of Computer Science II. Basic features of Queues

1 SIMPLE PENDULUM 1 L (1.1)

CSC321 Lecture 15: Recurrent Neural Networks

Announcements. Using a 72-hour extension, due Monday at 11:30AM.

Circuit Analysis and Ohm s Law

Thermodynamics (Classical) for Biological Systems Prof. G. K. Suraishkumar Department of Biotechnology Indian Institute of Technology Madras

HW8. Due: November 1, 2018

Let s start by defining what it means for two statements to be logically equivalent.

Today s Agenda: 1) Why Do We Need To Measure The Memory Component? 2) Machine Pool Memory / Best Practice Guidelines

import java. u t i l. ;... Scanner sc = new Scanner ( System. in ) ;

Introduction to Logic and Axiomatic Set Theory

WARM-UP. Compare using >, <, or =. 254 a 204 a m 63 m 36 c + d d + c b 287 b n n 440 k 540 k

Neural Networks Teaser

Math Fundamentals for Statistics I (Math 52) Unit 7: Connections (Graphs, Equations and Inequalities)

CSE 211. Pushdown Automata. CSE 211 (Theory of Computation) Atif Hasan Rahman

The Basics COPYRIGHTED MATERIAL. chapter. Algebra is a very logical way to solve

Lecture 5. September 4, 2018 Math/CS 471: Introduction to Scientific Computing University of New Mexico

Mathematics I. Quarter 2 : ALGEBRAIC EXPRESSIONS AND FIRST-DEGREE EQUATIONS AND INEQUALITIES IN ONE VARIABLE

In July: Complete the Unit 01- Algebraic Essentials Video packet (print template or take notes on loose leaf)

GRIDLang. Move-Driven Game Programming Language Final Report. May 10, 2017

MiniMat: Matrix Language in OCaml LLVM

Mapping Reducibility. Human-aware Robotics. 2017/11/16 Chapter 5.3 in Sipser Ø Announcement:

vsyscall and vdso Adrien «schischi» Schildknecht vsyscall and vdso March 17, 2014

Designing Information Devices and Systems I Spring 2018 Lecture Notes Note Introduction to Linear Algebra the EECS Way

Guidelines for Graphing Calculator Use at the Commencement Level

CS 700: Quantitative Methods & Experimental Design in Computer Science

Math101, Sections 2 and 3, Spring 2008 Review Sheet for Exam #2:

Rotational Motion. Figure 1: Torsional harmonic oscillator. The locations of the rotor and fiber are indicated.

P vs. NP. Data Structures and Algorithms CSE AU 1

ISLAND BIOGEOGRAPHY Lab 7

High-Performance Scientific Computing

S C F F F T T F T T S C B F F F F F T F T F F T T T F F T F T T T F T T T

Physics 212E Spring 2004 Classical and Modern Physics. Computer Exercise #2

Introduction to MPI. School of Computational Science, 21 October 2005

Lesson 20B: Absolute Value Equations and Inequalities

Extended Introduction to Computer Science CS1001.py. Lecture 8 part A: Finding Zeroes of Real Functions: Newton Raphson Iteration

Lecture 2: Linear regression

Math 308 Midterm Answers and Comments July 18, Part A. Short answer questions

Four-Pan Algebra Balance

Extensions to the Logic of All x are y: Verbs, Relative Clauses, and Only

GRADE 6 Projections Masters

Linear Programming Test Review. Day 6

Transcription:

Comp 11 Lectures Dr. Mike Shah Tufts University August 9, 2017 Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 1 / 34

Please do not distribute or host these slides without prior permission. Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 2 / 34

Wrap Up Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 3 / 34

Comp 11 - Pre-Class warm up Today s Agenda Pizza come to class hungry! Return Finals Course wrap up Learn about C Learn about Python Share what you did for final project (Optional) Class Picture Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 4 / 34

Lecture Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 5 / 34

Hidden Figures Figure 1: Katherine Johnson, Dorothy Vaughan, and Mary Jackson, were part of NASA s team of human computers. They were a group of programmers and mathematicians who helped send John Glenn around the Earth. The 2016 Hollywood movie is excellent in my opinion. Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 6 / 34

Course Wrap up Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 7 / 34

Summary We learned modern C++ Completed 6 homework assignments, 14 labs, and 15 in-class activities Got a preview of Data Structures (Comp 15), and Algorithms (Comp 160). You should be proud! I recommend continuing to learn a little bit of c++ each day keep practicing! Lynda, pluralsight, learncpp, cpluscplus, and youtube! Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 8 / 34

Programming in C Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 9 / 34

What is C? The C Programming language was created by Denis Ritchie. Popular Programming in C book is by Brian Kernighan and Ritchie, which is still a good read today! (Referred to as K and R book) C is essentially C++, without the object-oriented parts. It was one of the original systems programming languages. Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 10 / 34

Hello World from C 1 // The C l i b r a r y f o r i n p u t and output 2 #i n c l u d e <s t d i o. h> 3 4 i n t main ( ) { 5 6 // E q u i v a l e n t to s t d : : cout s t a t e m e n t 7 p r i n t f ( H e l l o world! \ n ) ; 8 9 r e t u r n 0 ; 0 } Listing 1: Back to where we first started this time in C! Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 11 / 34

Compiling Hello World When we compile, we use only clang without the ++. clang hello.c -o hello This means we are explicitly compiling C code only. No C++ features are available. Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 12 / 34

Similarities So it looks quite familiar to C++! What has changed from C++ are the libraries we call functions from. C++ was originally built on top of C. We can use in our C++ code, any of the C libraries (remember to compile with clang++). Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 13 / 34

Memory is King In C, we are often working very closely with memory. We use malloc instead of new - to allocate memory We use free instead of delete - to reclaim memory. Mixing the two will cause compiler problems. Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 14 / 34

Malloc and Free Example 1 #i n c l u d e <s t d i o. h> 2 #i n c l u d e < s t d l i b. h> 3 4 i n t main ( ) { 5 i n t s i z e = 1 0 0 ; 6 i n t myintarray = ( i n t ) m a l l o c (100 s i z e o f ( i n t ) ) ; 7 8 f o r ( i n t i =0; i < s i z e ; ++i ) { 9 myintarray [ i ] = i ; 0 p r i n t f ( %d, myintarray [ i ] ) ; 1 } 2 3 f r e e ( myintarray ) ; 4 5 r e t u r n 0 ; 6 } Listing 2: Equivalent to new and delete in C++ but they should not be mixed Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 15 / 34

Memory functions C offers some powerful functions for quickly operating on memory. memset memcpy Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 16 / 34

memset example 1 #i n c l u d e <s t d i o. h> 2 #i n c l u d e <s t r i n g. h> 3 4 i n t main ( ) { 5 6 i n t n = 1 0 0 ; 7 i n t myarray [ n ] ; 8 // Set each b y t e at a time to 0 9 memset ( myarray, 0, n s i z e o f ( myarray [ 0 ] ) ) ; 0 1 f o r ( i n t i =0; i < n ; ++i ) { 2 p r i n t f ( %d, myarray [ i ] ) ; 3 } 4 5 r e t u r n 0 ; 6 } Listing 3: Set one byte at a time to 0 Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 17 / 34

Why use C over C++ Generally a smaller footprint (executable sizes are smaller) This is helpful if we are programming embedded devices (think watches, a refrigerator, or a microwave, or items where we have limited storage). C has a more 1 to 1 ratio with assembly language, where you have even more control of how machine uses your code. Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 18 / 34

Here is what assembly looks like by the way 1 ; example from h t t p : / / a s m. s o u r c e f o r g e. n e t / i n t r o / h e l l o. h t m l 2 s e c t i o n. t e x t 3 g l o b a l s t a r t ; must be d e c l a r e d f o r l i n k e r ( l d ) 4 5 s t a r t : ; t e l l l i n k e r e n t r y p o i n t 6 mov edx, l e n ; message l e n g t h 7 mov ecx, msg ; message to w r i t e 8 mov ebx, 1 ; f i l e d e s c r i p t o r ( s t d o u t ) 9 mov eax, 4 ; system c a l l number ( s y s w r i t e ) 0 i n t 0 x80 ; c a l l k e r n e l 1 mov eax, 1 ; system c a l l number ( s y s e x i t ) 2 i n t 0 x80 ; c a l l k e r n e l 3 s e c t i o n. d a t a 4 msg db H e l l o, world!,0 xa ; our d e a r s t r i n g 5 l e n equ $ msg ; l e n g t h o f our d e a r s t r i n g Listing 4: Assembly actually maps almost 1 to 1 with machine code Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 19 / 34

Python Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 20 / 34

What is Python? Python is a high-level programming language created by Guido van Rossum. It is general purpose, in that any sort of program can be made. It is more a scripting language, that is, programs do not compile to executables. There is Python 2.7.X and Python 3 most actively used. (Similar to how we have C++ and C++11/14/17) Today we will be discussing Python 3, which is slightly different than 2.7, but will long term likely be the more dominant language. Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 21 / 34

Python - Follow Along https://repl.it/languages/python Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 22 / 34

Python - Hello World 1 p r i n t H e l l o World \n Listing 5: Hello world in python Wait-should we have more source code here, this slide looks empty Nope! Some trade offs on the next slides. Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 23 / 34

Python - Guessing Game in a Slide! 1 # Pound i s t h e comment symbol 2 import random # i n c l u d e a l i b r a r y 3 s e c r e t = r a n d i n t ( 1, 10) # G e n e r a t e a random number 4 g u e s s = 1 5 6 # C r e a t e our w h i l e l o o p 7 w h i l e g u e s s!= s e c r e t ; 8 g u e s s = r a w i n p u t ( ) 9 0 i f g u e s s < s e c r e t : 1 p r i n t g u e s s h i g h e r 2 e l s e : 3 p r i n t g u e s s l o w e r 4 5 p r i n t You got i t! Listing 6: Guessing Game Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 24 / 34

A few observations No curly braces {} s Instead, Python uses whitespace to create code blocks terminated with a colon to determine scope Also, less parentheses () s or semi-colons The language is very clean! Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 25 / 34

Python - The List 1 mylist = [ ] 2 3 mylist. append ( 1 ) 4 mylist. append ( 2 ) 5 mylist. append ( 3 ) 6 7 p r i n t mylist 8 9 mylist. pop ( ) 0 mylist. pop ( ) 1 2 p r i n t mylist Listing 7: The fundamental data structure in Python Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 26 / 34

Python - List Comprehension 1 numbers = [ 1, 2, 3, 4, 5 ] 2 3 # 3 l i n e s o f code to make a new l i s t 4 s q u a r e s L i s t = [ ] 5 f o r n i n numbers : 6 s q u a r e s L i s t. append ( n n ) 7 8 # 1 l i n e o f code to make a new l i s t 9 s q u a r e s L i s t C o m p r e h e n s i o n = [ n n f o r n i n numbers ] Listing 8: Generate a list Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 27 / 34

Python - Plots 1 import m a t p l o t l i b. p y p l o t as p l t 2 import math 3 4 f i g, ax = p l t. s u b p l o t s ( ) 5 6 loggrowth = [ ] 7 l i n e a r G r o w t h = [ ] 8 q u a d r a t i c G r o w t h = [ ] 9 f o r i i n range ( 1, 1 0 0 0 ) : 0 l i n e a r G r o w t h. append ( i ) 1 loggrowth. append ( math. l o g ( i, 2 ) ) 2 q u a d r a t i c G r o w t h. append ( i i ) ; 3 4 ax. p l o t ( loggrowth ) 5 ax. p l o t ( l i n e a r G r o w t h ) 6 ax. p l o t ( q u a d r a t i c G r o w t h ) 7 8 p l t. show ( ) Listing 9: Generate a list Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 28 / 34

Python - Functions 1 # D e f i n e a f u n c t i o n ( r e t u r n t y p e i n f e r r e d at run time ) 2 d e f s q u a r e ( x ) : 3 r e t u r n x x 4 5 # C r e a t e a l i s t o f 10 numbers 6 numbers = range ( 0, 1 0 ) 7 8 # Use a l i s t comprehension 9 s q u a r e s = [ s q u a r e ( n ) f o r n i n numbers ] 0 1 # P r i n t out the r e s u l t 2 p r i n t s q u a r e s Listing 10: Create a simple function Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 29 / 34

Python trade offs part 1 Python is extremely productive, in that there exist many libraries(i.e. package) to do the heavy lifting for us. The language was designed 20+ years after C, so there was time to innovate. C did its job for providing a higher level language in a resource constrained environment. There is no need to manage memory in Python (It is a memory managed language) There is no need to even declare the data type! (It is dynamically type, as opposed to statically typed the interpreter figures out what you mean) Python also has a nice REPL (Read-evaluate-print-loop) that lets you rapidly prototype code and ideas. You can do Object-Oriented program in Python just like in C++! Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 30 / 34

Python trade offs part 2 Typically, a Python program will run more slowly because it is interpreted. This means there is some overhead of the Python Interpreter that reads each line of python code you wrote over and over. There is no translation to 1 s and 0 s that the computer can quickly munch on. Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 31 / 34

In-Class Activity Quick 1-2 sentence pitch of your final project State your name (so your classmates remember you!) 1-2 sentence project pitch. Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 32 / 34

The End! (Of Comp 11) Figure 2: We made it! Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 33 / 34

Glossary Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, 2017 34 / 34