Lec 12 - Recap

Slides:

Lecture Slides

More on Data Structures

A very useful website recommended by Eldon:

C Plus Plus Reference

Recap of CS1010

Let's begin the most exciting, impressing and moving but probably the last part of CS1010! 😄 😭

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 (Types)

  • Beware of precision issue when using double and float

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

Branching and Logical Expressions

  • Use tables and flowcharts to help you understand the logic and simplify your code. (Lec 04 - Conditionals)

  • Use assertions to reason about your code. (Assertion)

  • How to use the assert macro

  • Avoid expressions that are always true or always false. (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)

  • Ensure we move towards the terminating conditions.

  • Use loop invariants to reason about your loops. (Loop Invariant)

  • We skipped break and continue statements.

Arrays and Structs

  • Composite data types that store multiple values together. (Fixed-Length Array)

  • Arrays is useful as a lookup table. (Counting Sort*)

  • String is nothing but an array with terminating null character (\0) (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 8. Compilation Error vs. Runtime Error)

Memory

  • Arrays decays into pointers.

  • Using pointers for passing by reference. (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 (Array Name Decay). For a 2-D array, arrays decay to the memory address of a pointer variable. (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

We focused on writing clean code. Not just code that works. That's why in Functions, we've mentioned that we should write functions that are small and do one thing.

2. How a C program behaves

Memory Model

Interacting with OS

3. Tools and Practices

clang, vim, bash

  • 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., 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., 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 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 😭

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

  • 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 😭. 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:

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

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

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

Last updated