Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS50 Week 2. RESOURCES Office hours (https://www.cs50.net/ohs)https://www.cs50.net/ohs Lecture videos, slides, source code, and notes (https://www.cs50.net/lectures.

Similar presentations


Presentation on theme: "CS50 Week 2. RESOURCES Office hours (https://www.cs50.net/ohs)https://www.cs50.net/ohs Lecture videos, slides, source code, and notes (https://www.cs50.net/lectures."— Presentation transcript:

1 CS50 Week 2

2 RESOURCES Office hours (https://www.cs50.net/ohs)https://www.cs50.net/ohs Lecture videos, slides, source code, and notes (https://www.cs50.net/lectures )https://www.cs50.net/lectures A growing corpus of course material (http://manual.cs50.net)http://manual.cs50.net Walkthroughs Sundays 7-8:30 in Northwest Science B103 (videos at https://www.cs50.net/psets)https://www.cs50.net/psets Bulletin board (http://help.cs50.net)http://help.cs50.net help@cs50.net Us! (https://www.cs50.net/staff)https://www.cs50.net/staff

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

4 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!

5 Anatomy of a function ( ) { } int main (int argc, char *argv[]) { // code }

6 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”

7 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!

8 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

9 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.

10 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};

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

12 MULTI-DIMENSIONAL ARRAYS A.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';...

13 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!

14 CAESAR CIPHER ABCDEFGHIJKLMNOPQRSTUVWXYZ GHIJKLMNOPQRSTUVWXYZABCDEF Example: Rot 6 Rotate each character in a string by some number n characters. Effectively, a new “starting point” of the alphabet is created

15 VIGENERE CIPHER Similar to Caesar, except now “n” isn't constant over the entire string Given the key 'abf': To encode: rotate the 1 st character in our secret string by 'a' (i.e. 0), the 2 nd by 'b' (1), the 3 rd by 'f' (5), the 4 th 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

16 NOW IT’S TIME FOR SOME CODING!!! https://cloud.cs50.net/~jjozwiak/


Download ppt "CS50 Week 2. RESOURCES Office hours (https://www.cs50.net/ohs)https://www.cs50.net/ohs Lecture videos, slides, source code, and notes (https://www.cs50.net/lectures."

Similar presentations


Ads by Google