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 1 Review
  • Code Style
  • Assert Library
  • Usage
  • Exercise 1 Further Discussion
  • odd.c
  • multiple.c
  • date.c
  • power.c
  • taxi.c
Edit on GitHub
  1. Lec/Tut/Lab/Exes
  2. Lab

Lab 03 - Assert

Thanks for my Lab Tutor Zhang Puyu!

PreviousLab 02 - DebuggingNextLab 04 - Test Cases

Last updated 6 months ago

Slides:

Exercise 1 Review

Code Style

  1. Initialize the variable immediately after the declaration.

For example,

// Not Recommended
double num;
num = cs1010_read_double();

// Recommended
double num = cs1010_read_double();
  1. Do not declare the function and then implement it somewhere else (Usually after main()). Implement your function right after the declaration.

No return after else

Code Example

// Not allowed
if (something)
{
    return something;
}
else
{
    return other_things;
}

Out of succient reasons, the else is of no use. So, we can simplify the code to below

// Allowed
if (something)
{
    return something;
}
return other_things;

No nested If

If your code is like below

if (something_1)
{
    if (something_2)
    {
        // do something
    }
}

Since there is only one if statement inside the "outer" if, so there is no need to use the nested if. And we can simplify it into

if (something_1 && something_2)
{
    // do sommething
}

Assert Library

assert.h is a library designed to help with debugging procedures.

Usage

assert(p) where p is a boolean expression. When the condition p fails during program execution, the program will halt with an error message.

Exercise 1 Further Discussion

odd.c

A solution that uses no condition is that m=n+(n%2)∗(n%2)+1m=n+(n\%2)*(n\%2)+1m=n+(n%2)∗(n%2)+1, where mmm f(x)=x∗e2piiξxf(x) = x * e^{2 pi i \xi x}f(x)=x∗e2piiξxis the smallest odd number that is strictly bigger than nnn.

The Deriving process

multiple.c

Modulo is not defined when we divide a non-zero number by 0. That is m%0m\%0m%0, where m≠0m\neq0m=0, is not defined!!!

date.c

There is a method not using conditions. Map each (m,d)(m,d)(m,d) date to the integer 100m+d100m+d100m+d.

The only criteria for coefficient of mmm is that the coefficient must be bigger than the maximum days in a month, which is 31. So m≥31m\geq31m≥31 should be okay.

The reason is that we should make sure the priority of the month is higher.

power.c

Ways to optimize

  1. Remove the useless work, when the base is 0, -1 or 1.

long compute_power(long x, long y)
{
    if (x == 0)
    {
        return 0;
    }
    if (x == 1)
    {
        return 1;
    }
    if (x == -1)
    {
        return y % 2 == 0 ? 1 : -1;
    }
    // ...
}
  1. Half the calculation

Recall that

xy={(x2)y2if y is even(x2)y−12⋅xif y is odd\large x^y=\begin{cases} \left(x^2\right)^{\frac{y}{2}} & \text{if } y \text{ is even} \\ \left(x^2\right)^{\frac{y-1}{2}} \cdot x & \text{if } y \text{ is odd} \end{cases}xy=⎩⎨⎧​(x2)2y​(x2)2y−1​⋅x​if y is evenif y is odd​

Then convert it to code will be intuitive

if (y % 2 == 0)
{
    return compute_power(x * x, y / 2);
}
return x * compute_power(x * x, (y - 1) / 2);

taxi.c

A normal way to compute ceil()

long ceil_of_quotient(long n, long m)
{
    if (n % m == 0)
    {
        return n / m;
    }
    return n / m + 1;
}

A smart way to compute ceil()

Suppose we want to calculate ⌈nm⌉\lceil \frac{n}{m} \rceil⌈mn​⌉, where nnn and mmm are two possitive integers. First, let us write n=mq+rn=mq+rn=mq+r, where q≥0q\geq0q≥0 and 0≤r≤m−10 \leq r \leq m-10≤r≤m−1. Now, let's consider

n+m−1m=mq+r+m−1m=m(q+1)+r−1m\large\frac{n+m-1}{m}=\frac{mq+r+m-1}{m}=\frac{m(q+1)+r-1}{m}mn+m−1​=mmq+r+m−1​=mm(q+1)+r−1​

If r=0r=0r=0, then we will get qqq as the output. If 0<r≤m−10<r \leq m-10<r≤m−1, then the above numerator is at least m(q+1)m(q+1)m(q+1) but strictly less than m(q+2)m(q+2)m(q+2), so the quotient evaluates to q+1q+1q+1, which will be our output.

The case r=0r=0r=0 is equivalent to the case that n%m=0n\%m=0n%m=0 The case 0<r≤m−10<r \leq m-10<r≤m−1 is equivalent to the case that n%m≠0n\%m \neq 0n%m=0

Thus, convert the result into `C` code ```c long ceil_of_quotient(long n, long m) { return (n + m - 1) / m; } ```

odd.c
348KB
Lab3.pdf
pdf
Lab 3 Slides