Apply Loop Invariant in Binary Search

When reading the book “Accelerated C++” years ago, I was impressed by the effectiveness of loop invariant in ensuring bug-free of many functions implemented in the book. That reading opens me another door to formulate and solve some of the error-prone programming problems. The idea of loop invariant is a …

Two C Techniques: Typedef and Switch by "Symbol"

First to summarize the usage of typedef in C/C++. The usage can be categorized into three class:

  1. to indicate what a variable indicates
  2. to simplify a declaration, i.e. struct, class
  3. using typedef with pointers
  4. using typedef with typecasting

Here are some tips:

  1. basically, typedef is define a more self-explained …

Pointer Notes

Pointers are very important, this post is a learning note form the cplusplus.com pointer section as well as the Stanford CS library booklet Pointers and Memory (By Nick Parlante, Copyright ©1998-2000, Nick Parlante). The first topic about pointer is how to declare them, how to dereference them, and how to assign a …

Function With Variable Arguments

To define a function with a variable number of arguments. One solution is to use the cstdarg header file. There are four parts needed:

  1. va_list, stores the list of arguments,
  2. va_start, initializes the argument list,
  3. va_arg, returns the next argument in the list,
  4. va_end, cleans up the variable argument list.

Whenever a …

C++ Simple File Handling

C++ has two basic stream classes to handle files, ifstream and ofstream. To use them, include the header file fstream. Here is an example:

 1#include <fstream>
 2#include <iostream>
 3using namespace std;
 4
 5int main()
 6{
 7  char str[10];
 8
 9  // Creates an instance of ofstream, and opens example.txt …