Solution A for programming assignment 2