adapt c++ code to flat arrays