> For the complete documentation index, see [llms.txt](https://wenbo-notes.gitbook.io/cs1010-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wenbo-notes.gitbook.io/cs1010-notes/lec-tut-lab-exes/lecture/lec-12-recap.md).

# Lec 12 - Recap

Slides:

{% embed url="<https://nus-cs1010.github.io/2425-s1/slides/cs1010-lec12.html#1>" %}
Lecture Slides
{% endembed %}

## More on Data Structures

A very useful website recommended by Eldon:

{% embed url="<https://en.cppreference.com/w/>" %}
C Plus Plus Reference
{% endembed %}

## Recap of CS1010

> Let's begin the most exciting, impressing and moving but probably the last part of CS1010! :smile: :sob:

## **What have you learned?**

> A legit soul searching question. What have I learned in this course? I believe everyone more or less must learn something. But now, let's follow Prof Ooi's steps and have a look at what we've gone through together!

### **1. How to write C**

#### **Types**

* Use the correct type for the correct data ([Lec 02 - Functions and Types](/cs1010-notes/lec-tut-lab-exes/lecture/lec-02-functions-and-types.md#types))
* Beware of precision issue when using `double` and `float`

#### Functions

* Write functions that are small and do one thing ([Lec 02 - Functions and Types](/cs1010-notes/lec-tut-lab-exes/lecture/lec-02-functions-and-types.md#writing-good-functions))

> This is legit awesome for me! The specific settings in the `clang` compiler legit force me to think "breaking a big problem into smaller ones"! It

#### Operations

* arithmetic operations: `+`, `-`, `*`, `/`, `%` ([Lec 03 - Basic C Programming](/cs1010-notes/lec-tut-lab-exes/lecture/lec-03-basic-c-programming.md#arithmetic-operations))
* bit operations in C: `^`, `|`, `&`, `~`, `<<`, `>>`

#### Branching and Logical Expressions

* Use tables and flowcharts to help you understand the logic and simplify your code. ([Lec 04 - Conditionals](/cs1010-notes/lec-tut-lab-exes/lecture/lec-04-conditionals.md))
* Use assertions to reason about your code. ([Lec 04 - Conditionals](/cs1010-notes/lec-tut-lab-exes/lecture/lec-04-conditionals.md#assertion))
* How to use the `assert` macro
* Avoid expressions that are always true or always false. ([Lec 04 - Conditionals](/cs1010-notes/lec-tut-lab-exes/lecture/lec-04-conditionals.md#skipping-else))
* We skipped `switch/case` and `goto` statements.

#### Loops

* Identify the inital condition, what to repeat, when to stop, what/how to update after each repetition. ([Lec 05 - Loops](/cs1010-notes/lec-tut-lab-exes/lecture/lec-05-loops.md))
* Ensure we move towards the terminating conditions.
* Use loop invariants to reason about your loops. ([Lec 05 - Loops](/cs1010-notes/lec-tut-lab-exes/lecture/lec-05-loops.md#loop-invariant))
* We skipped `break` and `continue` statements.

#### Arrays and Structs

* Composite data types that store multiple values together. ([Lec 06 - Call Stacks, Arrays](/cs1010-notes/lec-tut-lab-exes/lecture/lec-06-call-stacks-arrays.md#fixed-length-array))
* Arrays is useful as a lookup table. ([Lec 09 - Searching and Sorting](/cs1010-notes/lec-tut-lab-exes/lecture/lec-09-searching-and-sorting.md#counting-sort))
* String is nothing but an array with terminating null character (`\0`) ([Lec 07 - Pointers, Memory management](/cs1010-notes/lec-tut-lab-exes/lecture/lec-07-pointers-memory-management.md#string))
* Beware of array out-of-bound errors.
* We skipped `union` and `enum`.

> Usually, array out-of-bound errors won't be detected by the compiler, so it won't generate compilation error/warning. But, it will generate Runtime Error and usually it is the **segmentation fault.** (See more at [/pages/8xsYKpqqwH8t5zVtOGeM#id-8.-compilation-error-vs.-runtime-error](https://wenbo-notes.gitbook.io/cs1010-notes/lec-tut-lab-exes/lecture/pages/8xsYKpqqwH8t5zVtOGeM#id-8.-compilation-error-vs.-runtime-error "mention"))

#### Memory

* Arrays decays into pointers.
* Using pointers for passing by reference. ([Lec 06 - Call Stacks, Arrays](/cs1010-notes/lec-tut-lab-exes/lecture/lec-06-call-stacks-arrays.md#pass-an-array-to-a-function))
* Effective habits of memory management.

> For 1-D array, arrays decay to **memory address of a basic data type variable, e.g. `long`** ([Lec 06 - Call Stacks, Arrays](/cs1010-notes/lec-tut-lab-exes/lecture/lec-06-call-stacks-arrays.md#array-name-decay)). For a 2-D array, arrays decay to the **memory address of a pointer variable**. ([Lec 08 - Multi-d Array, Efficiency](/cs1010-notes/lec-tut-lab-exes/lecture/lec-08-multi-d-array-efficiency.md#array-name-decay-multidimensional-array))

#### Pre-processing

* `#include` for headers
* `#define` constants and macros
* `#ifdef` and `#endif` for conditional compilation

> These content are covered in [Lab 08 - C Preprocessor](/cs1010-notes/lec-tut-lab-exes/lab/lab-08-c-preprocessor.md)

We focused on writing clean code. Not just code that works. That's why in [#functions](#functions "mention"), we've mentioned that we should write functions that are small and do one thing.

<figure><img src="/files/WvFm1TRrUMiJpGMV7PhY" alt="" width="563"><figcaption></figcaption></figure>

### 2. How a C program behaves

#### Memory Model

* Stack and heap ([Lec 06 - Call Stacks, Arrays](/cs1010-notes/lec-tut-lab-exes/lecture/lec-06-call-stacks-arrays.md#call-stack) and [Lec 07 - Pointers, Memory management](/cs1010-notes/lec-tut-lab-exes/lecture/lec-07-pointers-memory-management.md#heap))
* Pointers and memory addresses ([Lec 07 - Pointers, Memory management](/cs1010-notes/lec-tut-lab-exes/lecture/lec-07-pointers-memory-management.md#pointers))
* Pass by value vs. pass by reference ([Lec 06 - Call Stacks, Arrays](/cs1010-notes/lec-tut-lab-exes/lecture/lec-06-call-stacks-arrays.md#pass-an-array-to-a-function))

#### Interacting with OS

* Proper memory management with `malloc` and `free`. ([Lec 08 - Multi-d Array, Efficiency](/cs1010-notes/lec-tut-lab-exes/lecture/lec-08-multi-d-array-efficiency.md#why-we-cannot-use-free-buckets-here))

### 3. Tools and Practices

#### `clang`, `vim`, `bash`

<figure><img src="/files/HsUOUdLpAEc4w3J0882A" alt=""><figcaption></figcaption></figure>

* Address and bound sanitizer
* `clang-format`
* `clang-tidy`
* `git`
* `make`

### 4. Problem Solving Techniques

#### Decomposition

* Break down the problem into "bite-size" sub-problems
* Solve them one-by-one
* Compose them back to solve the original problem

> There are many valuable Exercises that legit legit practice this "Decomposition" idea!

#### Recursion

* Solve the "simplest" version.
* Assume you can solve the "simpler" version.
* Compose the solution to the original problem from the solution of the simpler version.

> The idea of "wishful thinking", classic!

#### Thinking Tools

* Flowcharts
* Truth Tables
* Assertion and invariants
* Test cases

## Computational Thinking

The mental process associated with computational problem solving

### Four Pillars of Computational Thinking

* Decomposition
* Pattern Recognition
* Abstraction
* Algorithms

> If you want to grow up in programming, please don't stop "dirty work", that is coding practically and try these four very very important ideas!

#### Decomposition

> Decomposition makes a problem easier to solve, reason about, and test

George Polya said: "If you can't solve a problem, there is an easier problem that you can solve: find it"

* Solve the easier problem first, then generalized.
* E.g., [PE1 Review](/cs1010-notes/past-year-exam/pe1-review.md#ex5-q6-social): find two-hops friends, then generalized to k hops
* E.g., `pattern`: draw the left most cell, then generalized

**Recursion**

* Assume the easier problem is solved, then generalized
* E.g., `binary`: assume we know how to convey a binary number n-1 digits to decimal, then solve for n digits.
* E.g., [Lec 10 - More Recursion](/cs1010-notes/lec-tut-lab-exes/lecture/lec-10-more-recursion.md#tower-of-hanoi): assume we know how to move k-1 discs, then solve for k disc.

#### Pattern Recognition

> Observe trends and patterns, then generalized

**Writing loops**

* What to initialize?
* What changes from one loop to the next?
* Loop invariants

> This idea I feel is covered more in designed the search algorithm. e.g., should the bounds be inclusive or exclusive? See more from the tips in [Lec 09 - Searching and Sorting](/cs1010-notes/lec-tut-lab-exes/lecture/lec-09-searching-and-sorting.md#binary-search). Besides searching problem, when you are doing Exercise 6, you also need this skill of *Pattern Recognition*!

**Make code shorter and easier to change**

* `Taxi` from Exercise 1

> Wah, nostalgic, legit nostalgic :sob:

#### **Abstraction**

* Identifying and abstracting relevant information
* Hide details
* Adapt to change
* Generalize to other domain

**Data Abstraction**

> Model the problem with only the necessary information

* The string abstarction we've used in [Lec 10 - More Recursion](/cs1010-notes/lec-tut-lab-exes/lecture/lec-10-more-recursion.md#n-queens).

**Functional Abstraction**

> Hide details and focus on higher-level logic

#### **Algorithms**

Ways to solve problems

* Branches and Loops
* Recursion
* Memoization with arrays
* Divide and Conquer
* Exploit properties in data

[#four-pillars-of-computational-thinking](#four-pillars-of-computational-thinking "mention")

* Decomposition
* Pattern Recognition
* Abstraction
* Algorithms

Now, it comes to the end of the recap of the content covered in CS1010. It's a bit disappointing because why the time passes so fast :sob:. But, stay strong and be optimistic, there will be more challenging but interesting courses about coding waiting for you (CS2040C, CS3230, etc).

Still remember from Lec 1, Prof Ooi told us:

<figure><img src="/files/tS4Sx2WU3ThSsr3qJadS" alt="" width="563"><figcaption></figcaption></figure>

Lemme go back to the Lec01 Slides, here are the words of advice given by Prof Ooi before the lecture starts:

<div align="center"><figure><img src="/files/YHg9V8C8RCmeS46kuP3H" alt=""><figcaption></figcaption></figure> <figure><img src="/files/dMDxcrhDD3NBQx4JrnEZ" alt=""><figcaption></figcaption></figure></div>

<figure><img src="/files/ukRyerqnReDw32zKBLqS" alt="" width="563"><figcaption></figcaption></figure>

What great word of advice! After these intense and tough "trainings" by CS1010, I believe more or less you will fell growing up in coding! :clap:

> "Grade is not everything. Focus on: learning new things, level-up your skills and enriching your experience!"

At last, there is "One more thing..." as usual:

## Special Thanks to...

From Prof Ooi:

* The tutorial instructors: Eric, Gizem, Yunjeong, Malaika
* The lab tutors: (all 25 of them)
* The SoC IT Support Team: Tan Hsiao Wei, Lai Zit Seng, and lab technicians
* All the students

For me, I legit want to express my gratitude to:

* Prof Ooi and Dr. Eldon for lectures
* Dr. Eric for tutorials
* My senior Zhang Puyu for the lab
* All my classmates during lab and tutorials

> "The course may end, but the journey of learning will never end!"
