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