adding answer to question6, Q6.cpp