Task: Five weekends


C++: #include <vector>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <algorithm>
#include <iostream>
#include <iterator>
using namespace boost::gregorian ;

void print( const date &d ) {
   std::cout << d.year( ) << "-" << d.month( ) << "\n" ;
}

int main( ) {
   greg_month longmonths[ ] = {Jan, Mar , May , Jul ,
      Aug , Oct , Dec } ;
   int monthssize = sizeof ( longmonths ) / sizeof (greg_month ) ;
   typedef std::vector<date> DateVector ;
   DateVector weekendmonster ;
   std::vector<unsigned short> years_without_5we_months ;
   for ( unsigned short i = 1900 ; i < 2101 ; i++ ) {
      bool months_found = false ; 
      for ( int j = 0 ; j < monthssize ; j++ ) {
	 date d ( i , longmonths[ j ] , 1 ) ;
	 if ( d.day_of_week( ) == Friday ) {  
	    weekendmonster.push_back( d ) ;
	    if ( months_found == false )
	       months_found = true ;
         }
      }
      if ( months_found == false ) {
	 years_without_5we_months.push_back( i ) ;
      }
   }
   std::cout << "Between 1900 and 2100 , there are " << weekendmonster.size( )
      << " months with 5 complete weekends!\n" ;
   std::cout << "Months with 5 complete weekends are:\n" ;
   std::for_each( weekendmonster.begin( ) , weekendmonster.end( ) , print ) ;
   std::cout <<  years_without_5we_months.size( ) << " years had no months with 5 complete weekends!\n" ;
   std::cout << "These are:\n" ;
   std::copy( years_without_5we_months.begin( ) , years_without_5we_months.end( ) ,
	 std::ostream_iterator<unsigned short>( std::cout , "\n" ) ) ;
   std::cout << std::endl ;
   return 0 ;
}








COBOL:        program-id. five-we.
       data division.
       working-storage section.
       1 wk binary.
        2 int-date pic 9(8).
        2 dow pic 9(4).
        2 friday pic 9(4) value 5.
        2 mo-sub pic 9(4).
        2 months-with-5 pic 9(4) value 0.
        2 years-no-5 pic 9(4) value 0.
        2 5-we-flag pic 9(4) value 0.
         88 5-we-true value 1 when false 0.
       1 31-day-mos pic 9(14) value 01030507081012.
       1 31-day-table redefines 31-day-mos.
        2 mo-no occurs 7 pic 99.
       1 cal-date.
        2 yr pic 9(4).
        2 mo pic 9(2).
        2 da pic 9(2) value 1.
       procedure division.
           perform varying yr from 1900 by 1
           until yr > 2100
               set 5-we-true to false
               perform varying mo-sub from 1 by 1
               until mo-sub > 7
                   move mo-no (mo-sub) to mo
                   compute int-date = function
                       integer-of-date (function numval (cal-date))
                   compute dow = function mod
                       ((int-date - 1) 7) + 1
                   if dow = friday
                       perform output-date
                       add 1 to months-with-5
                       set 5-we-true to true
                   end-if
               end-perform
               if not 5-we-true
                   add 1 to years-no-5
               end-if
           end-perform
           perform output-counts
           stop run
           .

       output-counts.
           display "Months with 5 weekends: " months-with-5
           display "Years without 5 weekends: " years-no-5
           .

       output-date.
           display yr "-" mo
           .
       end program five-we.