Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Background: Formatting computer data in a way that’s easy for humans to read is

ID: 673508 • Letter: B

Question

Background: Formatting computer data in a way that’s easy for humans to read is important. Let’s work with the calendar to make sure 20151111 becomes Wednesday, November 11, 2015.

Program Requirements: Write an interactive program to prompt the user for a date in the form Month Day Year (the way Americans seem to prefer it) and output as an easy to read date display. For example: Input: 10 21 2009 Output: Wednesday, October 21, 2009 Input: 11 11 2015 Output: Wednesday, November 11, 2015 Page 2 of 5

Helpful Hints: Create a header file called lab6dates.h and place the following into it ( you may add other things besides what is below, but the items below must be in the header file. ):

#ifndef LAB6DATES_H

#define LAB6DATES_H // these values are used to calculate the day of the week.

#define CENTURY_TABLE_1900_1999_VALUE 0 #define CENTURY_TABLE_2000_2099_VALUE 6

#define MONTH_TABLE_JAN 0 #define MONTH_TABLE_FEB 3

#define MONTH_TABLE_MAR 3 #define MONTH_TABLE_APR 6

#define MONTH_TABLE_MAY 1 #define MONTH_TABLE_JUN 4

#define MONTH_TABLE_JUL 6 #define MONTH_TABLE_AUG 2

#define MONTH_TABLE_SEP 5 #define MONTH_TABLE_OCT 0

#define MONTH_TABLE_NOV 3 #define MONTH_TABLE_DEC 5

typedef enum { jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec } months_enum;

typedef enum { sat, sun, mon, tue, wed, thu, fri } dayOfWeek_enum;

#endif

Remember to write your code is small steps, stopping to compile and test your changes frequently as you go.

Create a function for each enumerated type to return a string representing the value represented by the enumerated type. To get you started, I'll provide the string function for the enumerated type dayOfWeek:

char *get_dayOfWeek( dayOfWeek d )

{

switch ( d )

{

case sat: return "Saturday";

case sun: return "Sunday";

case mon: return "Monday";

case tue: return "Tuesday";

case wed: return "Wednesday";

case thu: return "Thursday";

case fri: return "Friday";

default: return "ERROR in day of week value.";

}

}

You can put those functions in the dates.h file. Include a driver to test it, as with a line like

printf( "Wednesday is %s. ", getDay( wed ) );

Include any drivers you write in the final lab submission.

Use the following driver to test your functions. Your functions should work correctly when exercised with this driver.

int main()

{

months_enum month;

int year;

short day_of_month;

dayOfWeek_enum dow;

getUserInput( &month, &day_of_month, &year );

dow = getDayOfWeek( month, day_of_month, year );

printHumanFormat( month, day_of_month, year, dow );

return EXIT_SUCCESS;

}

How to get the day of the week.

The Wikipedia web page http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#A_tabular_method_to_calculate_the_day_of_the_week

among others, provides details on how to determine the day of the week from any given date.

Basically, it says You don't have to necessarily use the method provided by the Wikipedia page, although I'd strongly recommend it, and I've already included the table information in the header file. There are other ways to calculate it, including using functions available to you in C under Linux, such as strftime( ), but I will leave that to you and Google. Important hint. Desk check your algorithm before writing it in C. If you can't do the calculation by hand, it is unlikely you will be able to program the computer to do it correctly.

Explanation / Answer

The crux of the problem is to find the DAY corresponding to the date given.

I have used an entirely different but very short function to find DAY.

#include <stdio.h>

#include <lab6dates.h>

char* get_dayOfWeek2( int d )

{
switch ( d )
{
   case 0: return "Sunday"; break;
   case 1: return "Monday"; break;
   case 2: return "Tuesday"; break;
   case 3: return "Wednesday"; break;
   case 4: return "Thursday"; break;
   case 5: return "Friday"; break;
   case 6: return "Saturday"; break;
   default: return "ERROR in day of week value.";
}
}

char *get_monOfYear2( int m )

{
switch ( m )
{
   case 1: return "January"; break;
   case 2: return "February"; break;
   case 3: return "March"; break;
   case 4: return "April"; break;
   case 5: return "May"; break;
   case 6: return "June"; break;
   case 7: return "July"; break;
   case 8: return "August"; break;
   case 9: return "September"; break;
   case 10: return "October"; break;
   case 11: return "November"; break;
   case 12: return "December"; break;
   default: return "ERROR in month of year value.";
}
}

int getDayOfWeek(months_enum m, short d, int y )
{
static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
y -= m < 3;
return ( y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}

void printHumanFormat( month, day_of_month, year, dow )
{
   char *day, *mon;
   day = get_dayOfWeek2(dow);
   mon = get_monOfYear2(month);
   printf("%s, %s %d, %d",day,mon,day_of_month,year);  
}

int main()
{
   months_enum month;
   int year;
   short day_of_month;
   dayOfWeek_enum dow;   scanf("%s", &month);
   scanf("%d", &day_of_month);
   scanf("%d", year);
   /* getUserInput( &month, &day_of_month, &year ); */
   dow = getDayOfWeek( month, day_of_month, year );
   printHumanFormat( month, day_of_month, year, dow );
   return 0;
}

--------------------------------

/* lab6dates.h */
#ifndef LAB6DATES_H
#define LAB6DATES_H
// these values are used to calculate the day of the week.
#define CENTURY_TABLE_1900_1999_VALUE 0
#define CENTURY_TABLE_2000_2099_VALUE 6
#define MONTH_TABLE_JAN 0
#define MONTH_TABLE_FEB 3
#define MONTH_TABLE_MAR 3
#define MONTH_TABLE_APR 6
#define MONTH_TABLE_MAY 1
#define MONTH_TABLE_JUN 4
#define MONTH_TABLE_JUL 6
#define MONTH_TABLE_AUG 2
#define MONTH_TABLE_SEP 5
#define MONTH_TABLE_OCT 0
#define MONTH_TABLE_NOV 3
#define MONTH_TABLE_DEC 5
typedef enum { jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec } months_enum;
typedef enum { sat, sun, mon, tue, wed, thu, fri } dayOfWeek_enum;
#endif

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote