solving exercise 3 from arrays