Description
The purpose of this challenge is to use arrays and functions. This challenge calculates the number of days before and after Christmas.
Requirements
- Write a function int date_compare(int month, int day, int anchor_month, int anchor_day) that will return an integer. month and day is compared against anchor_month and anchor_day. The function returns -1 if month and day is before anchor_month and anchor_day, 0 (zero) if they are the same day, and 1 (one) if it is after.
- Write a function int days_diff(int m1, int d1, int m2, int d2).
int days_diff(int m1, int d1, int m2, int d2) { int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; }
- The days_diff() function will determine and return the number of days between m1 d1 and m2 d2. Use the array to write an algorithm that will perform this determination.
- Always assume that both dates represented by m1 d1 and m2 d2 are within the same year.
- Don’t worry about leap years – just assume that February has 28 days.
- In main, ask the user to enter two integers to represent month and day
- Call days_diff() from main() to determine the number of days between the month and day entered by the user and Christmas day.
- In main, call date_compare() function to determine how the entered month and day compares to Christmas (December 25). Use the return value of date_compare() function call to show a message, either “x more days until Christmas” or “It’s Christmas” or “It’s X days after Christmas” where x is the actual number of days returned by days_diff().
DO NOT USE
Any built-in date/time functions
Sample main()
int main() { int month, day; int compare, days; cout << "Enter month and day: "; cin >> month >> day; // determine which day is earlier compare = date_compare(month, day, 12, 25); // determine how many days from (or after) christmas days = date_diff(month, day, 12, 25); // show message if (compare == 0) cout << "It's Christmas" << endl; else if (compare == -1) cout << days << " more days until Christmas" << endl; else if (compare == 1) cout << "It's " << days << " days after Christmas" << endl; return 0; }
Sample Interaction / Output
Enter a month and day: 12 24 1 more day until Christmas [run it again] Enter a month and day: 12 1 24 more days until Christmas [run it again] Enter a month and day: 12 25 It's Christmas! [run it again] Enter a month and day: 12 31 It's 6 days after Christmas! [run it again] Enter a month and day: 9 30 86 more days until Christmas! [run it again] Enter a month and day: 1 1 358 more days until Christmas!
LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT
CATALOG ID: CPP-CHAL0040
Print Requirements