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

Programming In C Using Arrays Background: Formatting computer data in a way that

ID: 673099 • Letter: P

Question

Programming In C

Using Arrays

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 2015 11 11 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

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

A tabular method to calculate the day of the week

This method is valid for both the Gregorian calendar and the Julian calendar. Britain and its colonies started using the Gregorian calendar on Thursday, September 14, 1752; the previous day was Wednesday, September 2, 1752 (Old Style). The areas now forming the United States adopted the calendar at different times depending on the colonial power: Spain and France had been using it since 1582, while Russia was still using the Julian calendar when Alaska was purchased from it in 1867.

The formula is

where:

d is the day of the month,

m is the month's number in the months table,

y is the last two digits of the year, and

c is the century number. For a Gregorian date, this is 6 if the first two digits of the year are evenly divisible by 4, and

subsequent centuries are 4-2-0 (so the century numbers for 2000, 2100, 2200, and 2300 are respectively 6, 4, 2, and 0). For a Julian date, this is 6 for 1200, and subsequent centuries subtract 1 until 0, when the next century is 6 (so 1300 is 5, and 1100 is 0).

If the result is 0, the date was a Sunday; if 1 it was a Monday, and so on through the week until 6 = Saturday.

Explanation / Answer

#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=0, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec

} months_enum;

typedef enum
{
Saturday=1, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday

} dayOfWeek_enum; //#endif

dates.h

#include "lab6dates.h"
char *get_dayOfWeek(dayOfWeek d )
{

switch ( d )
  {
  case Saturday: return "Saturday";
  case Sunday: return "Sunday";
  case Monday: return "Monday";
  case Tuesday: return "Tuesday";
  case Wednesday: return "Wednesday";
  case Thursday: return "Thursday";
  case Friday: return "Friday";
  default: return "ERROR in day of week value.";

}
printf( "Wednesday is %s. ", getDayOfweek());
}

main.c

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include "lab6dates.h"
#include "dates.h"
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;
}

int getUserInput( &month, &day_of_month, &year )
{

printf("Enter first date (mm/dd/yyyy): ");
scanf("%d/%d/%d", &month.month, &day_of_month, &year);

}

char getDayOfWeek( int month, string day_of_month, int year )
{
    static int t[] = {0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5};

        year=year- month < 3;

    return (year + year/4 - year/100 + year/400 + t[month-1] + day_of_month) % 7;
}

int printHumanFormat( int month, int day_of_month, int year, string dow )
{


}