In: Computer Science
Write a C++ program which prints consecutive dates to the console window starting at January 1, 2000, as follows.
Saturday, January 1, 2000 Sunday, January 2, 2000 Monday, January 3, 2000 Tuesday, January 4, 2000 ...
You will define a function which prints the dates indefinitely, exactly as given above, or prints a specific number of dates. For example, if your function prints 7,581 days the result would look like this.
Saturday, January 1, 2000 Sunday, January 2, 2000 ...
Thursday, October 1, 2020 Friday, October 2, 2020
Your program must print every date, of course, so the console output above should be 7,581 lines.
Requirements
Your program must have exactly three files with exact names date functions.cpp, date functions.h, and main.cpp. All functions must be declared in date functions.h and defined in date functions.cpp. Your main.cpp must include main() and consist of any tests you wish. I will not check your main(), but you must submit it nonetheless.
You may define as many helper functions as you wish in date functions.cpp. How- ever, there are two required functions,
and
void print consecutive dates(int num, int delay ms); bool is leap year(int year);
The former prints num consecutive dates (or prints indefinitely if num is 0) with a de- lay of delay ms milliseconds in between each date (with no delay if delay ms is 0). You must define is leap year as its name describes, and define print consecutive dates to use the function is leap year in a meaningful way.
3. You can use cout in the function print consectutive dates, but you may not use it anywhere else in your program, except main(). You may not use cin anywhere in
your program except main(). (Feel free to use cin/cout anywhere in your code when testing, but your final submission must follow the aforementioned rules.)
Given below is the code for the question. Please do rate the answer if it helped. Thank you.
date_functions.h
----------------
#include <iostream>
void print_consecutive_dates(int num, int delay_ms);
bool is_leap_year(int year);
void sleepFor(int delay_ms);
date_functions.cpp
------------------
#include "date_functions.h"
#include <time.h>
using namespace std;
void print_consecutive_dates(int num, int delay_ms)
{
int maxDays[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30,
31, 30, 31};
string months[12] = {"January", "February", "March",
"April", "May", "June", "July", "August", "September", "October",
"November", "December"};
string weekdays[7] = {"Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday","Sunday"};
int day = 1, month = 1, year = 2000, weekday =
6;
for(int i = 1; i <= num; i++){
cout << weekdays[weekday-1]
<< ", " << months[month-1] << " " << day
<< ", " << year << endl;
day++;
if(day >
maxDays[month-1]){
day = 1;
month++;
if(month >
12){
month = 1;
year++;
//set correct days for february
if(is_leap_year(year))
maxDays[1] = 29;
else
maxDays[1]= 28;
}
}
weekday++;
if(weekday > 7)
weekday =
1;
sleepFor(delay_ms);
}
}
bool is_leap_year(int year)
{
if(year % 400 == 0)
return true;
else if(year % 4 == 0 && year % 100 !=
0)
return true;
else
return false;
}
void sleepFor(int delay_ms) {
clock_t end_time;
end_time = clock() + delay_ms *
CLOCKS_PER_SEC/1000;
while (clock() < end_time) {
//loop for waiting
}
}
main.cpp
========
#include "date_functions.h"
#include <iostream>
using namespace std;
int main(){
print_consecutive_dates(7581,0);
}