C++ Programming GalleryThe Programming Challenge

Challenge 15 – Display the calendar for the month of a given date in C++

Difficulty x10
x5

This Challenge was proposed for the now defunct C/C++ programming list CWORLD in 1999.
The source code of the challenge has been updated to modern C++, but maintaining the essence of the challenge, which its original author wanted to transmit.

Date: 07-III-2001
Created by: Nyder Menéndez
Challenge winner: Antonio González Cruz

Well… the challenge I propose… is to develop… different logics to obtain the output on the screen… (at least the one that is shown…)

The screen output would be something like:

day= 17

month= 3

year= 1960


        March
 Mo Tu We Th Fr Sa Su
     1  2  3  4  5  6
  7  8  9 10 11 12 13
 14 15 16 17 18 19 20
 21 22 23 24 25 26 27
 28 29 30 31

I ask you to use (I know you will…)… all the creativity you can… and you can even send more than one solution…

I am giving you… a way to get the day of the week now… (free 🙂 in numerical form… to be able to use it… in the logic…

Everything is valid… (libraries, functions, etc…) but as usual… portability… can be an evaluation parameter… (although for me “portability” is something new… 🙂

The program must work for any date… day, month and year… (I’ll give you the way to do it now…) but if you want to use yours… and this one works… then welcome…

For those who don’t know it… leap year is a “Macro… with parameters”… (at least I think that’s what it’s called… 🙂

In order to present this challenge… I had to modify the original little program that I had made… (I removed something like 95%)… From input validation… etc. etc…

I hope it’s useful… and that it helps you learn…

Any questions… or additional clarifications… I’ll be happy to answer them…

Good luck…

Conditions

  • Your custom code can only be included in the dotted section (…)
  • You can add as many includes as you need in the #include <whatever you want… or need…> section

Source Code

#include <iostream>
#include <string>
using std::cout;
using std::cin;

#include <whatever you want... or need...>

#define leap_year(bs)    (!(bs%4)&&(bs%100)||!(bs%400))

int main()
{
	int year, month, day, dw, monthd;

	// Variable containing the months of the year for the calendar.
	std::string monthc[12] = {	"January", "February", "March",
								"April", "May", "June", "July",
								"August", "September", "October", "November",
								"December" };

	// Date Entry
	cout << "\nday= ";
	cin >> day;
	cout << "\nmonth= "; 
	cin >> month;
	cout << "\nyear= ";
	cin >> year;

	// Calculation of the day of the week for the entered date
	// Monday = 0 ... Sunday = 6
	dw = (((1461 * ((year + 4800) + ((month - 14) / 12))) >> 2)			
		+ ((367 * (month - 2 - 12 * ((month - 14) / 12))) / 12)			
		- (3 * (((year + 4800) + ((month - 14) / 12) + 100) / 100) >> 2)
		+ day - 32075L) % 7;				

	// Calculation of the number of days in the month...
	switch (month)
	{
	case 1:case 3:case 5:case 7:case 8:case 10:
	case 12:monthd = 31; break;
	case 4:case 6:case 9:case 11:monthd = 30; break;
	case 2:monthd = 28; if (leap_year(year)) monthd = 29; break;
	}

	cout << "\n\n\t" << monthc[month - 1]; // Printing the Title of the month.
	cout << "\n Mo Tu We Th Fr Sa Su\n";	 // Headings of the days.

	...

	return 0;
}

AND FINALLY I CAN ONLY SAY …. BEST OF LUCK AND MAY THE BEST CODE WIN….

If you liked this challenge remember to leave a comment in our guestbook

Solution

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button