You have again been hired to finish a date-related program started by another developer. (It is believed that this developer, after watching one too many online videos about conspiracy theories, suddenly departed late one evening to seek a job with the lizard people who control Denver International Airport.)
The program should prompt the user to enter a month and year, then display a message indicating if the year is a leap year or not, the number of days in the month, and the weekday names of the first and last days of the month:
Enter a month number (Jan=1, Feb=2, etc.): 6
Enter a year with four digits (1600-2400): 1972
The year 1972 is a leap year.
June 1972 has 30 days.
The first day of the month is a Thursday.
The last day of the month is a Friday.
The programmer did not feel it was necessary to create any analysis or design documentation, but did leave behind some partially completed source code (http://cis.scc.losrios.edu/~krofb/cisp301/labs/exam02-code/) . A scrap of paper (http://cis.scc.losrios.edu/~krofb/cisp301/labs/exam02-algorithm/) was also found, listing steps to determine if a year is a leap year or not, which may or may not be helpful to you.
Your task is to complete the program in ways that fulfill the original specifications, and provide some rudimentary design documentation. Note: Do not change any of the modules in the source code provided other than the ones referred to in the list below; do not add or remove any modules, change the name of a module, or change the parameter list of any module - including the name, number, data type, or order of the parameters.
1. Author's Name
Update the source code file to list yourself as the author on Line 2.
2. Month Validation
Modify the input_month module so that it uses an indefinite post-test loop to ensure that the user's input is a number from 1 to 12. (You may assume that the user always types a reasonable integer when prompted.) When the input is out of bounds, an error message is displayed and the user is prompted to reenter the month number:
Enter a month number (Jan=1, Feb=2, etc.): 15
The month must be a number from 1 to 12.
Enter a month number (Jan=1, Feb=2, etc.): 0
The month must be a number from 1 to 12.
Enter a month number (Jan=1, Feb=2, etc.): 6
Enter a year with four digits (1600-2400):
3. Year Validation
Modify the input_year module so that it uses an indefinite pre-test loop to ensure that the user's input is a number from 1600 to 2400. (You may assume that the user always types a reasonable integer when prompted.) When the input is out of bounds, an error message is displayed that indicates if the number was too big or too small, and the user is prompted to reenter the year:
Enter a year with four digits (1600-2400): 1066
The number you've entered is too small!
Enter a year with four digits (1600-2400): 3001
The number you've entered is too big!
Enter a year with four digits (1600-2400): 1972
4. Leap Year Flowchart
Draw a flowchart that correctly implements the is_leap_year module using selection structures. The module should set leap_year to the Boolean value true or false as appropriate for the year provided via the 'in' parameter. (You may assume that the year provided is greater than 1582.) Your flowchart may not make use of any logical operators (AND, OR, NOT), and may not have more than three decision diamonds.
5. Leap Year Code
Implement the is_leap_year module using C/C++ code in a manner that reasonably follows from your flowchart, and subject to the same restrictions (i.e., without using the logical operators && , || , or ! ; and testing no more than three conditions using if or else if ).
6. Leap Year Status
Modify the display_leap_year module to call the module you created in the previous step. Use the value you get back, along with one or more selection structures to display the correct message.
7. Days Per Month
Modify the get_days module so that it uses selection structures to correctly set days to 28, 29, 30 or 31 based on the month and year provided via the 'in' parameters. Your code may not use the switch statement. (Hint: You will need to call another module you've created to help determine certain values; do not duplicate code into your module that appears elsewhere in the file.)
8. Structure Chart
Draw a structure chart for the finished program. If a module is called more than once by different modules, duplicate its box and show it as a distinct child of each caller, rather than having a box with two or more parents. (Hint: The instructor's solution has sixteen module boxes; yours may have a slightly different number depening on how certain modules were implemented.)
days-per-month.cpp
#include < iostream >
#include < string >
void compute_dow(int year, int month, int day, int& dow);
void compute_jdn(int year, int month, int day, int& jdn);
void display_leap_year(int year);
void display_results(int month, int year, int days, int first_weekday,
int last_weekday);
void get_days(int month, int year, int& days);
void get_month_name(int month, std::string& month_name);
void get_weekday_name(int dow, std::string& weekday_name);
void get_weekdays(int month, int year, int& first_weekday, int& last_weekday);
void input_date(int& month, int& year);
void input_month(int& month);
void input_year(int& year);
void is_leap_year(int year, bool& leap_year);
int main()
{
int month; // month entered by user
int year; // year entered by user
int days; // number of days in month entered by user
int first_weekday, // weekday number of first and last days
last_weekday; // of month entered by user
input_date(month, year);
get_days(month, year, days);
get_weekdays(month, year, first_weekday, last_weekday);
display_results(month, year, days, first_weekday, last_weekday);
}
//
// A processing module that computes the weekday number for a given date.
//
void compute_dow(int year, int month, int day, int& dow)
{
int jdn; // helper variable
compute_jdn(year, month, day, jdn);
dow = (jdn + 1) % 7;
}
//
// A processing module that computes the JDN for a given date.
//
void compute_jdn(int year, int month, int day, int& jdn)
{
int a, b, c, d; // helper variables for computing JDN
a = (14 - month) / 12;
b = (month - 3) + (12 * a);
c = 4800 + year - a;
d = c / 4 - c / 100 + c / 400;
jdn = day + ((153 * b) + 2) / 5 + (365 * c) + d - 32045;
}
//
// An output module that displays whether or not the given year is a leap year.
// When given the year 2020, for example, it displays the message "The year 2020
// is a leap year."; but when given the year 2021, it displays the message "The
// year 2021 is not a leap year."
//
void display_leap_year(int year)
{
std::cout << "The year 1972 is not a leap year." << std::endl;
}
//
// An output module that displays the month and year input by the user,
// the number of days in the month, and the weekday names of the first
// and last days of the month.
//
void display_results(int month, int year, int days, int first_weekday,
int last_weekday)
{
std::string month_name; // name of given month
std::string first_weekday_name; // weekday name of first and last days
std::string last_weekday_name; // of given month
get_month_name(month, month_name);
get_weekday_name(first_weekday, first_weekday_name);
get_weekday_name(last_weekday, last_weekday_name);
std::cout << std::endl;
display_leap_year(year);
std::cout << month_name << ' ' << year << " has " << days << " days."
<< std::endl;
std::cout << "The first day of the month is a " << first_weekday_name << "."
<< std::endl;
std::cout << "The last day of the month is a " << last_weekday_name << "."
<< std::endl;
}
//
// A processing module that determines the number of days in a given month.
//
void get_days(int month, int year, int& days)
{
days = 31;
}
//
// A processing module that gets the month name for a given month number.
//
void get_month_name(int month, std::string& month_name)
{
if (month == 1) {
month_name = "January";
}
else if (month == 2) {
month_name = "February";
}
else if (month == 3) {
month_name = "March";
}
else if (month == 4) {
month_name = "April";
}
else if (month == 5) {
month_name = "May";
}
else if (month == 6) {
month_name = "June";
}
else if (month == 7) {
month_name = "July";
}
else if (month == 8) {
month_name = "August";
}
else if (month == 9) {
month_name = "September";
}
else if (month == 10) {
month_name = "October";
}
else if (month == 11) {
month_name = "November";
}
else {
month_name = "December";
}
}
//
// A processing module that gets the weekday name for a given weekday number.
//
void get_weekday_name(int dow, std::string& weekday_name)
{
if (dow == 0) {
weekday_name = "Sunday";
}
else if (dow == 1) {
weekday_name = "Monday";
}
else if (dow == 2) {
weekday_name = "Tuesday";
}
else if (dow == 3) {
weekday_name = "Wednesday";
}
else if (dow == 4) {
weekday_name = "Thursday";
}
else if (dow == 5) {
weekday_name = "Friday";
}
else {
weekday_name = "Saturday";
}
}
//
// A processing module that determines the weekday number of the first and
// last days of the given month.
//
void get_weekdays(int month, int year, int& first_weekday, int& last_weekday)
{
int days; // number of days in given month
get_days(month, year, days);
compute_dow(year, month, 1, first_weekday);
compute_dow(year, month, days, last_weekday);
}
//
// In input module that reads month and year numbers from the user.
//
void input_date(int& month, int& year)
{
input_month(month);
input_year(year);
}
//
// An input module that reads a valid month number (1..12) from the user.
//
void input_month(int& month)
{
std::cout << "Enter a month number (Jan=1, Feb=2, etc.): ";
std::cin >> month;
while ((month < 1) || (month > 12)) {
std::cout << "The month must be a number from 1 to 12. " << std::endl;
std::cout << std::endl;
std::cout << "Enter a month number (Jan=1, Feb=2, etc.): ";
std::cin >> month;
}
}
//
// An input module that reads a valid year number (1600..2400) from the user.
//
void input_year(int& year)
{
do {
std::cout << "Enter a year with four digits (1800-2100): ";
std::cin >> year;
if (year < 1800) {
std::cout << "The number you've entered is too small!" << std::endl;
std::cout << std::endl;
}
else if (year > 2100) {
std::cout << "The number you've entered is too big!" << std::endl;
std::cout << std::endl;
}
} while ((year < 1800) || (year > 2100));
}
//
// A processing module that determines whether the given year is a leap year.
//
void is_leap_year(int year, bool& leap_year)
{
leap_year = true;
}