Menu
Coddy logo textTech

Recap - Calendar Grid

Part of the Logic & Flow section of Coddy's Swift journey — lesson 11 of 56.

challenge icon

Challenge

Medium

Read three integers from three lines: days (the total days in a month, 28 to 31), start_dow (0 = Monday … 6 = Sunday, the day of the week the 1st falls on), and cols (the number of columns to print).

Print a calendar grid:

  • Pad the first row with -- entries until the day of the week aligns (one -- per leading slot)
  • Print every day from 1 through days, two characters wide (use a leading space for single-digit days, e.g. 1, 10)
  • Use a single space between cells in a row
  • Wrap to a new line after every cols entries (including padding)

For input 30 / 2 / 7, the output is:

-- --  1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30

Try it yourself

let days = Int(readLine()!)!
let startDow = Int(readLine()!)!
let cols = Int(readLine()!)!

// TODO: build a flat array of cells ("--" + day strings),
// then print rows of `cols` cells joined by a single space

All lessons in Logic & Flow