How to resolve the algorithm Biorhythms step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Biorhythms step by step in the C programming language

Table of Contents

Problem Statement

For a while in the late 70s, the pseudoscience of biorhythms was popular enough to rival astrology, with kiosks in malls that would give you your weekly printout. It was also a popular entry in "Things to Do with your Pocket Calculator" lists. You can read up on the history at Wikipedia, but the main takeaway is that unlike astrology, the math behind biorhythms is dead simple. It's based on the number of days since your birth. The premise is that three cycles of unspecified provenance govern certain aspects of everyone's lives – specifically, how they're feeling physically, emotionally, and mentally. The best part is that not only do these cycles somehow have the same respective lengths for all humans of any age, gender, weight, genetic background, etc, but those lengths are an exact number of days. And the pattern is in each case a perfect sine curve. Absolutely miraculous! To compute your biorhythmic profile for a given day, the first thing you need is the number of days between that day and your birth, so the answers in Days between dates are probably a good starting point. (Strictly speaking, the biorhythms start at 0 at the moment of your birth, so if you know time of day you can narrow things down further, but in general these operate at whole-day granularity.) Then take the residue of that day count modulo each of the the cycle lengths to calculate where the day falls on each of the three sinusoidal journeys. The three cycles and their lengths are as follows: The first half of each cycle is in "plus" territory, with a peak at the quarter-way point; the second half in "minus" territory, with a valley at the three-quarters mark. You can calculate a specific value between -1 and +1 for the kth day of an n-day cycle by computing sin( 2πk / n ). The days where a cycle crosses the axis in either direction are called "critical" days, although with a cycle value of 0 they're also said to be the most neutral, which seems contradictory. The task: write a subroutine, function, or program that will, given a birthdate and a target date, output the three biorhythmic values for the day. You may optionally include a text description of the position and the trend (e.g. "up and rising", "peak", "up but falling", "critical", "down and falling", "valley", "down but rising"), an indication of the date on which the next notable event (peak, valley, or crossing) falls, or even a graph of the cycles around the target date. Demonstrate the functionality for dates of your choice. Example run of my Raku implementation: Double valley! This was apparently not a good day for Mr. Fischer to begin a chess tournament...

Let's start with the solution:

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

Code Explanation:

This C program calculates and displays the age and biorhythmic cycles for two given dates.

Function Declarations:

  • day: Calculates the number of days since a reference date (730530) based on the given year (y), month (m), and day (d).
  • cycle: Computes the biorhythmic cycle percentage for a given difference (diff) in days within a specified cycle length (l). It also displays the cycle name (t) and a warning if the percentage is critical (< 15%).

Main Function:

  • Parses command-line arguments to obtain six integers representing two dates in the format yyyy mm dd.
  • Calculates the absolute difference (diff) in days between the two dates using the day function.
  • Prints the age in days.
  • Computes and displays the biorhythmic cycles for three different lengths:
    • Physical: 23 days
    • Emotional: 28 days
    • Intellectual: 33 days

Code Breakdown:

  1. day Function:

    • Converts the given date into a sequence of integers representing the number of days since a reference date (730530).
    • Uses a mathematical formula that considers the year, month, and day.
  2. cycle Function:

    • Computes the biorhythmic cycle percentage using the sine function.
    • The cycle length (l) is specified as an integer.
    • Displays the cycle name (t) and the percentage.
    • If the percentage is less than 15% in absolute value, it prints a warning message.
  3. main Function:

    • Checks if there are enough command-line arguments.
    • Calculates the age in days using the day function.
    • Computes and displays the physical, emotional, and intellectual cycles using the cycle function.

Sample Input and Output:

./cbio 1972 7 11 1943 3 9
Age: 10717 days
   Physical cycle: -27%
  Emotional cycle: -100%
 Intellectual cycle:  33%

This output shows the age of the person as 10717 days. It also indicates that the emotional cycle is at its lowest point (100%).

Source code in the c programming language

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int day(int y, int m, int d) {
    return 367 * y - 7 * (y + (m + 9) / 12) / 4 + 275 * m / 9 + d - 730530;
}

void cycle(int diff, int l, char *t) {
    int p = round(100 * sin(2 * M_PI * diff / l));
    printf("%12s cycle: %3i%%", t, p);
    if (abs(p) < 15)
        printf(" (critical day)");
    printf("\n");
}

int main(int argc, char *argv[]) {
    int diff;

    if (argc < 7) {
        printf("Usage:\n");
        printf("cbio y1 m1 d1 y2 m2 d2\n");
        exit(1);
    }
    diff = abs(day(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]))
             - day(atoi(argv[4]), atoi(argv[5]), atoi(argv[6])));
    printf("Age: %u days\n", diff);
    cycle(diff, 23, "Physical");
    cycle(diff, 28, "Emotional");
    cycle(diff, 33, "Intellectual");
}


gcc -o cbio cbio.c -lm
./cbio 1972 7 11 1943 3 9
Age: 10717 days
    Physical cycle: -27%
   Emotional cycle: -100%


  

You may also check:How to resolve the algorithm Greatest subsequential sum step by step in the Picat programming language
You may also check:How to resolve the algorithm Averages/Mode step by step in the Lasso programming language
You may also check:How to resolve the algorithm File input/output step by step in the SenseTalk programming language
You may also check:How to resolve the algorithm Function composition step by step in the Janet programming language
You may also check:How to resolve the algorithm Ulam spiral (for primes) step by step in the VBScript programming language