How to resolve the algorithm Day of the week step by step in the Icon and Unicon programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Day of the week step by step in the Icon and Unicon programming language

Table of Contents

Problem Statement

A company decides that whenever Xmas falls on a Sunday they will give their workers all extra paid holidays so that, together with any public holidays, workers will not have to work the following week (between the 25th of December and the first of January).

In what years between 2008 and 2121 will the 25th of December be a Sunday? Using any standard date handling libraries of your programming language; compare the dates calculated with the output of other languages to discover any anomalies in the handling of dates which may be due to, for example, overflow in types used to represent dates/times similar to   y2k   type problems.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Day of the week step by step in the Icon and Unicon programming language

Source code in the icon programming language

link datetime

procedure main()
writes("December 25th is a Sunday in: ")
every writes((dayoweek(25,12,y := 2008 to 2122)=="Sunday",y)," ")
end


procedure dayoweek(day, month, year)	#: day of the week
   static d_code, c_code, m_code, ml_code, y, C, M, Y

   initial {
      d_code := ["Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]

      c_code := table()
      c_code[16] := c_code[20] := 0
      c_code[17] := c_code[21] := 6
      c_code[18] := c_code[22] := 4
      c_code[19] := c_code[23] := 2

      m_code := table()
      m_code[1] := m_code["January"] := 1
      m_code[2] := m_code["February"] := 4
      m_code[3] := m_code["March"] := 4
      m_code[4] := m_code["April"] := 0
      m_code[5] := m_code["May"] := 2
      m_code[6] := m_code["June"] := 5
      m_code[7] := m_code["July"] := 0
      m_code[8] := m_code["August"] := 3
      m_code[9] := m_code["September"] := 6
      m_code[10] := m_code["October"] := 1
      m_code[11] := m_code["November"] := 4
      m_code[12] := m_code["December"] := 6

      ml_code := copy(m_code)
      ml_code[1] := ml_code["January"] := 0
      ml_code[2] := ml_code["February"] := 3
      }

   if year < 1600 then stop("*** can't compute day of week that far back")
   if year > 2299 then stop("*** can't compute day of week that far ahead")

   C := c_code[(year / 100) + 1]
   y := year % 100
   Y := (y / 12) + (y % 12) + ((y % 12) / 4)
   month := integer(month)
   M := if (year % 4) = 0 then ml_code[month] else m_code[month]

   return d_code[(C + Y + M + day) % 7 + 1] 
   
end


  

You may also check:How to resolve the algorithm Read a specific line from a file step by step in the Java programming language
You may also check:How to resolve the algorithm Pernicious numbers step by step in the Plain English programming language
You may also check:How to resolve the algorithm First class environments step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Van der Corput sequence step by step in the 360 Assembly programming language
You may also check:How to resolve the algorithm Primorial numbers step by step in the Wren programming language