Lab 03 - Assert
Thanks for my Lab Tutor Zhang Puyu!
Last updated
Thanks for my Lab Tutor Zhang Puyu!
Last updated
Slides:
Initialize the variable immediately after the declaration.
For example,
Do not declare the function and then implement it somewhere else (Usually after main()
). Implement your function right after the declaration.
Code Example
Out of succient reasons, the else
is of no use. So, we can simplify the code to below
If your code is like below
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
assert.h
is a library designed to help with debugging procedures.
assert(p)
where p
is a boolean expression. When the condition p
fails during program execution, the program will halt with an error message.
A solution that uses no condition is that , where is the smallest odd number that is strictly bigger than .
Modulo is not defined when we divide a non-zero number by 0. That is , where , is not defined!!!
There is a method not using conditions. Map each date to the integer .
The only criteria for coefficient of is that the coefficient must be bigger than the maximum days in a month, which is 31. So should be okay.
The reason is that we should make sure the priority of the month is higher.
Remove the useless work, when the base is 0, -1 or 1.
Half the calculation
Recall that
Then convert it to code will be intuitive
Suppose we want to calculate , where and are two possitive integers. First, let us write , where and . Now, let's consider
If , then we will get as the output. If , then the above numerator is at least but strictly less than , so the quotient evaluates to , which will be our output.
The case is equivalent to the case that The case is equivalent to the case that
Thus, convert the result into `C` code ```c long ceil_of_quotient(long n, long m) { return (n + m - 1) / m; } ```