code for exercise 2 and solution 2