Debugging Help

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
#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=0;
    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++;
             monthDays[1]=29;
             if (N/100==0&&N/400!=0)
             {
                numDays--;
                monthDays[1]=28;
             }
        }
        for (int k=0;k<12;k++)
        {
            for (int l=0;l<monthDays[k];l++,totalDays++)
            {
                cout<<"\nl is "<<l<<"\ntotalDays is "<<totalDays;
                if (l=13)
                {
                         cout<<"\nl is"<<l;
                        int number=0;
                        number=totalDays%7;
                        daysNum[number]++;
                }
            }
        }
    }
    for (int j=0;j<7;j++)
    {
        fout<<daysNum[j]<<" ";
    }
    fout<<"\n";
    system ("PAUSE");
    return 0;
}

This program is for the third USACO problem, where you find how many of each day in a certain number of years the thirteenth is in to see if Friday the thirteenth is common. In the underlined area, the thirteenth run runs infinitely through the if statement and the fourteenth run goes infinitely and does not go past the if statement. I can't figure out why this is doing this, because it is not supposed to infinitely repeat. Can anyone help me?!?!
At line 35
if(l==13)
Last edited on
Also, at line 32,
for (int l=0;l<monthDays[k];l++,totalDays++)

iftotalDays++ has nothing to do with loop control, please refrain yourself from putting in the loop control.
I'm sorry, but I still don't understand what is wrong with line 35
if i=35 You are passing a value to a variable
ifi==35 You are comparing something
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
59
#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=0;
    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++)
    {
        cout<<"\nN is "<<N;
            
        if (N%4==0)
        {
             numDays=366;
             monthDays[1]=29;
             if (N/100==0&&N/400!=0)
             {
                numDays=365;
                monthDays[1]=28;
             }
        }
        else
        {
            numDays=365;
            monthDays[1]=28;
        cout<<"\ndays in February is "<<monthDays[1];
        }
        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]++;
                        totalDays++;
                        cout<<"\n the first number for the second day is "<<daysNum[1]<<" and k is "<<k<<" and j is "<<j;
                }
            }
        }
    }
    for (int j=0;j<7;j++)
    {
        fout<<daysNum[j]<<" ";
    }
    fout<<"\n";
    system ("PAUSE");
    return 0;
}

Well, this is my finished code, but the end results are wrong. It inputs 20 for N and outputs 35 35 34 34 34 34 instead of 36 33 34 33 35 35 34. The best that I can figure out is that it justs adds one to each month for every 13th. Can anyone tell me how to fix it?
This is my new code. It still has some problems because if you input 20, you get 35 35 34 34 34 34, when you should get 36 33 34 33 35 35 34. Can anyone help me debug this?
Wait, this is my new 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
55
56
57
58
59
#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=0;
    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++)
    {
        cout<<"\nN is "<<N;
            
        if (N%4==0)
        {
             numDays=366;
             monthDays[1]=29;
             if (N/100==0&&N/400!=0)
             {
                numDays=365;
                monthDays[1]=28;
             }
        }
        else
        {
            numDays=365;
            monthDays[1]=28;
        cout<<"\ndays in February is "<<monthDays[1];
        }
        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]++;
                        totalDays++;
                        cout<<"\n the first number for the second day is "<<daysNum[1]<<" and k is "<<k<<" and j is "<<j;
                }
            }
        }
    }
    for (int j=0;j<7;j++)
    {
        fout<<daysNum[j]<<" ";
    }
    fout<<"\n";
    system ("PAUSE");
    return 0;
}
Topic archived. No new replies allowed.