16
CS50 Week 2

CS50 Week 2. RESOURCES Office hours ( Lecture videos, slides, source code, and notes (

Embed Size (px)

Citation preview

Page 1: CS50 Week 2. RESOURCES Office hours ( Lecture videos, slides, source code, and notes (

CS50

Week 2

Page 2: CS50 Week 2. RESOURCES Office hours ( Lecture videos, slides, source code, and notes (

RESOURCES

Office hours (https://www.cs50.net/ohs)

Lecture videos, slides, source code, and notes (https://www.cs50.net/lectures)

A growing corpus of course material (http://manual.cs50.net)

Walkthroughs Sundays 7-8:30 in Northwest Science B103 (videos at https://www.cs50.net/psets)

Bulletin board (http://help.cs50.net)

[email protected]

Us! (https://www.cs50.net/staff)

Page 3: CS50 Week 2. RESOURCES Office hours ( Lecture videos, slides, source code, and notes (

FUNCTIONS

fx f(x)

A function is a black box – it takes some input(s), does something to them, and produces some output.

Page 4: CS50 Week 2. RESOURCES Office hours ( Lecture videos, slides, source code, and notes (

What's the use?

Organization – we can group ('encapsulate') lines of related code into a function, and give it some descriptive name. This helps us break a big problem into small chunks

Reusability – we can write a function once, and use it as many times as we'd like. Imagine if you had to copy and paste the code that implements “printf” every time you wanted to print something!

Page 5: CS50 Week 2. RESOURCES Office hours ( Lecture videos, slides, source code, and notes (

Anatomy of a function

<return type><name> (<arguments>){

<code>}

intmain (int argc, char *argv[]){

// code}

Page 6: CS50 Week 2. RESOURCES Office hours ( Lecture videos, slides, source code, and notes (

Anatomy of a function

Return type: int, void, char, float, etc...

Name: main, printf, GetInt, do_something3, etc...

Arguments: “int x”, “char *argv[]”, “float max”, etc.

These 3 things make up the “header” of the function. The implementation of the function is all that's left, and it is called the “body”

Page 7: CS50 Week 2. RESOURCES Office hours ( Lecture videos, slides, source code, and notes (

SCOPE All variables are contained within a certain scope, and can only be used within that scope.

Anything within the same set of curly braces is within the same scope. So functions, loops, and if/else blocks, which always have explicit or implied curly braces, always set up a new scope

If a variable is declared within a scope with the same name as a variable declared in a broader scope that contains the inner scope (for example, an if condition inside of a while loop), then it is impossible to access the first-declared variable until the inner scope is exited.

Global (as opposed to local) variables are declared outside all curly braces, and thus outside of all functions. There scope is thus the program itself!

Page 8: CS50 Week 2. RESOURCES Office hours ( Lecture videos, slides, source code, and notes (

MAGIC NUMBERS

Image: http://www.funbrain.com/guess2/index.html

Values hard-coded in a program that were seemingly “pulled out of a hat.” Magic numbers are bad style!

Instead, use #define to give the number a name, e.g. NUM_INPUTS, and replace all instances of the magic number with that constant. This allows us to, for example, change the number of inputs by just changing what that constant is defined as!

#define NUM_INPUTS 4

Page 9: CS50 Week 2. RESOURCES Office hours ( Lecture videos, slides, source code, and notes (

ARRAYS

A data structure that groups together a certain number of values, all of the same type.

Thanks to arrays, if we want 100 different variables that store user input, we don't need to declare 100 variables, we can just use a single array of size 100.

Allow for easy iteration over a set of data.

Page 10: CS50 Week 2. RESOURCES Office hours ( Lecture videos, slides, source code, and notes (

ARRAYS

int numbers[4];

numbers[0] = 7;numbers[1] = 8;numbers[3] = 2;numbers[2] = 5;

? ? ? ?

numbers 0 1 2 3

?

Alternative: int numbers[4] = {7,8,5,2};

Page 11: CS50 Week 2. RESOURCES Office hours ( Lecture videos, slides, source code, and notes (

ARRAYS

for(int i = 0; i < 4; i++){ printf(“%d\n”, numbers[i]);}

Easy iteration:

Page 12: CS50 Week 2. RESOURCES Office hours ( Lecture videos, slides, source code, and notes (

MULTI-DIMENSIONAL ARRAYSA.K.A., “arrays of arrays”

Useful when it makes more sense to think of an array in terms of having both rows and columns, or more!

char tictactoe[3][3];

tictactoe[0][0] = 'X';tictactoe[0][2] = 'X';tictactoe[1][1] = 'O';...

Page 13: CS50 Week 2. RESOURCES Office hours ( Lecture videos, slides, source code, and notes (

Passing Arrays to Functions

Remember from lecture that the “swap” function didn't work so well, since the variables in the function are just copies, and so changing them doesn't affect the original variables.

Arrays aren't primitive data types, so they don't work in the same way. Instead, they are passed by 'reference', as opposed to the way we have seen where primitives are passed by 'value'

So if you pass an array to a function, and that function changes the array, the change remains after the function returns!

Page 14: CS50 Week 2. RESOURCES Office hours ( Lecture videos, slides, source code, and notes (

CAESAR CIPHER

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

G H I J K L M N O P Q R S T U V W X Y Z A B C D E FExample: Rot 6

Rotate each character in a string by some number n characters. Effectively, a new

“starting point” of the alphabet is created

Page 15: CS50 Week 2. RESOURCES Office hours ( Lecture videos, slides, source code, and notes (

VIGENERE CIPHER

Similar to Caesar, except now “n” isn't constant over the entire string

Given the key 'abf': To encode: rotate the 1st character in our secret string by 'a' (i.e. 0),

the 2nd by 'b' (1), the 3rd by 'f' (5), the 4th by 'a' again, etc.

To decode: rotate letters in the opposite direction!

Instead of taking in just the string that we want to encode as input, we take an additional string, the “key”, which will be used

to encode the first string

Page 16: CS50 Week 2. RESOURCES Office hours ( Lecture videos, slides, source code, and notes (

NOW IT’S TIME FOR SOME CODING!!!

• https://cloud.cs50.net/~jjozwiak/