How to resolve the algorithm Averages/Mean time of day step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Averages/Mean time of day step by step in the C programming language

Table of Contents

Problem Statement

A particular activity of bats occurs at these times of the day: Using the idea that there are twenty-four hours in a day, which is analogous to there being 360 degrees in a circle, map times of day to and from angles; and using the ideas of Averages/Mean angle compute and show the average time of the nocturnal activity to an accuracy of one second of time.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Averages/Mean time of day step by step in the C programming language

The provided C program calculates the mean time from a set of input times. It does this by converting each time to an angle, finding the mean angle, and then converting the mean angle back to a time.

Here's a detailed breakdown of the code:

  1. The program includes the necessary header files:
  • <stdlib.h> for dynamic memory allocation
  • <math.h> for mathematical functions
  • <stdio.h> for input and output
  1. The following type definition creates a structure called digitime to represent time in hours, minutes, and seconds:
typedef struct
{
 int hour, minute, second;
} digitime;
  1. The timeToDegrees function converts a digitime struct to an angle in degrees:
double
timeToDegrees (digitime time)
{
 return (360 * time.hour / 24.0 + 360 * time.minute / (24 * 60.0) +
         360 * time.second / (24 * 3600.0));
}
  1. The timeFromDegrees function converts an angle in degrees to a digitime struct:
digitime
timeFromDegrees (double angle)
{
 digitime d;
 double totalSeconds = 24 * 60 * 60 * angle / 360;

 d.second = (int) totalSeconds % 60;
 d.minute = ((int) totalSeconds % 3600 - d.second) / 60;
 d.hour = (int) totalSeconds / 3600;

 return d;
}
  1. The meanAngle function calculates the mean angle from an array of angles:
double
meanAngle (double *angles, int size)
{
 double y_part = 0, x_part = 0;
 int i;

 for (i = 0; i < size; i++)
   {
     x_part += cos (angles[i] * M_PI / 180);
     y_part += sin (angles[i] * M_PI / 180);
   }

 return atan2 (y_part / size, x_part / size) * 180 / M_PI;
}
  1. The main function:
  • Declares an array of digitime structs called set, a digitime struct called meanTime, an integer inputs, an integer i, an array of doubles called angleSet, and a double angleMean.
  • Prompts the user to enter the number of inputs.
  • Allocates memory for set and angleSet using malloc.
  • Prompts the user to enter the time values in "hours: minutes: seconds" format.
  • Calls timeToDegrees to convert each time to an angle and stores the angle in angleSet.
  • Calls meanAngle to calculate the mean angle from angleSet.
  • Calls timeFromDegrees to convert the mean angle back to a digitime struct stored in meanTime.
  • Prints the mean time to the console in "hours: minutes: seconds" format.

In summary, the program takes a set of time values, converts them to angles, calculates the mean angle, and converts the mean angle back to a time value, which is then displayed as the mean time.

Source code in the c programming language

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

typedef struct
{
  int hour, minute, second;
} digitime;

double
timeToDegrees (digitime time)
{
  return (360 * time.hour / 24.0 + 360 * time.minute / (24 * 60.0) +
          360 * time.second / (24 * 3600.0));
}

digitime
timeFromDegrees (double angle)
{
  digitime d;
  double totalSeconds = 24 * 60 * 60 * angle / 360;

  d.second = (int) totalSeconds % 60;
  d.minute = ((int) totalSeconds % 3600 - d.second) / 60;
  d.hour = (int) totalSeconds / 3600;

  return d;
}

double
meanAngle (double *angles, int size)
{
  double y_part = 0, x_part = 0;
  int i;

  for (i = 0; i < size; i++)
    {
      x_part += cos (angles[i] * M_PI / 180);
      y_part += sin (angles[i] * M_PI / 180);
    }

  return atan2 (y_part / size, x_part / size) * 180 / M_PI;
}

int
main ()
{
  digitime *set, meanTime;
  int inputs, i;
  double *angleSet, angleMean;

  printf ("Enter number of inputs : ");
  scanf ("%d", &inputs);
  set = malloc (inputs * sizeof (digitime));
  angleSet = malloc (inputs * sizeof (double));
  printf ("\n\nEnter the data separated by a space between each unit : ");

  for (i = 0; i < inputs; i++)
    {
      scanf ("%d:%d:%d", &set[i].hour, &set[i].minute, &set[i].second);
      angleSet[i] = timeToDegrees (set[i]);
    }

  meanTime = timeFromDegrees (360 + meanAngle (angleSet, inputs));

  printf ("\n\nThe mean time is : %d:%d:%d", meanTime.hour, meanTime.minute,
          meanTime.second);
  return 0;
}


  

You may also check:How to resolve the algorithm Environment variables step by step in the E programming language
You may also check:How to resolve the algorithm Unicode variable names step by step in the ACL2 programming language
You may also check:How to resolve the algorithm Draw a cuboid step by step in the Zig programming language
You may also check:How to resolve the algorithm Jaro-Winkler distance step by step in the Java programming language
You may also check:How to resolve the algorithm I before E except after C step by step in the J programming language