UNDER CONSTRUCTION

  Lab 00b - DayOfWeek


Description

"Create a C++ program to determine the day of the week given the month, day and year."

Learning Outcomes

By completing the Day-Of-Week Lab, you will have:


Preparation

Install Visual Studio Community 20xx on your computer.

Documentation and installation steps for Windows Visual Studio 20xx can be found here.


How to determine the day of the week

First a brief explanation. In the Gregorian Calendar, over a period of four hundred years, there are 97 leap years and 303 normal years. Each normal year, the day of January 1 advances by one; for each leap year it advances by two. As a result, January 1 year N occurs on the same day of the week as January 1 year N + 400. But, because 7 does not divide 400, January 1 occurs more frequently on some days than others!

The Gregorian calendar was introduced in 1582 in parts of Europe and adopted in 1752 in Great Britain and its colonies. It was later replaced the Julian Calendar which has a four-year cycle of leap years which after four years January 1 has advanced by five days. There is still a 3 day over 10,000 years error which the Gregorian calendar does not take into account. At some time such a correction will have to be done but your software will probably not last that long!

Here is a standard method suitable for calculating the day of the week:

  1. Take the last two digits of the year.
  2. Divide by 4, discarding any fraction.
  3. Add the day of the month.
  4. Add the month's key value: JFM AMJ JAS OND 144 025 036 146
  5. Subtract 1 for January or February of a leap year.
  6. For a Gregorian date, add 0 for 1900's, 6 for 2000's, 4 for 1700's, 2 for 1800's; for other years, add or subtract multiples of 400.
  7. For a Julian date, add 1 for 1700's, and 1 for every additional century you go back.
  8. Add the last two digits of the year.
  9. Divide by 7 and take the remainder.

Now 1 is Sunday, the first day of the week, 2 is Monday, and so on.

The following formula, which is for the Gregorian calendar only, may be more convenient for computer programming. (Note that in some programming languages the remainder operation can yield a negative result if given a negative operand, so mod 7 may not translate to a simple remainder.)

W = (k + [2.6 m - 02] - 2 C + Y + [Y / 4] + [C / 4]) mod 7

where denotes the integer floor function,

  • k is day (1 to 31)
  • m is month (1 = March, ..., 10 = December, 11 = Jan, 12 = Feb) Treat Jan & Feb as months of the preceding year
  • C is century (1987 has C = 19)
  • Y is year (1987 has Y = 87 except Y = 86 for Jan & Feb)
  • W is week day (0 = Sunday, ..., 6 = Saturday)

The century and 400 year corrections are built into the above formula. The [2.6 m - 0.2] term relates to the repetitive pattern that the 30-day months show when March is taken as the first month.

The following short C program works for a restricted range, it returns 0 for Monday, 1 for Tuesday, etc.

dow(m,d,y)
{
   y -= m < 3;
   return (y + y / 4 - y / 100 + y / 400 + "-bed=pen+mad."[m] + d) % 7;
}

(The program appeared was posted by sakamoto@sm.sony.co.jp (Tomohiko Sakamoto) on comp.lang.c on March 10th, 1993.)

https://cs.uwaterloo.ca/~alopez-o/math-faq/node73.html



The Lab

Use the following steps in creating your lab:

  1. Run Visual Studio Community and create an empty project.
  2. Add the following C++ .cpp file (or write your own) to your project:

    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    #ifdef _MSC_VER
    #define _CRTDBG_MAP_ALLOC  
    #include <crtdbg.h>
    #define VS_MEM_CHECK _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
    #else
    #define VS_MEM_CHECK
    #endif
    
    int main(int argc, char* argv[])
    {
    	VS_MEM_CHECK         // enable VS check for memory leaks
    
    	if (argc < 3) return 1;
    	// open argv[1] for input and argv[2] for output
    	ifstream in = ifstream(argv[1]);
    	if (!in) return 1;
    	ofstream out = ofstream(argv[2]);
    	if (!out) return 2;
    
    	// read input file lines
    	for (string line; getline(in, line);)
    	{
    		// read and echo month day year
    		// calculate day of week
    		// output resulting day of the week to output file
    	}
    
    	return 0;
    }

  3. Set breakpoint on return statement to verify message.
  4. Compile and execute.
  5. Zip your day_of_week.cpp file.
  6. Submit the .zip folder and verify auto-grader results.

Auto-grader Results

How to pass arguments to your program:

How to fix memory leaks:

NOTE: THERE IS NO GRADING VALUE FOR THIS LAB!


Helps and Hints

Detecting Memory Leaks.(collapse)

  1. Place the following code near the beginning of the file containing your main function:

    #ifdef _MSC_VER
    #define _CRTDBG_MAP_ALLOC  
    #include <crtdbg.h>
    #define VS_MEM_CHECK _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
    #else
    #define VS_MEM_CHECK
    #endif

  2. Put the following C pre-processor macro at the beginning of your main code:

    int main(int argc, char * argv[])
    {
       VS_MEM_CHECK               // enable memory leak check
       // Your program...
       return 0;
    }

  3. If you get something like the following when your program exits, YOU HAVE A MEMORY LEAK! You are required to remove all memory leaks.

    The thread 0x910 has exited with code 0 (0x0).
    Detected memory leaks!
    Dumping objects ->
    {155} normal block at 0x0000022E97C5B890, 4 bytes long.
     Data: <d   > 64 00 00 00 
    Object dump complete.
    The program '[9324] Hello World.exe' has exited with code 0 (0x0).
    


DayOfWeek Grading Criteria

Instructions for submitting your lab:

Use the following test input and resulting output file in testing your Hello World lab.

Input FileOutput File
Test #1 lab00b_in_01.txt lab00b_out_01.txt

 

The auto-grader will grade your lab as follows:

Fail
Pass
Score1 = 0

 

There is no peer review for this lab. However, if there were a peer review, the following rubric would be used:

No
Partial
Yes
Score = 0
Overall
Rating
Easy to follow, readable, organized, and well commented.
                      
***NOTE: Any attempt to circumvent any lab requirement (ie, hard coding
output, using loops instead of iterators where required, modifying a class interface, etc.) will be reported by peer reviewers and result in a zero score for the lab.
Total Score = 0