Hi everyone. I am doing a USACO training assignment, and the instructions are as follows:

Is Friday the 13th really an unusual event?

That is, does the 13th of the month land on a Friday less often than on any other day of the week? To answer this question, write a program that will compute the frequency that the 13th of each month lands on Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday over a given period of N years. The time period to test will be from January 1, 1900 to December 31, 1900+N-1 for a given number of years, N. N is positive and will not exceed 400.

Note that the start year is NINETEEN HUNDRED, not 1990.

There are few facts you need to know before you can solve this problem:

January 1, 1900 was on a Monday.
Thirty days has September, April, June, and November, all the rest have 31 except for February which has 28 except in leap years when it has 29.
Every year evenly divisible by 4 is a leap year (1992 = 4*498 so 1992 will be a leap year, but the year 1990 is not a leap year)
The rule above does not hold for century years. Century years divisible by 400 are leap years, all other are not. Thus, the century years 1700, 1800, 1900 and 2100 are not leap years, but 2000 is a leap year.
Do not use any built-in date functions in your computer language.

Don't just precompute the answers, either, please.

PROGRAM NAME: friday

INPUT FORMAT

One line with the integer N.
SAMPLE INPUT (file friday.in)

20
OUTPUT FORMAT

Seven space separated integers on one line. These integers represent the number of times the 13th falls on Saturday, Sunday, Monday, Tuesday, ..., Friday.
SAMPLE OUTPUT (file friday.out)

36 33 34 33 35 35 34

I am coding this in Java. Here is my code:

/*
ID: kevlin91
LANG: JAVA
TASK: friday
*/

import java.io.*;
import java.util.*;

public class friday {
    public static void main( String [] args ) throws IOException {
        //BufferedReader br = new BufferedReader( new FileReader( " friday.in " ) );
        //final int numYears = Integer.parseInt( br.readLine( ) );

        int numYears = 20;          // this is just for testing purposes right now
        int [] dayCounts = new int [7];
        int [] month = new int [numYears * 12];
        int [] monthSize = new int [numYears * 12];
        int k = 0;

        for ( int j = 0; j < month.length; j++ ) {           // number the months 1 - 12
            month[j] = ++k;
            if ( k == 12 ) {
                k = 0;          // reset counter to zero after 12 months
            }
        }

        /*for ( int r = 0; r < month.length; r++ ) {
            System.out.println( month[r] );
        }
        daysMonth( month, monthSize );
        getDays( daysMonth( month, monthSize ), dayCounts );
        for ( int t = 0; t < daysMonth( month, monthSize ).length; t++ ) {
            System.out.println( daysMonth( month, monthSize )[t] );
        }*/
        for ( int y = 0; y < getDays( daysMonth( month, monthSize), dayCounts ).length; y++ ) {
            System.out.println( getDays( daysMonth( month, monthSize), dayCounts )[y] );
        }
    }

    public static int[] daysMonth( int [] month, int [] monthSize ) {       // return the number of days in a month
        int a = 0;

        for ( int q = 0; q < month.length; q++ ) {
            if ( ( month[q] == 2 ) && ( ( 1900 + a ) % 4 == 0 ) && ( intAt( ( 1900 + a ), 3 ) != 0 ) )  {           
                monthSize[q] = 29;          // leap year NOT on a century
            }
            else if ( ( month[q] == 2 ) && ( intAt( ( 1900 + a ), 3 ) == 0 ) && ( ( 1900 + a ) % 400 == 0 ) )  {            
                monthSize[q] = 29;          // leap year on a century
            }
            else if ( ( month[q] == 2 ) && ( ( ( 1900 + a ) % ( 400 ) != 0 ) || ( ( 1900 + a ) % ( 4 ) != 0 ) ) ) {
                monthSize[q] = 28;          // february NOT on a leap year
            }
            else if ( month[q] == 1 || month[q] == 3 || month[q] == 5 || month[q] == 7 || month[q] == 8 || month[q] == 10 || month[q] == 12 ) {         
                monthSize[q] = 31;          // months with 31 days
            }
            else if ( month[q] == 4 || month[q] == 6 || month[q] == 9 || month[q] == 11 ) {         
                monthSize[q] = 30;          // months with 30 days
            }
            if ( month[q] == 12 ) {
                a++;            // increment the year every 12 months
            }
        }
        return monthSize;
    }

    public static int[] getDays( int [] daysMonth, int [] dayCounts ) {
        int z = 0;
        int whichDay = 0;
        int dayCounter = 0;
        for ( int r = 0; r < daysMonth.length; r++, whichDay++ ) {
            for ( int i = 0; i < daysMonth[whichDay]; i++ ) {            // loop through the days of each month
                ++dayCounter;
                ++z;
                if ( dayCounter == 13 ) {           // increase the counter for every day (Mon-Sun) the 13th lands on   
                    switch ( z ) {
                        case 1: dayCounts[0]++;
                                break;
                        case 2: dayCounts[1]++;
                                break;
                        case 3: dayCounts[2]++;
                                break;
                        case 4: dayCounts[3]++;
                                break;
                        case 5: dayCounts[4]++;
                                break;
                        case 6: dayCounts[5]++;
                                break;
                        case 7: dayCounts[6]++;
                                break;
                    }
                }   
                if ( z == 7 ) {
                    z = 0;
                }
                if ( dayCounter == daysMonth[whichDay] ) {          // reset day counter once it reachers the lenght of the month
                    dayCounter = 0; 
                }   
            }       
        }
        return dayCounts;
    }

    public static int intAt( int num, int index ) {
        String s = Integer.toString( num );
        int r = Integer.parseInt( s.substring( index, index + 1 ) );
        return r;
    }   
}

When I run my program, the output I am getting is:

68
132
210
280
340
432
462

But now when I print dayCounts instead of getDays(), I get this:

34
33
35
35
34
36
33

like I am supposed to. However, I return dayCounts at the end of getDays(), so shouldn't its value be the same as dayCounts? Where did the weird numbers in getDays() come from?

Thanks.

Recommended Answers

All 3 Replies

Do not use any built-in date functions in your computer language.

What? Why reinvent the wheel?

Every time you call getDays you increment the elements of dayCounts - I can't see any code where its values are reset. Eg the loop counter on line 36 calls getDays each time the loop is iterated, each call incrementing dayCounts.

Ohh...I see now. How could I not have seen that?
Thank you for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.