Project Euler problem 19 - How many Sundays fell on the first

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
5
down vote

favorite
1












Here's the problem:




How many Sundays fell on the first of the month during the twentieth
century (1 Jan 1901 to 31 Dec 2000)?




And here's my code:



#include <stdio.h>

int main(void)
int months[12] = 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ;
int year=1901;
int dayoftheweek=2; //put a 2 because the 1 jan 1901 is tuesday. 0-sunday 6-Saturday
int sunday=0;
int firstsundays=0;

while(year<=2000)
if(year%4==0) //checking if it's a leap year
months[1]=29;

for (int i = 0; i < 12; ++i)
for (int d = 1; d <= months[i]; d++)
if(dayoftheweek==7) //reset the week
dayoftheweek=0;

if(dayoftheweek==sunday && d==1)
firstsundays++;

dayoftheweek++;



months[1]=28;
year++;


printf("There are %d Sundays that fell on the first of the monthn", firstsundays);

return 0;



What could I improve?







share|improve this question



























    up vote
    5
    down vote

    favorite
    1












    Here's the problem:




    How many Sundays fell on the first of the month during the twentieth
    century (1 Jan 1901 to 31 Dec 2000)?




    And here's my code:



    #include <stdio.h>

    int main(void)
    int months[12] = 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ;
    int year=1901;
    int dayoftheweek=2; //put a 2 because the 1 jan 1901 is tuesday. 0-sunday 6-Saturday
    int sunday=0;
    int firstsundays=0;

    while(year<=2000)
    if(year%4==0) //checking if it's a leap year
    months[1]=29;

    for (int i = 0; i < 12; ++i)
    for (int d = 1; d <= months[i]; d++)
    if(dayoftheweek==7) //reset the week
    dayoftheweek=0;

    if(dayoftheweek==sunday && d==1)
    firstsundays++;

    dayoftheweek++;



    months[1]=28;
    year++;


    printf("There are %d Sundays that fell on the first of the monthn", firstsundays);

    return 0;



    What could I improve?







    share|improve this question























      up vote
      5
      down vote

      favorite
      1









      up vote
      5
      down vote

      favorite
      1






      1





      Here's the problem:




      How many Sundays fell on the first of the month during the twentieth
      century (1 Jan 1901 to 31 Dec 2000)?




      And here's my code:



      #include <stdio.h>

      int main(void)
      int months[12] = 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ;
      int year=1901;
      int dayoftheweek=2; //put a 2 because the 1 jan 1901 is tuesday. 0-sunday 6-Saturday
      int sunday=0;
      int firstsundays=0;

      while(year<=2000)
      if(year%4==0) //checking if it's a leap year
      months[1]=29;

      for (int i = 0; i < 12; ++i)
      for (int d = 1; d <= months[i]; d++)
      if(dayoftheweek==7) //reset the week
      dayoftheweek=0;

      if(dayoftheweek==sunday && d==1)
      firstsundays++;

      dayoftheweek++;



      months[1]=28;
      year++;


      printf("There are %d Sundays that fell on the first of the monthn", firstsundays);

      return 0;



      What could I improve?







      share|improve this question













      Here's the problem:




      How many Sundays fell on the first of the month during the twentieth
      century (1 Jan 1901 to 31 Dec 2000)?




      And here's my code:



      #include <stdio.h>

      int main(void)
      int months[12] = 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ;
      int year=1901;
      int dayoftheweek=2; //put a 2 because the 1 jan 1901 is tuesday. 0-sunday 6-Saturday
      int sunday=0;
      int firstsundays=0;

      while(year<=2000)
      if(year%4==0) //checking if it's a leap year
      months[1]=29;

      for (int i = 0; i < 12; ++i)
      for (int d = 1; d <= months[i]; d++)
      if(dayoftheweek==7) //reset the week
      dayoftheweek=0;

      if(dayoftheweek==sunday && d==1)
      firstsundays++;

      dayoftheweek++;



      months[1]=28;
      year++;


      printf("There are %d Sundays that fell on the first of the monthn", firstsundays);

      return 0;



      What could I improve?









      share|improve this question












      share|improve this question




      share|improve this question








      edited Jan 25 at 22:35
























      asked Jan 25 at 22:07









      Alex_n00b

      89111




      89111




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          The loops could be clearer:



          • The outermost loop is actually spread across three lines: int year=1901, while(year<=2000), and year++. It should be written as a for loop.


          • i should be renamed to month or m.

          • The exceptional case for leap years is awkward: you sometimes clobber the February entry at the top of the loop, then reset it at the end. I'd consider such mutations to be a hack that makes the code harder to understand. It also makes the code harder to maintain, since you have hard-coded 29 and 28 in scattered places.

          As for the algorithm itself… you should be able to do better than advancing a day at a time through an entire century. Why not advance a month at a time?



          #include <stdio.h>

          static const int *month_lengths(int year) year % 400 == 0)) ?
          LEAP_YEAR : NON_LEAP_YEAR;


          int main(void)
          int first_sundays = 0;
          int day_of_the_week = 2; // 0 = Sun, 6 = Sat. 1 Jan 1901 was a Tuesday.
          for (int year = 1901; year <= 2000; ++year)
          const int *lengths = month_lengths(year);
          for (int month = 0; month < 12; month++)
          if (day_of_the_week == 0)
          first_sundays++;

          day_of_the_week = (day_of_the_week + lengths[month]) % 7;


          printf("There are %d Sundays that fell on the first of the monthn",
          first_sundays);






          share|improve this answer





















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186008%2fproject-euler-problem-19-how-many-sundays-fell-on-the-first%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            5
            down vote



            accepted










            The loops could be clearer:



            • The outermost loop is actually spread across three lines: int year=1901, while(year<=2000), and year++. It should be written as a for loop.


            • i should be renamed to month or m.

            • The exceptional case for leap years is awkward: you sometimes clobber the February entry at the top of the loop, then reset it at the end. I'd consider such mutations to be a hack that makes the code harder to understand. It also makes the code harder to maintain, since you have hard-coded 29 and 28 in scattered places.

            As for the algorithm itself… you should be able to do better than advancing a day at a time through an entire century. Why not advance a month at a time?



            #include <stdio.h>

            static const int *month_lengths(int year) year % 400 == 0)) ?
            LEAP_YEAR : NON_LEAP_YEAR;


            int main(void)
            int first_sundays = 0;
            int day_of_the_week = 2; // 0 = Sun, 6 = Sat. 1 Jan 1901 was a Tuesday.
            for (int year = 1901; year <= 2000; ++year)
            const int *lengths = month_lengths(year);
            for (int month = 0; month < 12; month++)
            if (day_of_the_week == 0)
            first_sundays++;

            day_of_the_week = (day_of_the_week + lengths[month]) % 7;


            printf("There are %d Sundays that fell on the first of the monthn",
            first_sundays);






            share|improve this answer

























              up vote
              5
              down vote



              accepted










              The loops could be clearer:



              • The outermost loop is actually spread across three lines: int year=1901, while(year<=2000), and year++. It should be written as a for loop.


              • i should be renamed to month or m.

              • The exceptional case for leap years is awkward: you sometimes clobber the February entry at the top of the loop, then reset it at the end. I'd consider such mutations to be a hack that makes the code harder to understand. It also makes the code harder to maintain, since you have hard-coded 29 and 28 in scattered places.

              As for the algorithm itself… you should be able to do better than advancing a day at a time through an entire century. Why not advance a month at a time?



              #include <stdio.h>

              static const int *month_lengths(int year) year % 400 == 0)) ?
              LEAP_YEAR : NON_LEAP_YEAR;


              int main(void)
              int first_sundays = 0;
              int day_of_the_week = 2; // 0 = Sun, 6 = Sat. 1 Jan 1901 was a Tuesday.
              for (int year = 1901; year <= 2000; ++year)
              const int *lengths = month_lengths(year);
              for (int month = 0; month < 12; month++)
              if (day_of_the_week == 0)
              first_sundays++;

              day_of_the_week = (day_of_the_week + lengths[month]) % 7;


              printf("There are %d Sundays that fell on the first of the monthn",
              first_sundays);






              share|improve this answer























                up vote
                5
                down vote



                accepted







                up vote
                5
                down vote



                accepted






                The loops could be clearer:



                • The outermost loop is actually spread across three lines: int year=1901, while(year<=2000), and year++. It should be written as a for loop.


                • i should be renamed to month or m.

                • The exceptional case for leap years is awkward: you sometimes clobber the February entry at the top of the loop, then reset it at the end. I'd consider such mutations to be a hack that makes the code harder to understand. It also makes the code harder to maintain, since you have hard-coded 29 and 28 in scattered places.

                As for the algorithm itself… you should be able to do better than advancing a day at a time through an entire century. Why not advance a month at a time?



                #include <stdio.h>

                static const int *month_lengths(int year) year % 400 == 0)) ?
                LEAP_YEAR : NON_LEAP_YEAR;


                int main(void)
                int first_sundays = 0;
                int day_of_the_week = 2; // 0 = Sun, 6 = Sat. 1 Jan 1901 was a Tuesday.
                for (int year = 1901; year <= 2000; ++year)
                const int *lengths = month_lengths(year);
                for (int month = 0; month < 12; month++)
                if (day_of_the_week == 0)
                first_sundays++;

                day_of_the_week = (day_of_the_week + lengths[month]) % 7;


                printf("There are %d Sundays that fell on the first of the monthn",
                first_sundays);






                share|improve this answer













                The loops could be clearer:



                • The outermost loop is actually spread across three lines: int year=1901, while(year<=2000), and year++. It should be written as a for loop.


                • i should be renamed to month or m.

                • The exceptional case for leap years is awkward: you sometimes clobber the February entry at the top of the loop, then reset it at the end. I'd consider such mutations to be a hack that makes the code harder to understand. It also makes the code harder to maintain, since you have hard-coded 29 and 28 in scattered places.

                As for the algorithm itself… you should be able to do better than advancing a day at a time through an entire century. Why not advance a month at a time?



                #include <stdio.h>

                static const int *month_lengths(int year) year % 400 == 0)) ?
                LEAP_YEAR : NON_LEAP_YEAR;


                int main(void)
                int first_sundays = 0;
                int day_of_the_week = 2; // 0 = Sun, 6 = Sat. 1 Jan 1901 was a Tuesday.
                for (int year = 1901; year <= 2000; ++year)
                const int *lengths = month_lengths(year);
                for (int month = 0; month < 12; month++)
                if (day_of_the_week == 0)
                first_sundays++;

                day_of_the_week = (day_of_the_week + lengths[month]) % 7;


                printf("There are %d Sundays that fell on the first of the monthn",
                first_sundays);







                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Jan 26 at 0:14









                200_success

                123k14143401




                123k14143401






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186008%2fproject-euler-problem-19-how-many-sundays-fell-on-the-first%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Chat program with C++ and SFML

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    Will my employers contract hold up in court?