This is CS 50. Harvard College’s Introduction to Computer Science I...
Transcript of This is CS 50. Harvard College’s Introduction to Computer Science I...
![Page 1: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/1.jpg)
\This is CS 50.
COMPUTER SCIENCE 50Harvard College’s Introduction to Computer Science I
DAVID J. MALAN ’[email protected]
WEEK 2
1
![Page 2: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/2.jpg)
z
Image from http://www.questexperiences.com/quest2/movieadventures/default.asp.
Or fher gb qevax lbhe binygvar!
2
![Page 3: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/3.jpg)
z
“Double ROT13 is pretty good, but for extra security, quadruple ROT13 is available. It is probably pretty
computationally expensive to pass it through the cipher than many times, but security is worth it.”
ROT13
3
http://channel9.msdn.com/forums/Coffeehouse/221675-Can-someone-translate-this-please/
![Page 4: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/4.jpg)
z
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
Academic Honesty
4
![Page 5: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/5.jpg)
z
Mobile Phones
5
![Page 6: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/6.jpg)
z
Mobile Carriers
6
![Page 7: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/7.jpg)
z
And you are...
7
![Page 8: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/8.jpg)
z
Dorms
8
![Page 9: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/9.jpg)
z
Houses
9
![Page 10: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/10.jpg)
z
Best House
10
![Page 11: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/11.jpg)
z
Operating Systems
11
![Page 12: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/12.jpg)
z
Why 50?
12
![Page 13: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/13.jpg)
z
Comfort Levels
13
![Page 14: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/14.jpg)
z
Prior Courses
14
![Page 15: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/15.jpg)
z
‣ C++‣ Java‣ LISP‣ Perl‣ PHP‣ ...
seehai.{cc,lisp,php,pl}, Hai.java
How to Write a Program in...
15
![Page 16: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/16.jpg)
z
seebuggy{1,2}.c
Image from http://www.history.navy.mil/.
Bugs
16
![Page 17: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/17.jpg)
z
int i = (int) 'A';
char c = (char) 65;
seeascii{1,2,3}.c, battleship.c
Casting
17
![Page 18: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/18.jpg)
z
99 bottles of beer on the wall,99 bottles of beer,Take one down, pass it around,98 bottles of beer on the wall.
seebeer{1,2,3,4}.c
Image from http://z.about.com/d/tvcomedies/1/7/n/5/-/-/homer_simpson.jpg.
FunctionsParameters and Arguments
18
![Page 19: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/19.jpg)
z
voidswap(int a, int b){ int tmp;
tmp = a; a = b; b = tmp;}
seebuggy3.c
FunctionsScope, Local Variables, Temporary Variables
19
![Page 20: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/20.jpg)
z
void
increment()
{
x++;}
seebuggy4.c, global.c, buggy5.c
FunctionsScope, Global Variables
20
![Page 21: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/21.jpg)
z
The StackFrames
21
![Page 22: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/22.jpg)
z
Memory ManagementSneak Preview
22
![Page 23: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/23.jpg)
z
int
cube(int a)
{
return a * a * a;
}
seereturn{1,2}.c
FunctionsReturn Values
23
![Page 24: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/24.jpg)
z
seearray.c, buggy6.c, string{1,2}.c, capitalize.c
Image from http://computer.howstuffworks.com/c10.htm.
Arrays
24
![Page 25: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/25.jpg)
z
‣ http://www.howstuffworks.com/c.htm‣ http://www.cs50.net/resources/cppreference.com/
Free Resources
25
![Page 26: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/26.jpg)
z
int main(int argc, char *argv[]);
seeargv{1,2}.c
Command-Line Argumentsargc, argv
26
![Page 27: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/27.jpg)
z
seehttp://www.cs50.net/pub/releases/cs50/
‣ bool
‣ string
‣ char GetChar();
‣ double GetDouble();
‣ float GetFloat();
‣ int GetInt();
‣ long long GetLongLong();
‣ string GetString();
CS 50’s Library(Memory Leaks)
27
![Page 28: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/28.jpg)
z
Or fher gb qevax lbhe binygvar!
Image from http://www.radioarchives.org/annie/.
Cryptography
28
![Page 29: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/29.jpg)
z
Image from http://en.wikipedia.org/wiki/Enigma_machine.
CryptographyEnigma Machine
29
![Page 30: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/30.jpg)
z
Image from http://www.nuitari.de/crypto.html.
CryptographySecret (Symmetric) Keys
30
![Page 31: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/31.jpg)
z
Image from http://commons.wikimedia.org/wiki/Image:Hw-caesar.jpg.
CryptographyCaesar Cipher
31
![Page 32: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/32.jpg)
z
ci = (pi + k) % 26
Image from http://en.wikipedia.org/wiki/Caesar_cipher.
CryptographyCaesar Cipher
32
![Page 33: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/33.jpg)
z
ci = (pi + ki) % 26
p H E L L O , W O R L D
+ + + + + + + + + +
k F O O B A R F O O B
↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
c M S Z M O , N T F Z E
CryptographyVigenère Cipher
33
![Page 34: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/34.jpg)
z
Figure from Larry Nyhoff’s C++: An Introduction to Data Structures
72,057,594,037,927,936
CryptographyDES
34
![Page 35: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/35.jpg)
z
Figure from http://www.nuitari.de/crypto.html.
CryptographyPublic and Private (Asymmetric) Keys
35
![Page 36: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/36.jpg)
z
-‐-‐-‐-‐-‐BEGIN PGP PUBLIC KEY BLOCK-‐-‐-‐-‐-‐Version: PGP Key Server 0.9.6
mQGiBEFMqoURBADaifCKsYkPBmVSMvspdLHLIAsb0xe4cyOieCA5LQCUZi9Z+YxuOSMkbQ+VSAvtP31/7o9pNf6LsLU4ADA5knZVB+GfZZpiGnEd62qKDKNpjVo20NRHXcd/4RpxE6aJWUWe2tPqLSCi3NFLPEhnfo5v9WyLRHjqdIQKc6vGAT41BQCg/5/1vNpKhyA6VrFDIuozNWqKpAUD/1lAHxPxfxLyn8K/Gv/wl0y97dRDq0vsRqkh57ITYKy/Xjv4qzNWZ/dSXI7Fa/6xULRuYK6tcr5aI6bFVLBl4fIXn5tapcCdYLAMo2apUf/+PRJgSNUg4j50F5GjjiKco7FYldaF3oy6DVQzjEtSHN2TFczVOMHJUax1Ip/UDRjIA/9vOO/MZ7FspAW1ZOdC13CxVSnig4oALGbNf76RViFGOl0bByVLVlBxMiIlv8wxSbydqxsvokPZ/uCOfSqedO+l9xmIEp/Luq4k2owKfyAB2U33+HkfzS8RM4zJWy1i8jXNzEfyFsqmJ0RKfrzJe7jXX34ZMfbPc3r39eR4w9lo+bQmUm9uYWxkIEwgUml2ZXN0IDxyaXZlc3RAY3NhaWwubWl0LmVkdT6JAEYEEBECAAYFAkFMquoACgkQ5DGedS1eYN7UGACgzEZmCLhzzVz2kc3/5curi183AiMAn3NOJx6SJOL3n2fNAAar7B5M0z9Zi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Z0Zrl0LC/SvyXyPNb87RAN1ttuDspFQ4/puUoxz/ICurJbBWx09oc29yyXiGX8YHfF6NFaUCSJH5WlfS9uIQEdip6dmFB7Q2qvOYHLF5nAg2zXvg8LzWI3dcxH0OXHVy2KkG1EbndUtq8cI8yz1+I6Pdfqb0DWvmIVVSHJMLtuZBUY1D8vsoZ2K0//PcNMuqHU8ZfHCAXwmrJAfzYhU8TP6P4YKqa/W4Cxwy897yaaZHoR3iqhdDakMHrnDPAw4isGJ20jPEXpzQ5H4i7PEqk+phVxiEhbLZbddz1y0ZK/5dub5ci5mCwGZBVb9XTecZruwOe7ptWIvBvYhGBltUUfSF4wEwvoaxcC6EzFRpEqBRm+tgcgcfwU1V9oywoMhLQwB9LDVjnNkRoNuaEa2o8CnheehNU05NSASsSo4z2WWbkRGErZZaWiafLe+XhDC+hImWwOdL5ZatkQ5qJp3GuFW0FldqaYJLY1KNn9P+cpLhPEq5Hq27vcULDa1L5AMnKIbusSSrRP9MhWiQBMBBgRAgAMBQJBTKqFBQkCak+AAAoJEIdenepUv6CUbCkAn13adk2J
HcZLgEhuNLZPTye4iNgRAKCtq+gBowVJ761YhVK2NMBi+8B3sw===j2zM
-‐-‐-‐-‐-‐END PGP PUBLIC KEY BLOCK-‐-‐-‐-‐-‐
Ron Rivest’s public key from http://pgp.mit.edu/.
CryptographyPGP
36
![Page 37: This is CS 50. Harvard College’s Introduction to Computer Science I …cdn.cs50.net/2009/fall/lectures/2/week2.pdf · 2012-08-22 · This is CS 50. COMPUTER SCIENCE 50 Harvard College’s](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec726a63ed94a09191bd662/html5/thumbnails/37.jpg)
z
‣ Public Key: (e, n)‣ Private Key: (d, n)‣ To Encrypt:
- c = pe (mod n)‣ To Decrypt:
- p = c d (mod n)
Ron Rivest’s public key from http://pgp.mit.edu/.
CryptographyRSA
37