Added loop that prints final results and destroys dynamically allocated memory.