converted moments function to C++