finished version of the cp.cpp code