class: middle, center # Lecture 3 ### 24 August 2020 `vim`
Unit 5: **First C Program**
Unit 6: **CS1010 I/O Library**
Unit 7: **Arithmetic Operations**
--- class: middle,center ## 工欲善其事
必先利其器 ### Confucius said: To get things done properly, get the tools ready first. --- class: middle, center ### Wei Tsang said: "Invest time to master the tools" (e.g., `bash`, `vim`, `git`, etc) ### "Think long term gain, not just to get things done now" --- class: middle, center ## Learn a source code editor --- class: middle,center ### One editor for all tasks (email, notes, report, programming) for the rest of your career --- class: middle,center ### Installed by default in many systems --- class: middle,center ### Use one that is terminal-based --- class: middle,center ### Avaiable on restricted exam environment for CS1010 --- class: middle,center # `vim` --- class: middle ### Basic Movement: `h` `j` `k` `l` ### Mode Switching: `i` `esc` ### Save and quiz: `ZZ` --- class: middle ### For the rest, learn as you need. ### Ask: "how to do this faster?" and learn from there. --- class: bottom,center background-image: url(figures/dota-hotkeys.jpg) If you can learn the hotkeys for games, you can learn the hotkeys for `vim`. --- class: middle ### Tips: - Run `vimtutor` - Read the `vim`-related readings on CS1010 website (incl Aaron's videos!) --- class: middle ## Reminders - Tutorials start this Thursday - Quizzes 1-3 due this Thursday 10am - 1st Catch-up this Saturday 10am - 1st Assignment out next week --- class: middle ## Before Thursday - GitHub account _must_ be ready - You _must_ be able to login to PE nodes - Attempt Problem 1.1 to 1.3 --- class: middle .fit[![TAs](figures/tutors/group1.png)] --- class: middle .fit[![TAs](figures/tutors/group2.png)] --- class: middle,center # Let's C --- class: middle ```C int square(int x) { return x * x; } ``` --- class: middle .tiny[ ```C int square(int x) { return x * x; } int main() { int hypotenuse_square; hypotenuse_square = square(3) + square(4); return 0; } ``` ] --- class: middle .tiny[ ```C int square(int x) { return x * x; } int hypotenuse_square; // 🙅 int main() { hypotenuse_square = square(3) + square(4); return 0; } ``` ] --- class: middle,center .fit[![meme](figures/meme/bad-good-global.jpg)] --- class: middle .tiny[ ```C #include
long square(long x) { return x * x; } double hypotenuse_of(long base, long height) { return sqrt(square(base) + square(height)); } int main() { hypotenuse_of(3, 4); } ``` ] --- class: middle,center .tiny.center[ type | min bits -----:|:-------- `char` | 8 `short` | 16 `int` | 16 `long` | 32 `long long` | 64 ] --- class: middle - `int` is not sufficient for most CS1010 purposes. - We will only use `long` in CS1010. --- class: middle .tiny.center[ type | num of bits -----:|:-------- `float` | 32 `double` | 64 ] - We will only use `double` in CS1010. --- class: middle,center .fit[![meme](figures/meme/bad-good-int-long.jpg)] --- class: middle .moretiny[ ```C #include
#include "cs1010.h" long square(long x) { return x*x; } double hypotenuse_of(long base, long height) { return sqrt(square(base) + square(height)); } int main() { double hypotenuse; long base = cs1010_read_long(); long height = cs1010_read_long(); hypotenuse = hypotenuse_of(base, height); cs1010_println_double(hypotenuse); } ``` ] --- class: middle,center ### Basic operators `+` `-` `*` `/` `%` --- class:middle ### Pitfalls: order of operations can be confusing `8 / 2 * (2 + 2)` --- class:middle ### CS1010: Use parenthesis everywhere `8 / (2 * (2 + 2))` `(8 / 2) * (2 + 2)` --- class: middle,center .fit[![meme](figures/meme/bad-good-arithmetic.jpg)] --- class:middle ```C x = x + 1; x += 1; ``` --- class: middle ```C x
= y; x = x
y; ``` --- class: middle ```C x = x + 1; x += 1; x++; ++x; ``` --- class: middle ```C y = x++; ``` is the same as ```C y = x; x = x + 1; ``` --- class: middle ```C y = ++x; ``` is the same as ```C x = x + 1; y = x; ``` --- class: middle ```C x = x++; ``` --- class:middle ```C x = x++; ``` The behavior is not defined. It is up to the compiler. --- class: middle,center .fit[![meme](figures/meme/bad-good-plusplus.jpg)] --- class: bottom .tiny[ Version: 1.0 Last Updated: Mon Aug 24 11:34:14 +08 2020 ]