CS1010 Notes
  • Welcome
  • Lec/Tut/Lab/Exes
    • Lecture
      • Lec 01 - Computational Problem Solving
      • Lec 02 - Functions and Types
      • Lec 03 - Basic C Programming
      • Lec 04 - Conditionals
      • Lec 05 - Loops
      • Lec 06 - Call Stacks, Arrays
        • Diagnostic Quiz
      • Lec 07 - Pointers, Memory management
        • Diagnostic Quiz
      • Lec 08 - Multi-d Array, Efficiency
        • Diagnostic Quiz
      • Lec 09 - Searching and Sorting
        • Diagnostic Quiz
      • Lec 10 - More Recursion
        • Diagnostic Quiz
      • Lec 11 - Strcut & Standard I/O
        • Diagnostic Quiz
      • Lec 12 - Recap
    • Tutorial
      • Tut 01 - Computational Problem-Solving
      • Tut 02 - Functions and Conditionals
      • Tut 03 - More on Conditionals
      • Tut 04 - Loops
      • Tut 08 - Searching and Sorting
    • Lab
      • Lab 01 - Unix/Vim Setup
      • Lab 02 - Debugging
      • Lab 03 - Assert
      • Lab 04 - Test Cases
      • Lab 05 - Arrays
      • Lab 06 - Memory Errors
      • Lab 07 - Compiling with Clang
      • Lab 08 - C Preprocessor
      • Lab 09 - Backtracking
      • Lab 10 - Struct and Wrap up
    • Exercises
      • Exercise 3 - Fixed-Length Arrays
      • Exercise 4 - Dynamic Arrays and Strings
      • Exercise 6 - Searching and Sorting
      • Exercise 7 - More Recursion
      • Exercise 8 - Struct
  • Past Year Exam
    • Midterm PE
      • PE1 (AY18/19)
      • PE1 (AY20/21)
      • PE1 (AY21/22)
      • PE0 (AY22/23)
      • PE0 (AY23/24)
    • Midterm Paper
      • Midterm (AY18/19)
      • Midterm (AY20/21)
      • Midterm (AY21/22)
      • Midterm (AY22/23)
    • PE1 Review
      • PE1 (AY23/24)
    • PE2 Review
      • PE2 (AY18/19)
      • PE2 (AY20/21)
      • PE2 (AY21/22)
      • PE2 (AY22/23)
      • PE2 (AY23/24)
    • Final Paper
      • Final (AY18/19)
      • Final (AY20/21)
      • Final (AY21/22)
      • Final (AY22/23)
      • Final (AY23/24)
  • Current Year Exam
    • PE0 (AY24/25)
    • PE1 (AY24/25)
    • PE2 (AY24/25)
    • Final (AY24/25)
  • Toolbox
    • Vim & Unix
    • GDB
  • After CS1010
Powered by GitBook
On this page
  • Exercise 5
  • C Preprocessing
  • Macro
  • Bonus Info
  • Searching and Sorting
  • Binary Search
  • Comparison-Based Sort
  • Counting Sort
  • Exercise 6
Edit on GitHub
  1. Lec/Tut/Lab/Exes
  2. Lab

Lab 08 - C Preprocessor

PreviousLab 07 - Compiling with ClangNextLab 09 - Backtracking

Last updated 6 months ago

Slides:

Exercise 5

  1. will include the \n character.

C Preprocessing

  1. Use #define to define a constant to make your program more readable.

Macro

A macro is a code snippet that is substituted into the program and expanded during pre-processing.

Example:

#include "cs1010.h"
#define SQUARE(x) x * x
int main() {
    // Will output 25
    cs1010_println_long(SQUARE(5));
    // Will output 16.0000
    cs1010_println_double(SQUARE(4.0));
    return 0;
}

Generic Types

We can use a generic type (or type parameter) to restrict the type of the arguments used in a macro.

Example:

#include "cs1010.h"
#define SWAP(T, x, y) {T t; t = x; x = y; y = t}
int main() {
    long a = 1;
    long b = 2;
    SWAP(long, a, b); // Now a == 2 && b == 1
    char m[4] = "abc";
    char n[4] = "123";
    SWAP(char *, m, n);
    // Now m is "123" and n is "abc"
    return 0;
}

Pitfall

Be careful with situations like this:

#include "cs1010.h"
#define SQUARE(x) x * x
int main() {
    cs1010_println_long(SQUARE(5 + 1));
    // The above gets expanded to 5 + 1 * 5 + 1
    return 0;
}

Therefore, we should always use brackets around the arguments of a macro, i.e., SQUARE(x) (x) * (x) is safe.

Bonus Info

  1. There are five major types of operations which are core to algorithm optimization: insertion, removal, retrieval, searching and sorting.

Searching and Sorting

Binary Search

  1. Probably the most powerful search algorithm for simple arrays.

  2. The idea of search space.

  3. Sorted means non-descending in CS.

Comparison-Based Sort

The idea is the pair-wised comparison is important.

Bubble Sort

  1. There are some better cases when the time complexity is O(N)O(N)O(N).

Insertion Sort

  1. When the array is sorted, the time complexity is O(N)O(N)O(N)

Selection Sort

  1. The time complexity is always O(N)O(N)O(N)

Counting Sort

  1. To use it on negative indices, use the idea of mapping. For example, -9 to 0.

Exercise 6

  1. Start from the minimum point, have two directions.

  2. Every time see log(n)log(n)log(n), try thinking about binary search.

  3. Every time see O(N)O(N)O(N), which means can done in one iteration.

char** cs1010_read_line_array(size_t k)
176KB
Lab8.pdf
pdf
Lab 8 Slides