added C code for distance calculations