completed solution for ex 4.