How to resolve the algorithm Doomsday rule step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Doomsday rule step by step in the C programming language

Table of Contents

Problem Statement

John Conway (1937-2020), was a mathematician who also invented several mathematically oriented computer pastimes, such as the famous Game of Life cellular automaton program. Dr. Conway invented a simple algorithm for finding the day of the week, given any date. The algorithm was based on calculating the distance of a given date from certain "anchor days" which follow a pattern for the day of the week upon which they fall. The formula is calculated assuming that Sunday is 0, Monday 1, and so forth with Saturday 7, and which, for 2021, is 0 (Sunday). To calculate the day of the week, we then count days from a close doomsday, with these as charted here by month, then add the doomsday for the year, then get the remainder after dividing by 7. This should give us the number corresponding to the day of the week for that date. Given the following dates:

Use Conway's Doomsday rule to calculate the day of the week for each date.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Doomsday rule step by step in the C programming language

This C code uses the Gregorian calendar to determine the weekday for a given date and prints the results for several dates. Here's how it works:

  • Header Includes:

    • The code includes necessary headers:
      • <stdio.h>: For input/output functions.
      • <stdint.h>: For integer type definitions.
      • <stdbool.h>: For the bool data type.
  • Date Structure:

    • A Date structure is defined to represent dates with three fields: year (16-bit unsigned integer), month (8-bit unsigned integer), and day (8-bit unsigned integer).
  • Leap Year Checker:

    • The leap function checks if a given year is a leap year. It returns true if the year is divisible by 4 and either not divisible by 100 or divisible by 400, following the Gregorian calendar rules.
  • Weekday Calculator:

    • The weekday function takes a Date as input and returns a string representing the weekday (e.g., "Sunday", "Monday"). It uses two arrays, leapdoom and normdoom, to account for leap years.
    • The function calculates the weekday based on the year, month, and day using the Zeller's congruence method.
  • Main Function:

    • The main function:
      • Defines arrays past and future for use in printing.
      • Defines an array months to provide names for months.
      • Defines an array dates containing several dates.
    • Iterates through the dates array and for each date:
      • Prints the date in the format "NAME_OF_MONTH DAY, YEAR will be/was on a WEEKDAY".
      • Determines whether the date is in the past or future compared to the current year (2021) and prints "will be" for future dates and "was" for past dates.

Source code in the c programming language

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>

typedef struct { 
    uint16_t year;
    uint8_t month;
    uint8_t day;
} Date;

bool leap(uint16_t year) {
    return year%4==0 && (year%100!=0 || year%400==0);
}

const char *weekday(Date date) {
    static const uint8_t leapdoom[] = {4,1,7,2,4,6,4,1,5,3,7,5};
    static const uint8_t normdoom[] = {3,7,7,4,2,6,4,1,5,3,7,5};
    static const char *days[] = {
        "Sunday", "Monday", "Tuesday", "Wednesday",
        "Thursday", "Friday", "Saturday"
    };
    
    unsigned c = date.year/100, r = date.year%100;
    unsigned s = r/12, t = r%12;
    
    unsigned c_anchor = (5 * (c%4) + 2) % 7;
    unsigned doom = (s + t + (t/4) + c_anchor) % 7;
    unsigned anchor = (leap(date.year) ? leapdoom : normdoom)[date.month-1];
    return days[(doom+date.day-anchor+7)%7];
}

int main(void) {
    const char *past = "was", *future = "will be";
    const char *months[] = { "",
        "January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December"
    };
    
    const Date dates[] = {
        {1800,1,6}, {1875,3,29}, {1915,12,7}, {1970,12,23}, {2043,5,14},
        {2077,2,12}, {2101,4,2}
    };
    
    int i;
    for (i=0; i < sizeof(dates)/sizeof(Date); i++) {
        printf("%s %d, %d %s on a %s.\n",
            months[dates[i].month], dates[i].day, dates[i].year,
            dates[i].year > 2021 ? future : past,
            weekday(dates[i]));
    }
    
    return 0;
}


  

You may also check:How to resolve the algorithm Tropical algebra overloading step by step in the Wren programming language
You may also check:How to resolve the algorithm Anagrams step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Exceptions step by step in the R programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the MoonScript programming language
You may also check:How to resolve the algorithm First-class functions/Use numbers analogously step by step in the Nim programming language