try the pseudocode given above and make sure you get the loops right....best of luck :)