Why are you working with arrays? A proper container (e.g., std::vector) is more robust and safer.
As for the other part of your problem, this is easily solvable with std::generate and a functor.