Help on Output results

I need help on debugging my code for the third USACO problem. my code is
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <fstream>
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    ifstream fin("friday.in");
    ofstream fout("friday.out");
    int numYear, N, numDays=365, totalDays=1;
    vector<int> monthDays(12);
    vector<int> daysNum(7);
    monthDays[0]=31, monthDays[1]=28, monthDays[2]=31, monthDays[3]=30, monthDays[4]=31, monthDays[5]=30, monthDays[6]=31, monthDays[7]=31, monthDays[8]=30, monthDays[9]=31, monthDays[10]=30, monthDays[11]=31, 
    fin>>N;
    for (int j=0;j<N;j++)
    {
            
        if (N%4==0)
        {
             numDays=366;
             monthDays[1]=29;
             if (N%100==0&&N%400!=0)
             {
        cout<<"\ndays in February is "<<monthDays[1];
                numDays=365;
                monthDays[1]=28;
             }
        }
        else
        {
            numDays=365;
            monthDays[1]=28;
        }
        for (int k=0;k<12;k++)
        {
            for (int l=0;l<monthDays[k];l++)
            {
                if (l==13)
                {
                        int number=0;
                        number=totalDays%7;
                        daysNum[number]++;
                        cout<<"\nSaturday value is"<<daysNum[1]<<" and k is "<<k;
                }
                        totalDays++;
            }
        }
    }
    for (int j=0;j<7;j++)
    {
        fout<<daysNum[j]<<" ";
    }
    fout<<"\n";
    system ("PAUSE");
    return 0;
}

If you f-in 20, you should get 36 33 34 33 35 35 34, but I get 35 34 34 35 34 33 35. Can anyone help me with this?
Third USACO problem:
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 non-negative and will not exceed 400.

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."
So, from 1900 to 1920, confirm that the answers are supposed to be:

Sunday: 36
Monday: 33
Tues: 34
Wed: 33
Thurs: 35
Fri: 35
Sat: 34

Let me know before I post my code.
Last edited on
No, it is
Saturday: 36
Sun: 33
Mon: 34
Tues: 33
Wed: 35
Thurs: 35
Fri: 34

And I would rather just have some help spotting the trouble rather than copying yours, if that is what you are saying.
Last edited on
Fair enough. For starters:

1
2
3
4
5
for (int l=0;l<monthDays[k];l++)
            {
                if (l==13)
// If you started off on day 1, your if (i == 13) statement would be fine.
// But you started it off as 0, so this should read if (i == 12) 


Days: 0 1 2 3 4 5 6 7 8 9 10 11 12 <- this is the 13th day for the month in your program

1
2
for (int j=0;j<N;j++)
// The question says to start at the year 1900, so I find it odd that j = 0 here 


1
2
3
totalDays=1;
...
totalDays++; // look at where this line of code currently is 


1
2
3
4
5
int number=0;
number=totalDays%7;
daysNum[number]++;
// I simplified this into daysNum[totalDays%7];
// one line of code verses 3 = bonus 


Hmm, I think that's it (may have changed something else to get it working - not sure). Fix those things, and it will work - if it doesn't, post your code again and I'll see what I can do.
Last edited on
I'm sorry, but I don't understand the middle 2 (2nd and 3rd) edits. Could you please clarify? I don't know what you mean...
And not to sound rude or anything, but could you please rush your response? I am in kind of a hurry.
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <fstream>
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    int totalDays=1;
    vector<int> monthDays(12);
    vector<int> daysNum(7);
    monthDays[0]=31, monthDays[1]=28, monthDays[2]=31, monthDays[3]=30, monthDays[4]=31, monthDays[5]=30, monthDays[6]=31, 		
		monthDays[7]=31, monthDays[8]=30, monthDays[9]=31, monthDays[10]=30, monthDays[11]=31;
   
    for (int years = 1900; years < 1920; years++) // The years the question wants is from 1900 to 1200
    {
            
        if (years % 4 == 0) // Much easier to read when the variable is called "years" instead of j
		{		
			monthDays[1]=29;	// You didn't really need a variable initialization to tell 
// you how many days in the year there are.  If February has 29 days, then bam, 366, 
// else, 365
             if (years % 100 == 0 && years % 400 != 0)
                monthDays[1]=28;
        }
        else
            monthDays[1]=28;
        for (int month = 0; month < 12; month++)
        {
            for (int day = 0; day < monthDays[month]; day++)
            {
		totalDays++;  // moved this up as well
                if (day == 12) // you either need if day == 13 - 1, or day == 12, since you NEED
// to initialize int day to 0 due to the way arrays work ( [0] being the first value)
                       daysNum[totalDays % 7]++; // instead of initializing int number to 
// totaldays % 7, I skipped the 2 lines of code and just put the number you get inside of 
// daysNum[ in here ]
            }
        }
    }

    for (int j = 0; j < 7; j++)
        cout << daysNum[j] <<" "; // I'm too lazy to open a text file to see the answers
// so I just displayed it to the screen

    cin.get(); // so I can see the answers (you used system pause, but alot of people don't 
// like that for whatever reason, guess its a bad habit to get into)
    return 0;
}


Output:
36 33 34 33 35 35 34

I'll comment what changes I made to your program in a second.
Last edited on
Thanks a lot!
No problem, you should ask earlier next time though, SOMEHOW I found this thread again 6 minutes after your post.
Okay, so I know that I am bothering you a lot, and I feel stupid, but my code only gives me a blank window that does not get to system pause. This is my code...
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <fstream>
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    ifstream fin("friday.in");
    ofstream fout("friday.out");
    int numYear, N, numDays=365, totalDays=1;
    vector<int> monthDays(12);
    vector<int> daysNum(7);
    monthDays[0]=31, monthDays[1]=28, monthDays[2]=31, monthDays[3]=30, monthDays[4]=31, monthDays[5]=30, monthDays[6]=31, monthDays[7]=31, monthDays[8]=30, monthDays[9]=31, monthDays[10]=30, monthDays[11]=31, 
    fin>>N;
    for (int years=1900;years<(years+N);years++)
    {
            
        if (years%4==0)
        {
             numDays=366;
             monthDays[1]=29;
             if (years%100==0&&years%400!=0)
             {
                numDays=365;
                monthDays[1]=28;
             }
        }
        else
        {
            numDays=365;
            monthDays[1]=28;
        }
        for (int k=0;k<12;k++)
        {
            for (int l=0;l<monthDays[k];l++)
            {
                totalDays++;
                if (l==12)
                {
                        daysNum[totalDays%7]++;
                }
            }
        }
    }
    for (int j=0;j<7;j++)
    {
        fout<<daysNum[j]<<" ";
    }
    fout<<"\n";
    system ("PAUSE");
    return 0;
}
What is n initialized to?

Edit: By the way, you aren't bothering me at all, in fact, if you posted 100 questions, I'd be happier than ever.
Last edited on
Thanks, but isn't N f-in-ed and counted as an int in fin>>N; and int numYear, N, numDays=365, totalDays=1;?
Last edited on
Indeed, it reads from the file and stores that value into int N, but I can't see the contents of the text files you're opening :)

Here's your problem:

for (int years = 1900; years < ( years + N ); years++) // problem

for (int years = 1900; years < 1920; years++) // fixed

Alternative:

for (int years = 1900; years < N; years++) // make sure N is initialized to 1920
Last edited on
Okay, so the USACO grading machine spat this out when I entered my program...
> Run 1: Execution error: Your program had this runtime error: Bad
syscall #32000174 (RT_SIGACTION) [email kolstad if you think this
is wrong]. The program ran for 0.000 CPU seconds before the error.
It used 3344 KB of memory.

------ Data for Run 1 ------
1
----------------------------
Can anyone tell me why/what this means?
Sounds like the grading machine doesn't like your use of system ("PAUSE"); - that's the only call to system I can see in your program, so replace it with cin.get(); and you should be fine (I could be wrong though, but try this out)
Last edited on
Oh yeah! I forgot that you can't have any console in or outs in the program. Thanks!
Yes! It worked! I want to thank everyone (and by everyone I mean the only 2 people who helped me in my 2 threads) for helping me!
Haha, nice, congrats :) I'm actually working on the USACO problems myself for fun
Last edited on
FUN? Yeah, I guess that they are a little fun.
Hi. I believe i sent you a pm. please help!
Topic archived. No new replies allowed.