class: middle, center # Lecture 3 ### 23 August 2021 Admin Matters
Unit 5: **First C Program**
Unit 6: **CS1010 I/O Library**
Unit 7: **Arithmetic Operations**
--- class: middle ## Reminders - Tutorial/lab start this week - 1st Catch-up this Saturday 10am - 1st Assignment out next week - Ungraded Exercise 0 release this Thursday --- class: middle ### Before Lab - GitHub account _must_ be ready - You _must_ be able to login to PE nodes ### Before Tutorial - Attempt Problem 1.1 to 1.3 --- class: middle ### Catch-Up Session - Q and A format - Ask via [PigeonHole](https://pigeonhole.at/SEEASTENTEN) (`SEEASTENTEN`) - Optional session for those falling behind - No new materials will be covered --- class: middle ### The Teaching Team - 2 full-time tutors - 3 Ph.D. part-time tutors - 30 UG tutors - 1:7 ratio --- class: middle .fit[] --- class: middle .fit[] --- class: center,middle ### _Don't:_ do it alone ### _Do:_ Discuss with classmates and teaching team --- 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[] --- 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[] --- 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[] --- 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 x = x++; ``` --- class:middle ```C x = x++; ``` The behavior is not defined. It is up to the compiler. --- class: middle,center .fit[] --- class: middle ### _Don't_: Stop at understanding the notes ### _Do_: "play" with it, ask "what if..", "why..", "why not..", etc --- class: bottom .tiny[ Version: 1.0 Last Updated: Mon Aug 23 00:44:28 +08 2021 ]