Exercise 7 done in cpp