Print Grid

Published by MrDrProfessor in

Write a method that accepts two integer parameters rows and cols. The output is a comma-separated grid of numbers displayed in column-major order, meaning the numbers shown increase sequentially down each column and wrap to the top of the next column to the right once the bottom of the current column is reached.

Examples

printGrid(3, 6) ➞ "
  1, 4, 7, 10, 13, 16
  2, 5, 8, 11, 14, 17
  3, 6, 9, 12, 15, 18
"

printGrid(5, 3) ➞ "
  1, 6, 11
  2, 7, 12
  3, 8, 13
  4, 9, 14
  5, 10, 15
"

printGrid(4, 1) ➞ "
  1
  2
  3
  4
"

Notes

  • Each row ends with "\n" e.g. "1, 6, 11\n" rather then a space "1, 6, 11 " (=wrong)
  • Last line "\n" should be trimed.
Watch a quick demo on how Edabit works.