How to resolve the algorithm Five weekends step by step in the XPL0 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Five weekends step by step in the XPL0 programming language

Table of Contents

Problem Statement

The month of October in 2010 has five Fridays, five Saturdays, and five Sundays.

Algorithm suggestions

Extra credit Count and/or show all of the years which do not have at least one five-weekend month (there should be 29).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Five weekends step by step in the XPL0 programming language

Source code in the xpl0 programming language

include c:\cxpl\codes;                  \intrinsic 'code' declarations


func    WeekDay(Year, Month, Day);      \Return day of week (0=Sat 1=Sun..6=Fri)
int     Year, Month, Day;
[if Month<=2 then [Month:= Month+12;  Year:= Year-1];
return rem((Day + (Month+1)*26/10 + Year + Year/4 + Year/100*6 + Year/400) / 7);
];      \WeekDay


int     MonthTbl, Year, I, C;
[MonthTbl:= [1, 3, 5, 7, 8, 10, 12];            \months with 31 days
C:= 0;
for Year:= 1900 to 2100 do
  for I:= 0 to 6 do                             \for all the 31-day months...
    if WeekDay(Year, MonthTbl(I), 1) = 6 then   \first of month is a Friday
        [C:= C+1;                               \count this year
        if C<=5 or C>201-5 then                 \show first 5 and last 5 years
                [IntOut(0, Year);  ChOut(0, ^ );
                 IntOut(0, MonthTbl(I));  CrLf(0);
                ];
        ];
IntOut(0, C);  CrLf(0);                         \show number of years

\Count and show all years that don't have any 5-weekend months
C:= 0;
for Year:= 1900 to 2100 do
        [for I:= 0 to 6 do                      \for all the 31-day months...
            if WeekDay(Year, MonthTbl(I), 1) = 6 \Friday\ then
                I:= 10;                         \bail out of 'for' loop
        if I<10 then                            \'for' loop completed
                [if (C&$F) = 0 then CrLf(0);    \(format 16 years per line)
                C:= C+1;                        \ without finding a 5-weekend
                IntOut(0, Year);  ChOut(0, ^ ); \ so show the year
                ];
        ];
CrLf(0);  IntOut(0, C);  CrLf(0);               \show number of years
]

  

You may also check:How to resolve the algorithm Find if a point is within a triangle step by step in the Racket programming language
You may also check:How to resolve the algorithm Rot-13 step by step in the BaCon programming language
You may also check:How to resolve the algorithm Execute a system command step by step in the Lasso programming language
You may also check:How to resolve the algorithm Trigonometric functions step by step in the Elixir programming language
You may also check:How to resolve the algorithm Parametric polymorphism step by step in the Common Lisp programming language