This sounds an awful lot like a homework assignment...

Why don't you try to code it, and someone can help you out when you run into issues?