Question

In: Computer Science

Write a C++ program which prints consecutive dates to the console window starting at January 1,...

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

  1. 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.

  2. 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.)

Solutions

Expert Solution

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);
}


Related Solutions

Using c++, Write a program to perform the multiplication of 10 consecutive number starting from 5?...
Using c++, Write a program to perform the multiplication of 10 consecutive number starting from 5? Write a program to perform the summation of 10 even number starting from 2? Write a program to perform the summation of 10 odd number starting from 2? Write a program to perform the summation of 10 number starting from 2 and increment is given by user? Write a program to combine all operations from 1 to 4 in a single program using ‘Switch’...
With C code Write a switch statement (not a complete program) which prints an appropriate message...
With C code Write a switch statement (not a complete program) which prints an appropriate message for a letter code entered. Use the following messages: If L is entered, output the message "Lakers" If C is entered, output the message "Clippers" If W is entered, output the message "Warriors" If any other character is entered, output the message "invalid code" Make sure to handle the case where the user enters in a small letter. That is, a capital or small...
Programming in C++ Write a program that prints the values in an array and the addresses...
Programming in C++ Write a program that prints the values in an array and the addresses of the array’s elements using four different techniques, as follows: Array index notation using array name Pointer/offset notation using array name Array index notation using a pointer Pointer/offset notation using a pointer Learning Objectives In this assignment, you will: Use functions with array and pointer arguments Use indexing and offset notations to access arrays Requirements Your code must use these eight functions, using these...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user to enter the three examinations ( test 1, test 2, and test 3), homework, and final project grades then calculate and display the overall grade along with a message, using the selection structure (if/else). The message is based on the following criteria: “Excellent” if the overall grade is 90 or more. “Good” if the overall grade is between 80 and 90 ( not including...
In C#, write a console-based program that asks a user to enter a number between 1...
In C#, write a console-based program that asks a user to enter a number between 1 and 10. Check if the number is between the range and if not ask the user to enter again or quit. Store the values entered in an array. Write two methods. Method1 called displayByVal should have a parameter defined where you pass the value of an array element into the method and display it. Method2 called displayByRef should have a parameter defined where you...
use c++ 1 a)Write a console program that creates an array of size 100 integers. Then...
use c++ 1 a)Write a console program that creates an array of size 100 integers. Then use Fibonacci function Fib(n) to fill up the array with Fib(n) for n = 3 to n = 103: So this means the array looks like: { Fib(3), Fib(4), Fib(5), ...., Fib[102) }. For this part of the assignment, you should first write a recursive Fib(n) function. .For testing, print out the 100 integers. b) For the second part of this assignment, you must...
Write a C program, char.c, which prints the numerical value of the special character constants given...
Write a C program, char.c, which prints the numerical value of the special character constants given below. Don’t just look up the codes and print them directly from your program. You should have your program output the values of the character constants by using them as string literals within your printf() statements. Your output should be presented in a neat, orderly, tabular format as shown below: Char Constant Description Value '\n' newline '\t' horizontal tab '\v' vertical tab '\b' backspace...
Write a c program that prints the final sum of all values from 1 to n...
Write a c program that prints the final sum of all values from 1 to n only when n is a positive value. The program is to print "Poor!" when the final sum is less than 70, print "Good" when the sum is between 71 and 90. or "Great!" when the sum is 91 or better.
Write a program in C++ that prints out the even numbers between 1 and 21 using...
Write a program in C++ that prints out the even numbers between 1 and 21 using WHILE loop. Also, find the sum AND product of these numbers and display the resulting sum and product.
C program, please Write a program that reads a sequence of 10 integer inputs and prints...
C program, please Write a program that reads a sequence of 10 integer inputs and prints the smallest and largest of the inputs and the number of even and odd inputs. for a beginner please, you could use a while loop,if-else,
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT