How to resolve the algorithm Day of the week step by step in the C++ programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Day of the week step by step in the C++ 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 C++ programming language

This C++ program uses modern C++20 features, including ranges and views, to calculate and print the years from 2008 to 2120 where Christmas (December 25th) falls on a Sunday. Here's a detailed explanation of the code:

  1. #include <chrono> and #include <ranges>: These lines include the necessary C++20 standard library headers for working with dates, times, and ranges.

  2. std::cout << "Yuletide holidays must be allowed in the following years:\n";: This line prints the introductory message indicating that the program will list the years where Christmas falls on a Sunday.

  3. for (int year : std::views::iota(2008, 2121) | std::views::filter([](auto year) { ... })):

    • std::views::iota(2008, 2121) creates a range of integers from 2008 to 2120 (inclusive).
    • std::views::filter applies a filter to the range, keeping only the years that meet a specific condition. The condition is specified within the lambda function.
  4. Inside the lambda function:

    • std::chrono::year{year}/std::chrono::December/25 creates a std::chrono::date object representing December 25th of the given year.
    • std::chrono::weekday{} gets the weekday for the specified date.
    • The expression std::chrono::weekday{ ... } == std::chrono::Sunday checks if the weekday is Sunday. If true, it means Christmas falls on Sunday for that year, and the year is kept in the filtered range.
  5. std::cout << year << '\n';: For each year that passes the filter, this line prints the year to the console.

The program loops through the years from 2008 to 2120, checking which years have Christmas falling on a Sunday. It then prints the list of years where this condition is met. The output will show the years where Yuletide holidays are allowed, i.e., when Christmas is on a Sunday.

Source code in the cpp programming language

#include <chrono>
#include <ranges>
#include <iostream>

int main() {
    std::cout << "Yuletide holidays must be allowed in the following years:\n";
    for (int year : std::views::iota(2008, 2121)
               | std::views::filter([](auto year) {
                    if (std::chrono::weekday{
                            std::chrono::year{year}/std::chrono::December/25}
                            == std::chrono::Sunday) {
                        return true;
                    }
                    return false;
                })) {
        std::cout << year << '\n';
    }
}


  

You may also check:How to resolve the algorithm 99 bottles of beer step by step in the Brat programming language
You may also check:How to resolve the algorithm Parsing/RPN to infix conversion step by step in the Ada programming language
You may also check:How to resolve the algorithm Superellipse step by step in the QBasic programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the SETL programming language
You may also check:How to resolve the algorithm Here document step by step in the 11l programming language