lecture_materials:week_00
                Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| lecture_materials:week_00 [2025-08-18 18:21] – created Jeffrey Bergamini | lecture_materials:week_00 [2025-08-22 12:59] (current) – Jeffrey Bergamini | ||
|---|---|---|---|
| Line 5: | Line 5: | ||
| </ | </ | ||
| - | <WRAP round tip> | + | ===== Monday  | 
| - | ===== Slides  | + | |
| + | {{ : | ||
| + | {{ : | ||
| + | |||
| + | ===== Tuesday ===== | ||
| + | |||
| + | ==== Scope and Lifetime ==== | ||
| + | |||
| + | {{ : | ||
| + | |||
| + | === scopes.cpp (illustrating locally and globally scoped variables) === | ||
| + | |||
| + | <file cpp scopes.cpp [enable_line_numbers=true]> | ||
| + | /** | ||
| + | * @file scopes.cpp | ||
| + | * @author Jeffrey Bergamini for CS 19 Boot Camp, jeffrey.bergamini@cabrillo.edu | ||
| + | * | ||
| + |  * Let's see how much you understand/ | ||
| + | */ | ||
| + | |||
| + | #include < | ||
| + | |||
| + | int x; | ||
| + | |||
| + | void f1() { | ||
| + | x = 2; | ||
| + |   std::cout << x << ' | ||
| + | } | ||
| + | |||
| + | void f2(int x) { | ||
| + |   std::cout << x << ' | ||
| + | { | ||
| + | int x; | ||
| + | x = 4; | ||
| + |     std::cout << x << ' | ||
| + | } | ||
| + | x = 5; | ||
| + |   std::cout << x << ' | ||
| + | } | ||
| + | |||
| + | int main() { | ||
| + | x = 1; | ||
| + |   std::cout << x << ' | ||
| + | f1(); | ||
| + |   std::cout << x << ' | ||
| + | f2(3); | ||
| + |   std::cout << x << ' | ||
| + | int x; | ||
| + | x = 6; | ||
| + |   std::cout << x << ' | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <WRAP round info> | ||
| + | == Notes == | ||
| + | |||
| + | Your job: Be the compiler and the shell and determine the output of this program without using any tools other than your mind! | ||
| + | |||
| + | Then compile and run this code to see if you were right. | ||
| </ | </ | ||
| - | ===== Monday ===== | + | === scopes_annotated.cpp (illustrating locally and globally scoped variables)  | 
| - | Forthcoming | + | <file cpp scopes_annotated.cpp [enable_line_numbers=true]> | 
| + | /** | ||
| + | * @file scopes_annotated.cpp | ||
| + | * @author Jeffrey Bergamini for CS 19 Boot Camp, jeffrey.bergamini@cabrillo.edu | ||
| + | * | ||
| + | * Let's add *memory addresses* to the output to see which symbol `x` represents in which context. | ||
| + | */ | ||
| - | ===== Tuesday ===== | + | #include < | 
| - | TBD | + | int x; // global variable, global scope | 
| + | |||
| + | void f1() { | ||
| + | x = 2; // assigns to global variable `x` | ||
| + |   std::cout << &x << ' | ||
| + | } | ||
| + | |||
| + | void f2(int x) { | ||
| + |   std::cout << &x << ' | ||
| + | { | ||
| + | int x; // local variable (local to function `f1()`), scoped within anonymous block | ||
| + | x = 4; // assigns to local variable `x` scoped within anonymous block | ||
| + |     std::cout << &x << ' | ||
| + | } | ||
| + | x = 5; // assigns to local variable `x` (function parameter) | ||
| + |   std::cout << &x << ' | ||
| + | } | ||
| + | |||
| + | int main() { | ||
| + | x = 1; // assigns to global variable (i.e., global var `x` is a *lvalue*) | ||
| + |   std::cout << &x << ' | ||
| + | f1(); | ||
| + |   std::cout << &x << ' | ||
| + | f2(3); | ||
| + |   std::cout << &x << ' | ||
| + | int x; // local variable, local scope | ||
| + |   x = 6;  // assigns to local variable `x` (global `x` is now " | ||
| + |   std::cout << &x << ' | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <WRAP round info> | ||
| + | == Notes == | ||
| + | |||
| + | This is the same program, but prints the **memory address** of each variable in question. Note which '' | ||
| + | </ | ||
| + | |||
| + | === raii.cpp (illustrating the concept of RAII) === | ||
| + | |||
| + | <file cpp raii.cpp [enable_line_numbers=true]> | ||
| + | /** | ||
| + | * @file raii.cpp | ||
| + | * @author Jeffrey Bergamini for CS 19 Boot Camp, jeffrey.bergamini@cabrillo.edu | ||
| + | * | ||
| + |  * RAII: " | ||
| + |  * Perhaps easier to understand: " | ||
| + | * | ||
| + |  * An object' | ||
| + |  * and released during destruction. When an object' | ||
| + |  * deallocated/ | ||
| + | * | ||
| + |  * We'll run this through `strace` to verify when files are opened/ | ||
| + |  * strace --trace=openat, | ||
| + | */ | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | // Counts and returns the number of English vowel characters in an input stream. | ||
| + | size_t count_vowels(std:: | ||
| + | size_t vowel_count = 0; | ||
| + | for (char c; source >> c;) { | ||
| + |     c = std:: | ||
| + |     if (c == ' | ||
| + |       ++vowel_count; | ||
| + | } | ||
| + |   return vowel_count; | ||
| + | } | ||
| + | |||
| + | int main() { | ||
| + |   std:: | ||
| + |   // Preview of subtype polymorphism: | ||
| + | // (i.e., an input stream reading from a file is a specific kind of input stream) | ||
| + |   std::cout << " | ||
| + |   std::cout << " | ||
| + | { | ||
| + |     std:: | ||
| + |     std::cout << " | ||
| + | // Lifetime of `macbeth` ends here. File will be closed. | ||
| + | } | ||
| + |   std:: | ||
| + |   std::cout << " | ||
| + | // std::cin is just a plain input stream: | ||
| + |   std::cout << " | ||
| + | // Lifetimes of `hamlet` and `othello` end here. | ||
| + | // Will be closed in the opposite order of initialization (`othello`, then `hamlet`) | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <WRAP round info> | ||
| + | == Notes == | ||
| + | |||
| + | [[https:// | ||
| + | </ | ||
| + | |||
| + | === scope_lifetime_practice.cpp (an opportunity to think and practice) === | ||
| + | |||
| + | <file cpp scope_lifetime_practice.cpp [enable_line_numbers=true]> | ||
| + | /** | ||
| + | * @file scope_lifetime_practice.cpp | ||
| + | * @author Jeffrey Bergamini for CS 19 Boot Camp, jeffrey.bergamini@cabrillo.edu | ||
| + | * | ||
| + | * Some lunchtime practice! | ||
| + | */ | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | /** | ||
| + | * `main()` can have parameters to access *command-line arguments* and *environment variables*. | ||
| + | * | ||
| + | * @param argc argument count: the number of command-line arguments (including the executable name) | ||
| + | * @param argv argument values: an array of C strings containing the command-line arguments | ||
| + | */ | ||
| + | int main(int argc, char **argv) { | ||
| + |   // Exit with an error message if command-line arguments weren' | ||
| + | if (argc < 3) { | ||
| + |     std::cerr << " | ||
| + | return 1; // nonzero exit status reports unsuccessful termination to the shell | ||
| + | } | ||
| + |   // Attempt to open the two files specified as command-line arguments: | ||
| + |   std:: | ||
| + |   std:: | ||
| + | if (!f1 || !f2) { | ||
| + |     std::cerr << " | ||
| + | return 2; | ||
| + | } | ||
| + | // TODO: Find all the unique printable characters present in one file and not the other. | ||
| + | // Print each unique character and its ASCII encoding, one character per line, in ascending order. | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <WRAP round info> | ||
| + | == Notes == | ||
| + | |||
| + | Having some data to test with is useful. I have many textual datasets available. If you have your program in a form that is executable in a terminal, the following commands should work for testing: | ||
| + | |||
| + | Command: | ||
| + | <code bash> | ||
| + | ./a.out <(echo suitably) <(echo implausible) | ||
| + | </ | ||
| + | |||
| + | Expected Output: | ||
| + | < | ||
| + | e 101 | ||
| + | m 109 | ||
| + | p 112 | ||
| + | t 116 | ||
| + | y 121 | ||
| + | </ | ||
| + | |||
| + | Command: | ||
| + | <code bash> | ||
| + | ./a.out <(curl -s https:// | ||
| + | </ | ||
| + | |||
| + | Expected output: | ||
| + | < | ||
| + | '  | ||
| + | , 44 | ||
| + | - 45 | ||
| + | . 46 | ||
| + | J 74 | ||
| + | M 77 | ||
| + | j 106 | ||
| + | k 107 | ||
| + | v 118 | ||
| + | </ | ||
| + | |||
| + | Command: | ||
| + | <code bash> | ||
| + | ./a.out <(curl -s https:// | ||
| + | </ | ||
| + | |||
| + | Expected Output: | ||
| + | < | ||
| + | "  | ||
| + | # 35 | ||
| + | $ 36 | ||
| + | % 37 | ||
| + | * 42 | ||
| + | + 43 | ||
| + | / 47 | ||
| + | 0 48 | ||
| + | 4 52 | ||
| + | 5 53 | ||
| + | 6 54 | ||
| + | 7 55 | ||
| + | 8 56 | ||
| + | 9 57 | ||
| + | < 60 | ||
| + | = 61 | ||
| + | > 62 | ||
| + | @ 64 | ||
| + | J 74 | ||
| + | U 85 | ||
| + | X 88 | ||
| + | Z 90 | ||
| + | _ 95 | ||
| + | j 106 | ||
| + | ~ 126 | ||
| + | </ | ||
| + | </ | ||
| ===== Wednesday ===== | ===== Wednesday ===== | ||
| - | TBD | + | {{ : | 
| ===== Thursday ===== | ===== Thursday ===== | ||
| - | TBD | + | {{ : | 
| + | & Programming Languages Guide}} | ||
| ===== Friday ===== | ===== Friday ===== | ||
| - | TBD | + | {{ : | 
lecture_materials/week_00.1755566467.txt.gz · Last modified:  by Jeffrey Bergamini
                
                