Computer Science 50 Introduction to Computer Science I Harvard College

Walkthrough 2 Marta Bralic [email protected]

1

Design and Style Design •  • 

What is good design? Questions: •  •  • 

Is my code straightforward to read? Am I wasting CPU cycles unnecessarily? Is my code more complicated than it need be?

2

Design and Style Style •  • 

What is good style? Questions: • 

Comments • 

• 

Variable names • 

• 

Is it clear what I am doing at each major step? Is it clear what each variable represents?

Prettiness •  • 

Is no line longer than 80 characters? Did I indent uniformly? 3

Problem Set 2 Standard Edition • 

Old Man song • 

• 

Caesar cipher •  • 

• 

Repetition…design decisions galore Command line arguments Arrays

Vigenère cipher • 

Arrays of arrays

4

Old Man’s Song The Lyrics

This old man, he played one He played knick-knack on my thumb Knick-knack paddywhack, give your dog a bone This old man came rolling home This old man, he played two He played knick-knack on my shoe Knick-knack paddywhack, give your dog a bone This old man came rolling home
 5

oldman.c – several ways • 

printf the entire song • 

• 

loop •  • 

• 

bad design good design what kind?

how do we store changing parts? •  •  • 

array function + conditions other ways? 6

Caesar Cipher ci = (pi + k)% 26 p k c

7

Command Line Arguments • 

argc •  • 

• 

when is it 1? when is it 2?

argv[] •  • 

argv[0] argv[1]

8

caesar.c • 

ci = (pi + k)% 26 •  •  •  • 

what is p? what is k? what is % 26? what is c?

9

Tools •  •  •  •  •  •  •  • 

isdigit atoi GetString() strlen loop conditions ascii printf 10

Challenges • 

how do we preserve case?

• 

how do we leave spaces unchanged?

11

Vigenère Cipher ci = (pi + kj) % 26 H E L L O ,

W O

R

L

D

+ + + + +

+

+

+

+

+

k

F O O B A

R

F

O O

B

c

↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ MS Z MO , N

↓ T

↓ F

↓ E

p

↓ Z

12

Differences • 

key is alphabetical • 

•  • 

find key’s numerical value must know when to advance key • 

• 

use isalpha instead of isdigit

not on spaces

must worry about wrapping around key

13

Computer Science 50 Introduction to Computer Science I Harvard College

Walkthrough 2

Marta Bralic [email protected]

14

Computer Science 50 Walkthrough 2

This old man, he played one. He played knick-knack on my thumb. Knick-knack paddywhack, give your dog a bone. This old man came rolling home. This old ...

163KB Sizes 0 Downloads 280 Views

Recommend Documents

Computer Science 50 Fall 2010 Scribe Notes Week 1 Friday ...
links in the ncurses.h library, a graphic library for C. In step 3, we actually .... a problem in industries like finance or biology. ... Conditions take the following form in C: ..... Linux command called man that provides a manual page for any func

Computer Science 50 Fall 2010 Scribe Notes Week 0 Wednesday ...
Sep 1, 2010 - the order of a web search engine), how many steps would it take to find that one page we're looking for? Not as many as you might think: only.

Computer Science 50 Fall 2010 Scribe Notes Week 0 Wednesday ...
Sep 1, 2010 - the order of a web search engine), how many steps would it take to find that one page we're looking for? Not as many as you might think: only.

Computer Science 50 Fall 2010 Scribe Notes Week 0 Wednesday ...
Another more intelligent approach would be to flip to the middle of the phonebook to ... oped by MIT's Media Lab to teach programming to children. ... flash drive.

Computer Science 50 Fall 2010 Scribe Notes Week 3 Monday ...
Almost 200 of you have so-called normal phones (i.e. non-smartphones). By the way ... is for Android, BlackBerry, and iPhone apps come semester's end when.