solving exercise 8 from arrays