What Day Is It? For this lab, you\'ll be filling in values in a struct that you
ID: 3603207 • Letter: W
Question
What Day Is It?
For this lab, you'll be filling in values in a struct that you didn't write, and passing that struct to functions you also didn't write, to get an idea of how structs are used in the real world.
Background:
This is struct tm, designed for holding date and time values:
No need to type it in; it's defined in the C header file #include <time.h>. As you can see, it has some familiar looking components, as well as some odd quirks; why, for example, are the days of a month numbered from 1 to 31, but the months numbered 0 to 11? You simply have to deal with it.
The mktime() function, also defined in #include <time.h>, takes a struct tm (actually it takes the address of a struct tm, so you need to use an ampersand) with some of the fields filled in, and mktime() fills in the rest. If you give mktime() a struct tm with only the year, month, and day fields filled in, mktime() will give it back to you with the day of the week filled in.
Things to keep in mind:
If you mean 1999, it's stored in the tm_year field as 99. The year 2016 is stored as 116.
Sunday is day 0 of the week.
January is month 0 of the year.
Today is the 27th day of the month, and it's stored in tm_mday as 27.
Don't forget to set all the other unused fields in the struct tm to zero before you call mktime() on it, or you might get strange results, because uninitialized variables in a C program take on random values.
Depending on the implementation, struct tm might have a few additional components, such as the name of your time zone, but the standard says it has these nine fields, and they're all int. They can appear in any order but it doesn't matter because you always refer to struct fields by their names.
What you need to do:
Write a C program to use struct tm and a standard C function called mktime() to find out on what day of the week you were born.
Your program should have these features:
Prompt the user for year, month, and day values as integers. Check the ranges of these values to make sure they're reasonable, e.g., 1-12 for the month. (Don't worry about figuring out whether February has 28 or 29 days; that's one of the things mktime() does for you.)
Declare a variable of type struct tm and fill in the appropriate fields for year, month, and day. Don't forget to adjust the year minus 1900 and the month minus 1. Note that to declare a variable of type struct tm, you need to use the keyword struct as in
to declare a struct named datetime. When we defined structs in class we used typedef to avoid the extra usage of struct.
Set all the other fields in the struct tm to zero.
Call mktime() with the address of your struct tm.
Finally, print out the date with the day of the week and the name of the month spelled out in words.
Call the mktime() function in this way:
Like many standard C functions, mktime() uses its return value to indicate when an error has occurred, such as a year that's out of range (struct tm is not guaranteed to work for years before 1900).
Sample Output
User input is in bold
Hints:
Structure your program in three phases:
Get the year, month, and day values from the user.
Declare a struct tm variable and fill in some of its fields, then call mktime() on it.
Print out the result.
Because mktime changes the struct tm variable, you need to put an ampersand in there.
Use an array-of-strings for printing the day of the week, like this:
To access those elements, simply do something like days[0] to access "Sunday". In your program you will be accessing entries from these arrays based upon the values in the struct.
Now do you realize why the tm_wday and tm_mon values are indexed from zero?
Some C Language Trivia:
The mktime() function isn't guaranteed to work for years before 1901.
Things To Try:
Does your program give the correct day of the week for today's date?
What about yesterday?
Explanation / Answer
#include <stdio.h>
#include <time.h>
int main(){
char days[][100] = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
};
char months[][100] = {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
};
int year,month,day;
printf("What year?");
scanf("%d",&year);
printf("What month?");
scanf("%d",&month);
printf("What day of month?");
scanf("%d",&day);
struct tm datetime;
datetime.tm_year = year-1900;
datetime.tm_mon = month-1;
datetime.tm_mday = day;
datetime.tm_sec = 0;
datetime.tm_min = 0;
datetime.tm_hour = 0;
datetime.tm_wday = 0;
datetime.tm_yday = 0;
datetime.tm_isdst = 0;
time_t return_code = mktime(&datetime);
if (return_code == -1) {
printf ("Error: that time value is out of range. ");
return 1;
}
printf("%s %d,%d was a %s",months[datetime.tm_mon],datetime.tm_mday,datetime.tm_year+1900,days[datetime.tm_wday]);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.