Completed ex2 of assignment 4