Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First, that <code>leap</code> function feels overly complicated; you don't need to do both dates in one function call, and I'm sure that can be written more succinctly so that it is more obviously correct. Here's a version I've got laying around that isn't <em>succinct</em> but I'm confident it is easy to check the logic:</p> <pre><code>int is_leap_year(int year) { if (year % 400 == 0) { return 1; } else if (year % 100 == 0) { return 0; } else if (year % 4 == 0) { return 1; } else { return 0; } } </code></pre> <p>You could call it like this:</p> <pre><code>int year1, year2, leap1, leap2; year1 = get_input(); year2 = get_input(); leap1 = is_leap_year(year1); leap2 = is_leap_year(year2); </code></pre> <p>No pointers and significantly less code duplication. Yes, I know that <code>is_leap_year()</code> can be reduced to a single <code>if(...)</code> statement, but this is easy for me to read.</p> <p>Second, I think you're got a mismatch between 0-indexed arrays and 1-indexed human months:</p> <pre><code> if(*month1 &lt; 1 || *month1 &gt; 12) </code></pre> <p>vs</p> <pre><code> int daysPerMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31}; </code></pre> <p>Third, I think that days per month can be calculated slightly nicer:</p> <pre><code>int days_in_month(int month, int year) { int leap = is_leap_year(year); /* J F M A M J J A S O N D */ int days[2][12] = {{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}}; if (month &lt; 0 || month &gt; 11 || year &lt; 1753) return -1; return days[leap][month]; } </code></pre> <p>Here, I assume January is 0; you would need to force the rest of the code to match. (I learned this double-array trick from <a href="http://en.wikipedia.org/wiki/The_Elements_of_Programming_Style">The Elements of Programming Style</a> (<a href="http://books.google.com/books?ei=jzeXT--_OYeYiAKJjuX6Dw&amp;id=-_QmAAAAMAAJ&amp;dq=%22the+elements+of+programming+style%22&amp;q=leap+year#search_anchor">page 54</a>).) The best part of using a routine like this is that it removes the leap condition from the difference calculation.</p> <p>Fourth, you're indexing arrays outside their bounds:</p> <pre><code> for(i = month1 + 1; i &lt;= 12; i++) { if(leap1 == 1) total += daysPerMonthLeap[i]; </code></pre> <p>This is just another instance of the problem with 0-indexed arrays and 1-indexed months -- but be sure that you fix <em>this</em>, too, when you fix the months.</p> <p>I have a fear that I haven't yet found all the issues -- you may find it easier to <em>sort</em> the first and the second date after input and remove all that validation code -- and then use names <code>before</code> and <code>after</code> or something to give names that are easier to think through in the complicated core of the calculation.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload