TheAlgorithms-C/dd/df0/problem__19_2sol1_8c.html

300 lines
22 KiB
HTML
Raw Normal View History

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.18"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C: project_euler/problem_19/sol1.c File Reference</title>
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../resize.js"></script>
<script type="text/javascript" src="../../navtreedata.js"></script>
<script type="text/javascript" src="../../navtree.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">Algorithms_in_C
&#160;<span id="projectnumber">1.0.0</span>
</div>
<div id="projectbrief">Set of algorithms implemented in C.</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.18 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
/* @license-end */
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
initMenu('../../',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('dd/df0/problem__19_2sol1_8c.html','../../'); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="header">
<div class="summary">
<a href="#func-members">Functions</a> </div>
<div class="headertitle">
<div class="title">sol1.c File Reference</div> </div>
</div><!--header-->
<div class="contents">
<p><a href="https://projecteuler.net/problem=19">Problem 19</a> solution
<a href="#details">More...</a></p>
<div class="textblock"><code>#include &lt;stdio.h&gt;</code><br />
</div><div class="textblock"><div class="dynheader">
Include dependency graph for sol1.c:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../dd/d24/problem__19_2sol1_8c__incl.svg" width="158" height="127"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:ab7f9ad087f124b8e0615aa535b4c8a75"><td class="memItemLeft" align="right" valign="top">char&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../dd/df0/problem__19_2sol1_8c.html#ab7f9ad087f124b8e0615aa535b4c8a75">get_month_days</a> (short month)</td></tr>
<tr class="separator:ab7f9ad087f124b8e0615aa535b4c8a75"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a6561b1adc8a19c092679b9874da24e2e"><td class="memItemLeft" align="right" valign="top">char&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../dd/df0/problem__19_2sol1_8c.html#a6561b1adc8a19c092679b9874da24e2e">is_leap_year</a> (short year)</td></tr>
<tr class="separator:a6561b1adc8a19c092679b9874da24e2e"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a3c04138a5bfe5d72780bb7e82a18e627"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../dd/df0/problem__19_2sol1_8c.html#a3c04138a5bfe5d72780bb7e82a18e627">main</a> (int argc, char **argv)</td></tr>
<tr class="separator:a3c04138a5bfe5d72780bb7e82a18e627"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p><a href="https://projecteuler.net/problem=19">Problem 19</a> solution </p>
<dl class="section author"><dt>Author</dt><dd><a href="https://github.com/kvedala">Krishna Vedala</a> </dd></dl>
</div><h2 class="groupheader">Function Documentation</h2>
<a id="ab7f9ad087f124b8e0615aa535b4c8a75"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ab7f9ad087f124b8e0615aa535b4c8a75">&#9670;&nbsp;</a></span>get_month_days()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">char get_month_days </td>
<td>(</td>
<td class="paramtype">short&#160;</td>
<td class="paramname"><em>month</em></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>returns number of days in a month. Month is identified by an integer -<br />
</p><blockquote class="doxtable">
<p>0 = Jan and 11 = December<br />
</p>
</blockquote>
<p>For February, adjust for leap year outside the function. </p>
<div class="fragment"><div class="line"><a name="l00015"></a><span class="lineno"> 15</span>&#160;{</div>
<div class="line"><a name="l00016"></a><span class="lineno"> 16</span>&#160; <span class="keywordflow">if</span> (month == 1) <span class="comment">/* February has 28 days. Adjust leap year in the loop */</span></div>
<div class="line"><a name="l00017"></a><span class="lineno"> 17</span>&#160; <span class="keywordflow">return</span> 28;</div>
<div class="line"><a name="l00018"></a><span class="lineno"> 18</span>&#160; <span class="keywordflow">else</span> <span class="keywordflow">if</span> (month &lt;= 6) <span class="comment">/* odd months till July have 30 days - Jan = 0 (even)*/</span></div>
<div class="line"><a name="l00019"></a><span class="lineno"> 19</span>&#160; {</div>
<div class="line"><a name="l00020"></a><span class="lineno"> 20</span>&#160; <span class="keywordflow">if</span> (month &amp; 0x01)</div>
<div class="line"><a name="l00021"></a><span class="lineno"> 21</span>&#160; <span class="keywordflow">return</span> 30;</div>
<div class="line"><a name="l00022"></a><span class="lineno"> 22</span>&#160; <span class="keywordflow">else</span></div>
<div class="line"><a name="l00023"></a><span class="lineno"> 23</span>&#160; <span class="keywordflow">return</span> 31;</div>
<div class="line"><a name="l00024"></a><span class="lineno"> 24</span>&#160; }</div>
<div class="line"><a name="l00025"></a><span class="lineno"> 25</span>&#160; <span class="keywordflow">else</span> <span class="keywordflow">if</span> (month &gt;= 7) <span class="comment">/* odd months after July have 31 days*/</span></div>
<div class="line"><a name="l00026"></a><span class="lineno"> 26</span>&#160; {</div>
<div class="line"><a name="l00027"></a><span class="lineno"> 27</span>&#160; <span class="keywordflow">if</span> (month &amp; 0x01)</div>
<div class="line"><a name="l00028"></a><span class="lineno"> 28</span>&#160; <span class="keywordflow">return</span> 31;</div>
<div class="line"><a name="l00029"></a><span class="lineno"> 29</span>&#160; <span class="keywordflow">else</span></div>
<div class="line"><a name="l00030"></a><span class="lineno"> 30</span>&#160; <span class="keywordflow">return</span> 30;</div>
<div class="line"><a name="l00031"></a><span class="lineno"> 31</span>&#160; }</div>
<div class="line"><a name="l00032"></a><span class="lineno"> 32</span>&#160; <span class="comment">/* should never reach here! */</span></div>
<div class="line"><a name="l00033"></a><span class="lineno"> 33</span>&#160; perror(<span class="stringliteral">&quot;Should never have reached this point!\n&quot;</span>);</div>
<div class="line"><a name="l00034"></a><span class="lineno"> 34</span>&#160; <span class="keywordflow">return</span> -1;</div>
<div class="line"><a name="l00035"></a><span class="lineno"> 35</span>&#160;}</div>
</div><!-- fragment -->
</div>
</div>
<a id="a6561b1adc8a19c092679b9874da24e2e"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a6561b1adc8a19c092679b9874da24e2e">&#9670;&nbsp;</a></span>is_leap_year()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">char is_leap_year </td>
<td>(</td>
<td class="paramtype">short&#160;</td>
<td class="paramname"><em>year</em></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>return 1 if input year is a leap year otherwise, return 0 </p>
<div class="fragment"><div class="line"><a name="l00042"></a><span class="lineno"> 42</span>&#160;{</div>
<div class="line"><a name="l00043"></a><span class="lineno"> 43</span>&#160; <span class="keywordflow">if</span> ((year % 400 == 0) || ((year % 4 == 0) &amp;&amp; (year % 100 != 0)))</div>
<div class="line"><a name="l00044"></a><span class="lineno"> 44</span>&#160; <span class="keywordflow">return</span> 1;</div>
<div class="line"><a name="l00045"></a><span class="lineno"> 45</span>&#160; </div>
<div class="line"><a name="l00046"></a><span class="lineno"> 46</span>&#160; <span class="keywordflow">return</span> 0;</div>
<div class="line"><a name="l00047"></a><span class="lineno"> 47</span>&#160;}</div>
</div><!-- fragment -->
</div>
</div>
<a id="a3c04138a5bfe5d72780bb7e82a18e627"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a3c04138a5bfe5d72780bb7e82a18e627">&#9670;&nbsp;</a></span>main()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int main </td>
<td>(</td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>argc</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">char **&#160;</td>
<td class="paramname"><em>argv</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Main function </p>
<p>Let us identify days i.e., Sunday thru Saturday with integers - 0 thru 6 respectively Jan 1 1901 was a Tuesday</p>
<p>These two for-loops count the start of day for the next month. Hence, we have to skip the last December count</p>
<p>Main Algorithm: every week has 7 days hence, the start of next day would be modulo 7 add to this, the current start date and ensure the result is still modulo 7!</p>
<div class="fragment"><div class="line"><a name="l00077"></a><span class="lineno"> 77</span>&#160;{</div>
<div class="line"><a name="l00078"></a><span class="lineno"> 78</span>&#160; <span class="keywordtype">int</span> count_sundays = 0;</div>
<div class="line"><a name="l00079"></a><span class="lineno"> 79</span>&#160; <span class="keyword">const</span> <span class="keywordtype">short</span> start_year = 1901;</div>
<div class="line"><a name="l00080"></a><span class="lineno"> 80</span>&#160; <span class="keyword">const</span> <span class="keywordtype">short</span> end_year = 2000;</div>
<div class="line"><a name="l00081"></a><span class="lineno"> 81</span>&#160;<span class="comment"></span> </div>
<div class="line"><a name="l00082"></a><span class="lineno"> 82</span>&#160;<span class="comment"> /**</span></div>
<div class="line"><a name="l00083"></a><span class="lineno"> 83</span>&#160;<span class="comment"> * Let us identify days i.e., Sunday thru Saturday with integers - 0 thru 6</span></div>
<div class="line"><a name="l00084"></a><span class="lineno"> 84</span>&#160;<span class="comment"> *respectively Jan 1 1901 was a Tuesday</span></div>
<div class="line"><a name="l00085"></a><span class="lineno"> 85</span>&#160;<span class="comment"> **/</span></div>
<div class="line"><a name="l00086"></a><span class="lineno"> 86</span>&#160; <span class="keywordtype">char</span> start_day = 2;</div>
<div class="line"><a name="l00087"></a><span class="lineno"> 87</span>&#160; </div>
<div class="line"><a name="l00088"></a><span class="lineno"> 88</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> year = start_year; year &lt;= end_year; year++)</div>
<div class="line"><a name="l00089"></a><span class="lineno"> 89</span>&#160; {</div>
<div class="line"><a name="l00090"></a><span class="lineno"> 90</span>&#160; <span class="keywordtype">char</span> is_leap = <a class="code" href="../../dd/df0/problem__19_2sol1_8c.html#a6561b1adc8a19c092679b9874da24e2e">is_leap_year</a>(year);</div>
<div class="line"><a name="l00091"></a><span class="lineno"> 91</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">char</span> month = 0; month &lt; 12; month++)</div>
<div class="line"><a name="l00092"></a><span class="lineno"> 92</span>&#160; {<span class="comment"></span></div>
<div class="line"><a name="l00093"></a><span class="lineno"> 93</span>&#160;<span class="comment"> /**</span></div>
<div class="line"><a name="l00094"></a><span class="lineno"> 94</span>&#160;<span class="comment"> * These two for-loops count the start of day for the next month.</span></div>
<div class="line"><a name="l00095"></a><span class="lineno"> 95</span>&#160;<span class="comment"> * Hence, we have to skip the last December count */</span></div>
<div class="line"><a name="l00096"></a><span class="lineno"> 96</span>&#160; <span class="keywordflow">if</span> (year == end_year &amp;&amp; month == 11)</div>
<div class="line"><a name="l00097"></a><span class="lineno"> 97</span>&#160; <span class="keywordflow">continue</span>;</div>
<div class="line"><a name="l00098"></a><span class="lineno"> 98</span>&#160; </div>
<div class="line"><a name="l00099"></a><span class="lineno"> 99</span>&#160; <span class="keywordtype">int</span> days = <a class="code" href="../../dd/df0/problem__19_2sol1_8c.html#ab7f9ad087f124b8e0615aa535b4c8a75">get_month_days</a>(month);</div>
<div class="line"><a name="l00100"></a><span class="lineno"> 100</span>&#160; </div>
<div class="line"><a name="l00101"></a><span class="lineno"> 101</span>&#160; <span class="keywordflow">if</span> (is_leap &amp;&amp; month == 1) <span class="comment">/* for a leap year february, add a day */</span></div>
<div class="line"><a name="l00102"></a><span class="lineno"> 102</span>&#160; days++;</div>
<div class="line"><a name="l00103"></a><span class="lineno"> 103</span>&#160; </div>
<div class="line"><a name="l00104"></a><span class="lineno"> 104</span>&#160;<span class="preprocessor">#ifdef DEBUG</span></div>
<div class="line"><a name="l00105"></a><span class="lineno"> 105</span>&#160; <span class="keywordflow">if</span> (year == end_year)</div>
<div class="line"><a name="l00106"></a><span class="lineno"> 106</span>&#160; {</div>
<div class="line"><a name="l00107"></a><span class="lineno"> 107</span>&#160; printf(<span class="stringliteral">&quot;Year: %d\t Month: %d\t Days: %d\t First of day: %s\n&quot;</span>,</div>
<div class="line"><a name="l00108"></a><span class="lineno"> 108</span>&#160; year, month, days, day_string(start_day));</div>
<div class="line"><a name="l00109"></a><span class="lineno"> 109</span>&#160; }</div>
<div class="line"><a name="l00110"></a><span class="lineno"> 110</span>&#160;<span class="preprocessor">#endif</span></div>
<div class="line"><a name="l00111"></a><span class="lineno"> 111</span>&#160;<span class="comment"></span> </div>
<div class="line"><a name="l00112"></a><span class="lineno"> 112</span>&#160;<span class="comment"> /** Main Algorithm:</span></div>
<div class="line"><a name="l00113"></a><span class="lineno"> 113</span>&#160;<span class="comment"> * every week has 7 days hence, the start of next day would be</span></div>
<div class="line"><a name="l00114"></a><span class="lineno"> 114</span>&#160;<span class="comment"> *modulo 7 add to this, the current start date and ensure the result</span></div>
<div class="line"><a name="l00115"></a><span class="lineno"> 115</span>&#160;<span class="comment"> *is still modulo 7!</span></div>
<div class="line"><a name="l00116"></a><span class="lineno"> 116</span>&#160;<span class="comment"> **/</span></div>
<div class="line"><a name="l00117"></a><span class="lineno"> 117</span>&#160; start_day = ((days % 7) + start_day) % 7;</div>
<div class="line"><a name="l00118"></a><span class="lineno"> 118</span>&#160; </div>
<div class="line"><a name="l00119"></a><span class="lineno"> 119</span>&#160; <span class="comment">/* If start-day is a Sunday, increment counter */</span></div>
<div class="line"><a name="l00120"></a><span class="lineno"> 120</span>&#160; <span class="keywordflow">if</span> (start_day == 0)</div>
<div class="line"><a name="l00121"></a><span class="lineno"> 121</span>&#160; count_sundays++;</div>
<div class="line"><a name="l00122"></a><span class="lineno"> 122</span>&#160; }</div>
<div class="line"><a name="l00123"></a><span class="lineno"> 123</span>&#160; }</div>
<div class="line"><a name="l00124"></a><span class="lineno"> 124</span>&#160; </div>
<div class="line"><a name="l00125"></a><span class="lineno"> 125</span>&#160; printf(<span class="stringliteral">&quot;Total number of Sundays that happened on the 1st of a month in the &quot;</span></div>
<div class="line"><a name="l00126"></a><span class="lineno"> 126</span>&#160; <span class="stringliteral">&quot;last century: %d\n&quot;</span>,</div>
<div class="line"><a name="l00127"></a><span class="lineno"> 127</span>&#160; count_sundays);</div>
<div class="line"><a name="l00128"></a><span class="lineno"> 128</span>&#160; </div>
<div class="line"><a name="l00129"></a><span class="lineno"> 129</span>&#160; <span class="keywordflow">return</span> 0;</div>
<div class="line"><a name="l00130"></a><span class="lineno"> 130</span>&#160;}</div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../dd/df0/problem__19_2sol1_8c_a3c04138a5bfe5d72780bb7e82a18e627_cgraph.svg" width="228" height="88"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<div class="ttc" id="aproblem__19_2sol1_8c_html_ab7f9ad087f124b8e0615aa535b4c8a75"><div class="ttname"><a href="../../dd/df0/problem__19_2sol1_8c.html#ab7f9ad087f124b8e0615aa535b4c8a75">get_month_days</a></div><div class="ttdeci">char get_month_days(short month)</div><div class="ttdef"><b>Definition:</b> sol1.c:14</div></div>
<div class="ttc" id="aproblem__19_2sol1_8c_html_a6561b1adc8a19c092679b9874da24e2e"><div class="ttname"><a href="../../dd/df0/problem__19_2sol1_8c.html#a6561b1adc8a19c092679b9874da24e2e">is_leap_year</a></div><div class="ttdeci">char is_leap_year(short year)</div><div class="ttdef"><b>Definition:</b> sol1.c:41</div></div>
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a class="el" href="../../dir_b3d331eabd4234f247030c297aaf021c.html">project_euler</a></li><li class="navelem"><a class="el" href="../../dir_0bc3e2a5baf077fa21dadc079776fcac.html">problem_19</a></li><li class="navelem"><a class="el" href="../../dd/df0/problem__19_2sol1_8c.html">sol1.c</a></li>
<li class="footer">Generated on Sun Jun 28 2020 15:07:07 for Algorithms_in_C by
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/></a> 1.8.18 </li>
</ul>
</div>
</body>
</html>