How to resolve the algorithm Last Friday of each month step by step in the C++ programming language
Published on 7 June 2024 03:52 AM
How to resolve the algorithm Last Friday of each month step by step in the C++ programming language
Table of Contents
Problem Statement
Write a program or a script that returns the date of the last Fridays of each month of a given year. The year may be given through any simple input method in your language (command line, std in, etc).
Example of an expected output:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Last Friday of each month step by step in the C++ programming language
Both code snippets demonstrate how to find the last Friday in each month of a given year. However, they use different libraries and approaches.
First Code Snippet (Boost Library):
- This code uses the Boost Date and Time Library, which provides a comprehensive set of classes and functions for working with dates and times.
- It creates an array of
greg_month
enums representing the 12 months of the year. - It parses a year as an integer from the command-line argument.
- It iterates through the months and uses the
last_day_of_the_week_in_month
class to calculate the date of the last Friday in each month. - It prints the calculated dates using the
operator<<
overload for thedate
class.
Second Code Snippet (C++20 Library):
- This code uses the C++20 Standard Library, which includes support for time and date manipulation.
- It uses the
std::chrono
namespace to work with dates and times. - It iterates through the months of the year from January to December.
- For each month, it calculates the number of days using the
last
manipulator, which returns the last day of the month. - It then uses a loop to find the last Friday by decrementing the
std::chrono::year_month_day
object representing the last day of the month until it encounters a Friday. - It prints the calculated dates using the
operator<<
overload for thestd::chrono::year_month_day
class.
Both code snippets accomplish the task of finding the last Friday in each month of a given year. However, the first snippet relies on an external library, while the second one uses the standard library, which may be more convenient in some cases.
Source code in the cpp programming language
#include <boost/date_time/gregorian/gregorian.hpp>
#include <iostream>
#include <cstdlib>
int main( int argc , char* argv[ ] ) {
using namespace boost::gregorian ;
greg_month months[ ] = { Jan , Feb , Mar , Apr , May , Jun , Jul ,
Aug , Sep , Oct , Nov , Dec } ;
greg_year gy = atoi( argv[ 1 ] ) ;
for ( int i = 0 ; i < 12 ; i++ ) {
last_day_of_the_week_in_month lwdm ( Friday , months[ i ] ) ;
date d = lwdm.get_date( gy ) ;
std::cout << d << std::endl ;
}
return 0 ;
}
#include <chrono>
#include <iostream>
int main() {
std::cout << "The dates of the last Friday in each month of 2023:" << std::endl;
for ( unsigned int m = 1; m <= 12; ++m ) {
std::chrono::days days_in_month = std::chrono::sys_days{std::chrono::year{2023}/m/std::chrono::last}
- std::chrono::sys_days{std::chrono::year{2023}/m/1} + std::chrono::days{1};
const unsigned int last_day = days_in_month / std::chrono::days{1};
std::chrono::year_month_day ymd{std::chrono::year{2023}, std::chrono::month{m}, std::chrono::day{last_day}};
while ( std::chrono::weekday{ymd} != std::chrono::Friday ) {
ymd = std::chrono::sys_days{ymd} - std::chrono::days{1};
}
std::cout << ymd << std::endl;
}
}
You may also check:How to resolve the algorithm Repeat a string step by step in the Delphi programming language
You may also check:How to resolve the algorithm Symmetric difference step by step in the Maxima programming language
You may also check:How to resolve the algorithm Multifactorial step by step in the CLU programming language
You may also check:How to resolve the algorithm Function definition step by step in the Oberon-2 programming language
You may also check:How to resolve the algorithm Binary search step by step in the PL/I programming language