Write a C function seconds_since_jan1() that takes the time as six integer argum
ID: 3554237 • Letter: W
Question
Write a C function seconds_since_jan1() that takes the time as six integer arguments (year, month, day, hour,
minute, and second) and returns the number of seconds since the beginning of the year. Use numbers 1
through 12 to represent months. Use numbers 0 through 23 to represent hours. Use the year number to
determine the number of days in February.
Use this function to calculate the amount of time in seconds between January 28, 2014, 12:35 pm and May 22,
2014, 1:15 pm.
For each set of integers below, write a single statement that will print a number at random from the set. (Hint:
Learn to use the library function rand().)
(a) 2, 4, 6, 8.
(b) 3, 7, 11, 15, 19
Explanation / Answer
#include <stdio.h>
#include <string.h>
#include<time.h>
//declaring required function that calculates the seconds of the dates provided.The content of the function //has to be written below main
long seconds_since_jan1(int yr,int mnth,int day,int hr,int min,int sec);
//start of program
void main()
{
//declaring second variable to hold the seconds.
long second;
printf("Difference between dates in seconds The difference between January 28, 2014, 12:35 pm and May 22,2014, 1:15 is : ");
//calls the required function with respective dates. Then the seconds which are returned by the function are subtracted and assigned to the "second" vaiable.
second=(seconds_since_jan1(2014,05,22,13,15,0))-(seconds_since_jan1(2014,1,28,12,35,00));
//Prints the "second" variable value
printf("%d",second);
}
//content of the required function. It takes 6 argument/parameters, the year,month,day,hour,min,second
long seconds_since_jan1(int yr,int mnth,int day,int hr,int min,int sec)
{
//checks for leap year and assigns days extra day to feb. Increases days based on the months.
if((yr%4)==0 || (yr%400)==0)
{
day=day+31+29;
}
if(mnth==1)
{
day=day+31;
}
else if(mnth==2)
{
day=day+31+28;
}
else if(mnth==3)
{
day=day+31+28+31;
}
else if(mnth==4)
{
day=day+31+28+31+30;
}
else if(mnth==5)
{
day=day+31+28+31+30+31;
}
else
{
day=day+31+28+31+30+31+30;
}
//Converting hours minutes and second and adding to second
sec=((hr*3600)+(min*360)+sec);
//Converting days and adding to seconds
sec=sec+(day*86400);
//returning seconds
return sec;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.