TheAlgorithms-C/dd/df0/problem__19_2sol1_8c.html

315 lines
20 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=11"/>
<meta name="generator" content="Doxygen 1.9.4"/>
<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 id="projectrow">
<td id="projectalign">
<div id="projectname">Algorithms_in_C<span id="projectnumber">&#160;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.9.4 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "../../search",'Search','.html');
/* @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:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(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:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(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" target="_blank">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 id="func-members" 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="memdesc:ab7f9ad087f124b8e0615aa535b4c8a75"><td class="mdescLeft">&#160;</td><td class="mdescRight">Function to get the number of days in a month. <a href="../../dd/df0/problem__19_2sol1_8c.html#ab7f9ad087f124b8e0615aa535b4c8a75">More...</a><br /></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="memdesc:a6561b1adc8a19c092679b9874da24e2e"><td class="mdescLeft">&#160;</td><td class="mdescRight">Check if input year is a leap year. <a href="../../dd/df0/problem__19_2sol1_8c.html#a6561b1adc8a19c092679b9874da24e2e">More...</a><br /></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="memdesc:a3c04138a5bfe5d72780bb7e82a18e627"><td class="mdescLeft">&#160;</td><td class="mdescRight">Main function. <a href="../../dd/df0/problem__19_2sol1_8c.html#a3c04138a5bfe5d72780bb7e82a18e627">More...</a><br /></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" target="_blank">Problem 19</a> solution </p>
<dl class="section author"><dt>Author</dt><dd><a href="https://github.com/kvedala" target="_blank">Krishna Vedala</a> </dd></dl>
</div><h2 class="groupheader">Function Documentation</h2>
<a id="ab7f9ad087f124b8e0615aa535b4c8a75" name="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>Function to get the number of days in a month. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">month</td><td>month identified by an integer -<br />
<blockquote class="doxtable">
<p >&zwj;0 = Jan and 11 = December </p>
</blockquote>
</td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>number of days in given month </dd></dl>
<dl class="section note"><dt>Note</dt><dd>For February, adjust for leap year outside the function. </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 16</span>{</div>
<div class="line"><span class="lineno"> 17</span> <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"><span class="lineno"> 18</span> <span class="keywordflow">return</span> 28;</div>
<div class="line"><span class="lineno"> 19</span> <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"><span class="lineno"> 20</span> {</div>
<div class="line"><span class="lineno"> 21</span> <span class="keywordflow">if</span> (month &amp; 0x01)</div>
<div class="line"><span class="lineno"> 22</span> <span class="keywordflow">return</span> 30;</div>
<div class="line"><span class="lineno"> 23</span> <span class="keywordflow">else</span></div>
<div class="line"><span class="lineno"> 24</span> <span class="keywordflow">return</span> 31;</div>
<div class="line"><span class="lineno"> 25</span> }</div>
<div class="line"><span class="lineno"> 26</span> </div>
<div class="line"><span class="lineno"> 27</span> <span class="comment">// else if (month &gt;= 7) /* odd months after July have 31 days*/</span></div>
<div class="line"><span class="lineno"> 28</span> </div>
<div class="line"><span class="lineno"> 29</span> <span class="keywordflow">if</span> (month &amp; 0x01)</div>
<div class="line"><span class="lineno"> 30</span> <span class="keywordflow">return</span> 31;</div>
<div class="line"><span class="lineno"> 31</span> </div>
<div class="line"><span class="lineno"> 32</span> <span class="keywordflow">return</span> 30;</div>
<div class="line"><span class="lineno"> 33</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="a6561b1adc8a19c092679b9874da24e2e" name="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>Check if input year is a leap year. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">year</td><td>year to check </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>1 if input year is a leap year </dd>
<dd>
0 if input year is not a leap year </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 42</span>{</div>
<div class="line"><span class="lineno"> 43</span> <span class="keywordflow">if</span> ((year % 400 == 0) || ((year % 4 == 0) &amp;&amp; (year % 100 != 0)))</div>
<div class="line"><span class="lineno"> 44</span> <span class="keywordflow">return</span> 1;</div>
<div class="line"><span class="lineno"> 45</span> </div>
<div class="line"><span class="lineno"> 46</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><span class="lineno"> 47</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="a3c04138a5bfe5d72780bb7e82a18e627" name="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>
<div class="fragment"><div class="line"><span class="lineno"> 80</span>{</div>
<div class="line"><span class="lineno"> 81</span> <span class="keywordtype">int</span> count_sundays = 0;</div>
<div class="line"><span class="lineno"> 82</span> <span class="keyword">const</span> <span class="keywordtype">short</span> start_year = 1901;</div>
<div class="line"><span class="lineno"> 83</span> <span class="keyword">const</span> <span class="keywordtype">short</span> end_year = 2000;</div>
<div class="line"><span class="lineno"> 84</span> </div>
<div class="line"><span class="lineno"> 85</span> <span class="comment">/*</span></div>
<div class="line"><span class="lineno"> 86</span><span class="comment"> * Let us identify days i.e., Sunday thru Saturday with integers - 0 thru 6</span></div>
<div class="line"><span class="lineno"> 87</span><span class="comment"> * respectively Jan 1 1901 was a Tuesday</span></div>
<div class="line"><span class="lineno"> 88</span><span class="comment"> */</span></div>
<div class="line"><span class="lineno"> 89</span> <span class="keywordtype">char</span> start_day = 2;</div>
<div class="line"><span class="lineno"> 90</span> </div>
<div class="line"><span class="lineno"> 91</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> year = start_year; year &lt;= end_year; year++)</div>
<div class="line"><span class="lineno"> 92</span> {</div>
<div class="line"><span class="lineno"> 93</span> <span class="keywordtype">char</span> is_leap = <a class="code hl_function" href="../../dd/df0/problem__19_2sol1_8c.html#a6561b1adc8a19c092679b9874da24e2e">is_leap_year</a>(year);</div>
<div class="line"><span class="lineno"> 94</span> <span class="keywordflow">for</span> (<span class="keywordtype">char</span> month = 0; month &lt; 12; month++)</div>
<div class="line"><span class="lineno"> 95</span> {</div>
<div class="line"><span class="lineno"> 96</span> <span class="comment">/*</span></div>
<div class="line"><span class="lineno"> 97</span><span class="comment"> * These two for-loops count the start of day for the next month.</span></div>
<div class="line"><span class="lineno"> 98</span><span class="comment"> * Hence, we have to skip the last December count</span></div>
<div class="line"><span class="lineno"> 99</span><span class="comment"> */</span></div>
<div class="line"><span class="lineno"> 100</span> <span class="keywordflow">if</span> (year == end_year &amp;&amp; month == 11)</div>
<div class="line"><span class="lineno"> 101</span> <span class="keywordflow">continue</span>;</div>
<div class="line"><span class="lineno"> 102</span> </div>
<div class="line"><span class="lineno"> 103</span> <span class="keywordtype">int</span> days = <a class="code hl_function" href="../../dd/df0/problem__19_2sol1_8c.html#ab7f9ad087f124b8e0615aa535b4c8a75">get_month_days</a>(month);</div>
<div class="line"><span class="lineno"> 104</span> </div>
<div class="line"><span class="lineno"> 105</span> <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"><span class="lineno"> 106</span> days++;</div>
<div class="line"><span class="lineno"> 107</span> </div>
<div class="line"><span class="lineno"> 108</span><span class="preprocessor">#ifdef DEBUG</span></div>
<div class="line"><span class="lineno"> 109</span> <span class="keywordflow">if</span> (year == end_year)</div>
<div class="line"><span class="lineno"> 110</span> {</div>
<div class="line"><span class="lineno"> 111</span> 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"><span class="lineno"> 112</span> year, month, days, day_string(start_day));</div>
<div class="line"><span class="lineno"> 113</span> }</div>
<div class="line"><span class="lineno"> 114</span><span class="preprocessor">#endif</span></div>
<div class="line"><span class="lineno"> 115</span> </div>
<div class="line"><span class="lineno"> 116</span> <span class="comment">/* Main Algorithm:</span></div>
<div class="line"><span class="lineno"> 117</span><span class="comment"> * every week has 7 days hence, the start of next day would be</span></div>
<div class="line"><span class="lineno"> 118</span><span class="comment"> * modulo 7 add to this, the current start date and ensure the</span></div>
<div class="line"><span class="lineno"> 119</span><span class="comment"> * result is still modulo 7!</span></div>
<div class="line"><span class="lineno"> 120</span><span class="comment"> */</span></div>
<div class="line"><span class="lineno"> 121</span> start_day = ((days % 7) + start_day) % 7;</div>
<div class="line"><span class="lineno"> 122</span> </div>
<div class="line"><span class="lineno"> 123</span> <span class="comment">/* If start-day is a Sunday, increment counter */</span></div>
<div class="line"><span class="lineno"> 124</span> <span class="keywordflow">if</span> (start_day == 0)</div>
<div class="line"><span class="lineno"> 125</span> count_sundays++;</div>
<div class="line"><span class="lineno"> 126</span> }</div>
<div class="line"><span class="lineno"> 127</span> }</div>
<div class="line"><span class="lineno"> 128</span> </div>
<div class="line"><span class="lineno"> 129</span> printf(</div>
<div class="line"><span class="lineno"> 130</span> <span class="stringliteral">&quot;Total number of Sundays that happened on the 1st of a month in the &quot;</span></div>
<div class="line"><span class="lineno"> 131</span> <span class="stringliteral">&quot;last century: %d\n&quot;</span>,</div>
<div class="line"><span class="lineno"> 132</span> count_sundays);</div>
<div class="line"><span class="lineno"> 133</span> </div>
<div class="line"><span class="lineno"> 134</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><span class="lineno"> 135</span>}</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="ttdoc">Check if input year is a leap year.</div><div class="ttdef"><b>Definition:</b> sol1.c:41</div></div>
<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="ttdoc">Function to get the number of days in a month.</div><div class="ttdef"><b>Definition:</b> sol1.c:15</div></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 -->
<!-- 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 by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.4 </li>
</ul>
</div>
</body>
</html>