<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
> <channel><title>Jonathan Landrum</title> <atom:link href="http://jonlandrum.com/feed/" rel="self" type="application/rss+xml" /><link>http://jonlandrum.com</link> <description>Producing fine code in Java, C++, Fortran, and others</description> <lastBuildDate>Sat, 12 May 2012 23:56:43 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.2</generator> <xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" /> <item><title>Calculating Logarithms of an Arbitrary Base</title><link>http://jonlandrum.com/2012/05/12/calculating-logarithms-of-an-arbitrary-base/</link> <comments>http://jonlandrum.com/2012/05/12/calculating-logarithms-of-an-arbitrary-base/#comments</comments> <pubDate>Sat, 12 May 2012 23:56:43 +0000</pubDate> <dc:creator>Jonathan</dc:creator> <category><![CDATA[Fortran]]></category> <guid
isPermaLink="false">http://jonlandrum.com/?p=354</guid> <description><![CDATA[Fortran includes a couple of intrinsic functions for calculating logarithms: LOG(), which calculates the loge &#8212; the natural log or ln &#8212; of the number, and LOG10(), which calculates the log10 of the number. Why the former isn&#8217;t LN() and &#8230; <a
href="http://jonlandrum.com/2012/05/12/calculating-logarithms-of-an-arbitrary-base/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>Fortran includes a couple of intrinsic functions for calculating logarithms: <code>LOG()</code>, which calculates the <i>log<sub>e</sub></i> &#8212; the natural log or <i>ln</i> &#8212; of the number, and <code>LOG10()</code>, which calculates the <i>log<sub>10</sub></i> of the number. Why the former isn&#8217;t <code>LN()</code> and the latter <code>LOG()</code> is beyond me, but I digress.</p><p>These two logarithm functions are normally all one would need for their calculations, but say you need the logarithm of a fractional number to a fractional base? You can do that, since the definition of a logarithm has been extended to the set of all positive numbers, and can be calculated with some simpler math:</p><p><img
src="http://jonlandrum.com/wp-content/uploads/2012/05/arbitrary-log.gif" alt="Equation for solving logarithms of an arbitrary base" title="Equation for solving logarithms of an arbitrary base" width="112" height="42" class="size-full wp-image-355" /></p><p>Since calculating logarithms to a base of 10 is trivial, we can simply use this formula within a function called <code>LogB()</code> to return the logarithm of any positive number to any positive base:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>49
50
51
52
53
54
55
56
57
58
59
60
61
62
</pre></td><td
class="code"><pre class="fortran" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">! ----------------------------------------------------------</span>
<span style="color: #666666; font-style: italic;">! FUNCTION LogB:</span>
<span style="color: #666666; font-style: italic;">! Calculates the logarithm of a number to an arbitrary base.</span>
<span style="color: #666666; font-style: italic;">! ----------------------------------------------------------</span>
<span style="color: #000066;">REAL</span> <span style="color: #b1b100;">FUNCTION</span> LogB<span style="color: #009900;">&#40;</span>n, b<span style="color: #009900;">&#41;</span>
	<span style="color: #000066;">IMPLICIT</span> <span style="color: #000066;">NONE</span>
&nbsp;
	<span style="color: #000066;">REAL</span> <span style="color: #339933;">::</span> <span style="color: #202020;">n</span>, b
&nbsp;
	LogB <span style="color: #339933;">=</span> <span style="color: #993333;">LOG10</span><span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #993333;">LOG10</span><span style="color: #009900;">&#40;</span>b<span style="color: #009900;">&#41;</span>
&nbsp;
	<span style="color: #b1b100;">RETURN</span>
&nbsp;
<span style="color: #b1b100;">END</span> <span style="color: #b1b100;">FUNCTION</span> LogB</pre></td></tr></table></div><p>Using this function within a program is as simple as calling it: <code> LogB(number, base)</code>. So simple, in fact, that I&#8217;m not even going to reproduce the main program here. This function is all that matters in the scope of this article, anyway. But why do the numbers in the actual parameter list have to be positive? Because negative logarithms are undefined:</p><div
id="attachment_356" class="wp-caption alignnone" style="width: 522px"><img
src="http://jonlandrum.com/wp-content/uploads/2012/05/natural-log.png" alt="Graph of the natural logarithm of x" title="Graph of the natural logarithm of x" width="512" height="512" class="size-full wp-image-356" /><p
class="wp-caption-text">Graph of the natural logarithm of x</p></div><p>As the number to be calculated approaches 0, the value decreases without bound. The <i>y</i>-axis is actually a vertical asymptote for it. In the function I wrote above, I didn&#8217;t put bounds checking in place. But Fortran is smart enough to deal with poor input, yielding <code>NaN</code> for negative input, and <code>-Infinity</code> for 0. And people wonder why I love this language.</p><p>~Jonathan</p> ]]></content:encoded> <wfw:commentRss>http://jonlandrum.com/2012/05/12/calculating-logarithms-of-an-arbitrary-base/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Fun With Permutations</title><link>http://jonlandrum.com/2012/05/01/fun-with-permutations/</link> <comments>http://jonlandrum.com/2012/05/01/fun-with-permutations/#comments</comments> <pubDate>Wed, 02 May 2012 02:23:26 +0000</pubDate> <dc:creator>Jonathan</dc:creator> <category><![CDATA[C/C++]]></category> <category><![CDATA[Computer Science]]></category> <guid
isPermaLink="false">http://jonlandrum.com/?p=350</guid> <description><![CDATA[A friend of mine was complaining at work the other day about a programming assignment he had, frustrated that he couldn&#8217;t think of the answer. I asked him if he&#8217;d email me the basic premise of the assignment so I &#8230; <a
href="http://jonlandrum.com/2012/05/01/fun-with-permutations/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>A friend of mine was complaining at work the other day about a programming assignment he had, frustrated that he couldn&#8217;t think of the answer. I asked him if he&#8217;d email me the basic premise of the assignment so I could tackle it myself after finals were over, and he agreed. Here&#8217;s what he sent me:</p><blockquote><p>Write a program that returns the number of <b>permutations</b> of the set {<i>A</i>, <i>B</i>, <i>C</i>, <i>D</i>, <i>E</i>, <i>F</i>} where <i>A</i> appears before <i>D</i>.</p></blockquote><p>The reason most people in that class had a problem with this assignment is because they hadn&#8217;t yet had Probability and Statistics. What you have to throw away is the concept of the letters &#8220;A&#8221; and &#8220;D&#8221; and where they appear in the list. You only need to concern yourself with the blanks they occupy and how many ways these blanks can be arranged. A <i><a
href="http://en.wikipedia.org/wiki/Permutation#In_combinatorics" title="Article on Wikipedia">Permutation</a></i> is an arrangement of items in a set where order matters, and where there&#8217;s no replacement of items. This value is generalized by <i>n!</i> where <i>n</i> is the size of the set.</p><p>At first glance, this task seems daunting, since we&#8217;re not dealing with numbers. Sure, I can factorial an integer, but how do you factorial D? As I alluded before, you don&#8217;t have to. The easiest way to think about this &#8212; and make no mistake, this is primarily a cognitive assignment &#8212; is to imagine all the permutations of the set where &#8220;A&#8221; is first. Since &#8220;D&#8221; will always fall after &#8220;A&#8221; in this case, all we have to calculate now is how many permutations of the subset from &#8220;B&#8221; to &#8220;F&#8221; there are. That is as simple as calculating 5! using the <a
href="http://jonlandrum.com/2012/02/15/calculating-factorials-with-a-recursive-function/" title="Calculating Factorials with a Recursive Function"><code>factorial()</code> method</a> we&#8217;ve come to know and love.</p><p>That&#8217;s our first number, now we need to find our second one, where &#8220;A&#8221; is second in the list. This will be the same <code>factorial(5)</code> call as before, but we have to subtract from this total the number of times &#8220;D&#8221; appears before &#8220;A&#8221;. That set looks like &#8220;{D, A, X, X, X, X}&#8221; where the X&#8217;s are the other letters. And again, notice it doesn&#8217;t matter what those letters are, just that there are four blanks we&#8217;re swapping around. So &#8220;A&#8221; appears in the second slot 5! times, and &#8220;D&#8221; appears before &#8220;A&#8221; a subset of those times, which is the number of ways the remaining four letters can be arranged. The final calculation for the second number is <code>factorial(5) - factorial(4)</code>.</p><p>And this pattern repeats itself for each successive number, up to the calculation where &#8220;A&#8221; appears sixth in the set. That calculation looks like this:</p><pre>
factorial(5) - factorial(4) - factorial(4) - factorial(4) - factorial(4) - factorial(4)
</pre><p>That is one <code>factorial(5)</code> for our &#8220;A&#8221; position, and one <code>factorial(4)</code> for each possible position of &#8220;D&#8221;. Since there are five blanks before &#8220;A&#8221; this time, that&#8217;s five <code>factorial(4)</code> calculations. If you are very familiar with factorials, you will notice that <strong><i>5 &times; 4! = 5!</i></strong>. So what we really have here is <i>5! &#8211; 5!</i>, which is obviously 0. And that makes sense, because when &#8220;A&#8221; is last in the list, there are no permutations where &#8220;A&#8221; appears before &#8220;D&#8221;.</p><p>Once you see the pattern, the program practically writes itself. Now add up the six numbers, and you have your total. You want to know what&#8217;s <em>really</em> interesting? It&#8217;s the same number, no matter which two letters you pick. Remember: what&#8217;s in the blanks doesn&#8217;t matter. I hope I didn&#8217;t math you out too much this time.</p><p>~Jonathan</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
</pre></td><td
class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #ff0000; font-style: italic;">/*********************************
 Programmer:      Jonathan Landrum
 Program:         permutations.cpp
 Date:            1 May 2012
 ---------------------------------
 Assumptions:     None.
 Dependencies:    None.
 *********************************/</span>
&nbsp;
&nbsp;
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
<span style="color: #0000ff;">long</span> <span style="color: #0000ff;">int</span> factorial <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span> <span style="color: #0000ff;">int</span> in<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
&nbsp;
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #666666;">// main():</span>
<span style="color: #666666;">//</span>
<span style="color: #666666;">// Calls the factorial function, and returns results.</span>
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> first, second, third, fourth, fifth, sixth<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;/====================================<span style="color: #000099; font-weight: bold;">\\</span>&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;|                                    |&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;|          C++ Permutations          |&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;|                                    |&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>====================================/&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;This program returns the number of&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;permutations there are of the letters&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;from A to F where A appears before D.&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;--------------------------------------&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Processing...&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #ff0000; font-style: italic;">/****************************
     Each factorial(5) is where
     A appears in the set, and
     each factorial(4) is an
     exception where D appears
     before A. Notice that when A
     is first, it doesn't matter
     where D appears.
&nbsp;
     We could have more easily
     multiplied our factorial
     calculations and gained some
     speed, but I wanted to show
     WHY these numbers are right.
     ****************************/</span>
    first  <span style="color: #000080;">=</span> factorial<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">5</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
                        <span style="color: #666666;">// {D,A,X,X,X,X}</span>
    second <span style="color: #000080;">=</span> factorial<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">5</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> factorial<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
                        <span style="color: #666666;">// {D,X,A,X,X,X} &amp; {X,D,A,X,X,X}</span>
    third  <span style="color: #000080;">=</span> factorial<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">5</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> factorial<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> factorial<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
                        <span style="color: #666666;">// {D,X,X,A,X,X} &amp; {X,D,X,A,X,X} &amp; {X,X,D,A,X,X}</span>
    fourth <span style="color: #000080;">=</span> factorial<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">5</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> factorial<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> factorial<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> factorial<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
                        <span style="color: #666666;">// {D,X,X,X,A,X} &amp; {X,D,X,X,A,X} &amp; {X,X,D,X,A,X} &amp; {X,X,X,D,A,X}</span>
    fifth  <span style="color: #000080;">=</span> factorial<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">5</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> factorial<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> factorial<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> factorial<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> factorial<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #ff0000; font-style: italic;">/****************************
     Notice that factorial(4) x 5
     is equal to factorial(5). So
     this is a wasted calculation
     for the sake of example.
     ****************************/</span>
                        <span style="color: #666666;">// {D,X,X,X,X,A} &amp; {X,D,X,X,X,A} &amp; {X,X,D,X,X,A} &amp; {X,X,X,D,X,A} &amp; {X,X,X,X,D,A}</span>
    sixth  <span style="color: #000080;">=</span> factorial<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">5</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> factorial<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> factorial<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> factorial<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> factorial<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> factorial<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; &quot;</span> <span style="color: #000080;">&lt;&lt;</span> first <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; &lt;- All where A is 1st&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;  &quot;</span> <span style="color: #000080;">&lt;&lt;</span> second <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; &lt;- All where A is 2nd&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;  &quot;</span> <span style="color: #000080;">&lt;&lt;</span> third  <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; &lt;- All where A is 3rd&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;  &quot;</span> <span style="color: #000080;">&lt;&lt;</span> fourth <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; &lt;- All where A is 4th&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;  &quot;</span> <span style="color: #000080;">&lt;&lt;</span> fifth <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; &lt;- All where A is 5th&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;+  &quot;</span> <span style="color: #000080;">&lt;&lt;</span> sixth <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; &lt;- All where A is 6th&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;----&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; &quot;</span> <span style="color: #000080;">&lt;&lt;</span> first <span style="color: #000040;">+</span> second <span style="color: #000040;">+</span> third <span style="color: #000040;">+</span> fourth <span style="color: #000040;">+</span> fifth <span style="color: #000040;">+</span> sixth <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Process complete.&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;--------------------------------------&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\\</span><span style="color: #000099; font-weight: bold;">\\</span>//_ Live long and prosper.&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #666666;">// factorial():</span>
<span style="color: #666666;">//</span>
<span style="color: #666666;">// Recursive function.</span>
<span style="color: #666666;">// Returns long int, accepts (long int in).</span>
<span style="color: #666666;">//</span>
<span style="color: #666666;">// Calculates the factorial of the given number.</span>
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #0000ff;">long</span> <span style="color: #0000ff;">int</span> factorial <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span> <span style="color: #0000ff;">int</span> in<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">long</span> <span style="color: #0000ff;">int</span> result <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>in <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
        result <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">else</span>
        result <span style="color: #000080;">=</span> in <span style="color: #000040;">*</span> factorial<span style="color: #008000;">&#40;</span>in <span style="color: #000040;">-</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">return</span> result<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// End factorial</span></pre></td></tr></table></div> ]]></content:encoded> <wfw:commentRss>http://jonlandrum.com/2012/05/01/fun-with-permutations/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Translating Programs Between Languages</title><link>http://jonlandrum.com/2012/04/29/translating-programs-between-languages/</link> <comments>http://jonlandrum.com/2012/04/29/translating-programs-between-languages/#comments</comments> <pubDate>Sun, 29 Apr 2012 21:11:23 +0000</pubDate> <dc:creator>Jonathan</dc:creator> <category><![CDATA[C/C++]]></category> <category><![CDATA[Fortran]]></category> <category><![CDATA[Java]]></category> <guid
isPermaLink="false">http://jonlandrum.com/?p=347</guid> <description><![CDATA[I&#8217;ve written on this site so far in a few of the languages I know, and today I want to demonstrate that any language you like is usually a perfectly valid language to use. Each has their own niche that &#8230; <a
href="http://jonlandrum.com/2012/04/29/translating-programs-between-languages/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>I&#8217;ve written on this site so far in a few of the languages I know, and today I want to demonstrate that any language you like is usually a perfectly valid language to use. Each has their own niche that sets them apart, but generally they all do the same thing.</p><p>I was recently given an assignment to write a program that prints an hourglass shape using asterisks, and the user input should specify how many rows of asterisks there are on each end of the figure. For instance, a user input of 3 should output the following figure:</p><pre>
*****
 ***
  *
 ***
*****
</pre><p>Three rows on top, and three on bottom (counting the center asterisk as one each time.) I wrote the assignment in C++, since that&#8217;s what the assignment specifically called for. But I have also translated the program into Java and Fortran to demonstrate my point.</p><p>Java is only a slight change from C++, so it&#8217;s obvious. But Fortran is absolutely foreign in comparison; can an algorithm from C++ really be translated to Fortran? If you keep the basic premise of your algorithm&#8217;s logic intact, then yes, it can easily be done. You just have to be familiar with the differences in each language&#8217;s logical structures. Let&#8217;s start with the original C++ code:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td
class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> input <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Enter the size of your hourglass: &quot;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cin</span>  <span style="color: #000080;">&gt;&gt;</span> input<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> c <span style="color: #000080;">=</span> input <span style="color: #000040;">-</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> c <span style="color: #000080;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> <span style="color: #000040;">--</span>c<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> <span style="color: #008000;">&#40;</span>input <span style="color: #000040;">-</span> c<span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span>
            <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; &quot;</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">2</span> <span style="color: #000040;">*</span> c <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> <span style="color: #000040;">--</span>i<span style="color: #008000;">&#41;</span>
            <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;*&quot;</span><span style="color: #008080;">;</span>
        <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span> <span style="color: #666666;">// End top for loop</span>
    <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> c <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> c <span style="color: #000080;">&lt;</span> input<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>c<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> <span style="color: #008000;">&#40;</span>input <span style="color: #000040;">-</span> c<span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span>
            <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; &quot;</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">2</span> <span style="color: #000040;">*</span> c <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> <span style="color: #000040;">--</span>i<span style="color: #008000;">&#41;</span>
            <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;*&quot;</span><span style="color: #008080;">;</span>
        <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span> <span style="color: #666666;">// End bottom for loop</span>
<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// End main</span></pre></td></tr></table></div><p>Two <code>for</code> loops with diligent attention given to the counter variables and the conditions that make the loops exit, and you&#8217;re done. Simple, right? Converting this to Java is just as simple:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>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
</pre></td><td
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Scanner</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> hourglass <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">int</span> input <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		Scanner scan <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Scanner<span style="color: #009900;">&#40;</span><span style="color: #003399;">System</span>.<span style="color: #006633;">in</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Enter the size of your hourglass: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		input <span style="color: #339933;">=</span> scan.<span style="color: #006633;">nextInt</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> c <span style="color: #339933;">=</span> input <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> c <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #339933;">--</span>c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #009900;">&#40;</span>input <span style="color: #339933;">-</span> c<span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i<span style="color: #009900;">&#41;</span>
				<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span> <span style="color: #339933;">*</span> c <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #339933;">--</span>i<span style="color: #009900;">&#41;</span>
				<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;*&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// End top for loop</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> c <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> c <span style="color: #339933;">&lt;</span> input<span style="color: #339933;">;</span> <span style="color: #339933;">++</span>c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #009900;">&#40;</span>input <span style="color: #339933;">-</span> c<span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i<span style="color: #009900;">&#41;</span>
				<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span> <span style="color: #339933;">*</span> c <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #339933;">--</span>i<span style="color: #009900;">&#41;</span>
				<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;*&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// End bottom for loop</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div><p>So we&#8217;ve got our program working in two different languages now (which wasn&#8217;t all that difficult, really.) Now let&#8217;s give Fortran a try. This one is going to be a bit tricky; we can&#8217;t just copy-and-paste the code and change the differences. Fortran is altogether different. But the principles of making this function work are <em>identical</em>. Watch:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>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
</pre></td><td
class="code"><pre class="fortran" style="font-family:monospace;"><span style="color: #b1b100;">PROGRAM</span> hourglass
	<span style="color: #000066;">IMPLICIT</span> <span style="color: #000066;">NONE</span>
&nbsp;
	<span style="color: #000066;">INTEGER</span> <span style="color: #339933;">::</span> <span style="color: #202020;">input</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>
	<span style="color: #000066;">INTEGER</span> <span style="color: #339933;">::</span> <span style="color: #202020;">c</span>
	<span style="color: #000066;">INTEGER</span> <span style="color: #339933;">::</span> <span style="color: #202020;">i</span>
&nbsp;
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #ff0000;">'(a)'</span>,<span style="color: #b1b100;">advance</span><span style="color: #339933;">=</span><span style="color: #ff0000;">'no'</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'Enter the size of your hourglass: '</span>
	READ  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> input
&nbsp;
	c <span style="color: #339933;">=</span> input <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span>
	<span style="color: #b1b100;">DO</span> <span style="color: #b1b100;">WHILE</span> <span style="color: #009900;">&#40;</span>c &gt; <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
		i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>
		<span style="color: #b1b100;">DO</span> <span style="color: #b1b100;">WHILE</span> <span style="color: #009900;">&#40;</span>i &lt; <span style="color: #009900;">&#40;</span>input <span style="color: #339933;">-</span> c<span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
			WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #ff0000;">'(a)'</span>,<span style="color: #b1b100;">advance</span><span style="color: #339933;">=</span><span style="color: #ff0000;">'no'</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">' '</span>
			i <span style="color: #339933;">=</span> i <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span>
		<span style="color: #b1b100;">END</span> <span style="color: #b1b100;">DO</span>
		i <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span> <span style="color: #339933;">*</span> c <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
		<span style="color: #b1b100;">DO</span> <span style="color: #b1b100;">WHILE</span> <span style="color: #009900;">&#40;</span>i &gt; <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
			WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #ff0000;">'(a)'</span>,<span style="color: #b1b100;">advance</span><span style="color: #339933;">=</span><span style="color: #ff0000;">'no'</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'*'</span>
			i <span style="color: #339933;">=</span> i <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span>
		<span style="color: #b1b100;">END</span> <span style="color: #b1b100;">DO</span>
		c <span style="color: #339933;">=</span> c <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span>
		WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
	<span style="color: #b1b100;">END</span> <span style="color: #b1b100;">DO</span>
	c <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>
	<span style="color: #b1b100;">DO</span> <span style="color: #b1b100;">WHILE</span> <span style="color: #009900;">&#40;</span>c &lt; input<span style="color: #009900;">&#41;</span>
		i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>
		<span style="color: #b1b100;">DO</span> <span style="color: #b1b100;">WHILE</span> <span style="color: #009900;">&#40;</span>i &lt; <span style="color: #009900;">&#40;</span>input <span style="color: #339933;">-</span> c<span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
			WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #ff0000;">'(a)'</span>,<span style="color: #b1b100;">advance</span><span style="color: #339933;">=</span><span style="color: #ff0000;">'no'</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">' '</span>
			i <span style="color: #339933;">=</span> i <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span>
		<span style="color: #b1b100;">END</span> <span style="color: #b1b100;">DO</span>
		i <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span> <span style="color: #339933;">*</span> c <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
		<span style="color: #b1b100;">DO</span> <span style="color: #b1b100;">WHILE</span> <span style="color: #009900;">&#40;</span>i &gt; <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
			WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #ff0000;">'(a)'</span>,<span style="color: #b1b100;">advance</span><span style="color: #339933;">=</span><span style="color: #ff0000;">'no'</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'*'</span>
			i <span style="color: #339933;">=</span> i <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span>
		<span style="color: #b1b100;">END</span> <span style="color: #b1b100;">DO</span>
		c <span style="color: #339933;">=</span> c <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span>
		WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
	<span style="color: #b1b100;">END</span> <span style="color: #b1b100;">DO</span>
<span style="color: #b1b100;">END</span> <span style="color: #b1b100;">PROGRAM</span> hourglass</pre></td></tr></table></div><p>It&#8217;s longer than the other two; nearly twice as long. And there&#8217;s a lot of line return control going on. But it can be done, and without any hacks or Fortran-fu. It&#8217;s a simple translation of the exact <code>for</code> loop structure from before into a Fortran <code>DO WHILE</code> structure. And that, my friend, is how you translate code from one language to another. Know both languages first, know how to work logical constructs in both, and you&#8217;ll do fine. Happy coding.</p><p>~Jonathan</p> ]]></content:encoded> <wfw:commentRss>http://jonlandrum.com/2012/04/29/translating-programs-between-languages/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Inductive Reasoning with Recursion</title><link>http://jonlandrum.com/2012/04/16/inductive-reasoning-with-recursion/</link> <comments>http://jonlandrum.com/2012/04/16/inductive-reasoning-with-recursion/#comments</comments> <pubDate>Mon, 16 Apr 2012 21:37:11 +0000</pubDate> <dc:creator>Jonathan</dc:creator> <category><![CDATA[C/C++]]></category> <guid
isPermaLink="false">http://jonlandrum.com/?p=343</guid> <description><![CDATA[This afternoon I found some programming assignments posted on a university website by a professor for his Discrete Structures course, and I decided to tackle these assignments one at a time. This is the first of those assignments, written in &#8230; <a
href="http://jonlandrum.com/2012/04/16/inductive-reasoning-with-recursion/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>This afternoon I found some programming assignments posted on a university website by a professor for his <a
href="http://en.wikipedia.org/wiki/Discrete_mathematics" title="Article on Wikipedia">Discrete Structures</a> course, and I decided to tackle these assignments one at a time. This is the first of those assignments, written in C++.</p><p>This program makes use of <a
href="http://en.wikipedia.org/wiki/Inductive_reasoning" title="Article on Wikipedia">Inductive Reasoning</a> to prove the <em>fundamental theorem of arithmetic:</em></p><blockquote><p> The <strong>fundamental theorem of arithmetic</strong> states that any integer greater than 1 can be written as a unique product of prime numbers.</p></blockquote><p>This program makes use of two recursive functions: the <code>prime()</code> function, which you&#8217;ve seen here many times before (<a
href="http://jonlandrum.com/?s=prime&#038;submit=Search" title="Search results">just search for &#8220;prime&#8221;</a>), and the <code>induce()</code> function. The former is an exact duplicate of that used in <a
href="http://jonlandrum.com/2012/02/06/computing-the-nth-prime/" title="Computing the nth Prime">Computing the nth Prime</a> and <a
href="http://jonlandrum.com/2012/02/10/adding-the-prime-numbers-less-than-2000000/" title="Adding the Prime Numbers Less Than 2,000,000">Adding the Prime Numbers Less Than 2,000,000</a>. The latter function had to be written from scratch. Fortunately, it&#8217;s a trivial function. To decide how to go about writing that function, let&#8217;s look at the assignment:</p><blockquote><p> Given a particular n > 1, your job is to generate a direct proof that n can be written as a product of primes. For example, if given n = 5, your program should generate output similar to the following:</p><pre>5 is prime, so it is a product of a single prime.</pre><p>On the other hand, given n = 60, your program should output:</p><pre>In order to prove that 60 is a product of primes
	we prove that 6 and 10 are products of primes.
In order to prove that 6 is a product of primes
	we prove that 2 and 3 are products of primes.
2 is prime, so it is a product of a single prime.
3 is prime, so it is a product of a single prime.
Having proven that 2 and 3 are products of primes,
	we conclude that 6 = 2 * 3
	is a product of primes.
In order to prove that 10 is a product of primes
	we prove that 2 and 5 are products of primes.
2 is prime, so it is a product of a single prime.
5 is prime, so it is a product of a single prime.
Having proven that 2 and 5 are products of primes,
	we conclude that 10 = 2 * 5
	is a product of primes.
Having proven that 6 and 10 are products of primes,
	we conclude that 60 = 6 * 10
	is a product of primes.</pre><p>The generated proof breaks 60 into 6 and 10, proves that 6 and 10 can each be written as a product of primes, and concludes as a result that 60&nbsp;=&nbsp;6&nbsp;&times;&nbsp;10 can also be written as a product of primes.</p></blockquote><p>This program works according to the directions, with the simple deviation that my <code>induce()</code> method checks for divisibility by a short list of primes rather than the two factors nearest to &radic;<i>n</i>. I have tested it to the maximum size of an <code>int</code> in C, and it works flawlessly.</p><p>~Jonathan</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
</pre></td><td
class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// ==================================================</span>
<span style="color: #666666;">// Programmer:  Jonathan Landrum</span>
<span style="color: #666666;">// Date:        16 April 2012</span>
<span style="color: #666666;">// ==================================================</span>
<span style="color: #666666;">// Program:     induce.cpp</span>
<span style="color: #666666;">// Purpose:     Uses inductive reasoning to show that</span>
<span style="color: #666666;">//		a number input by the user is a</span>
<span style="color: #666666;">//		product of primes.</span>
<span style="color: #666666;">// Assumptions: 1.) The number input is a natural</span>
<span style="color: #666666;">//		    number.</span>
<span style="color: #666666;">//		2.) The number input is &gt; 1.</span>
<span style="color: #666666;">// ==================================================</span>
&nbsp;
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #666666;">// Function prototypes</span>
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #0000ff;">void</span> induce<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">bool</span> prime<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
&nbsp;
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #666666;">// main():</span>
<span style="color: #666666;">// Calls the induce() function</span>
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #0000ff;">int</span> main <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
	<span style="color: #666666;">// Variables</span>
	<span style="color: #0000ff;">int</span>  input <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Introduce the program</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;--------------------------------&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;-      Inductive Reasoning     -&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;--------------------------------&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;This program uses recursion to&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;show that any natural number &gt; 1&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;is a product of primes.&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Get user input</span>
	<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>input <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Enter an integer to test: &quot;</span><span style="color: #008080;">;</span>
		<span style="color: #0000dd;">cin</span>  <span style="color: #000080;">&gt;&gt;</span> input<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Return the results</span>
	induce<span style="color: #008000;">&#40;</span>input<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;--------------------------------&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\\</span><span style="color: #000099; font-weight: bold;">\\</span>//_ Live long and prosper.&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// End main</span>
&nbsp;
&nbsp;
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #666666;">// induce():</span>
<span style="color: #666666;">// Prints out the results of proving the number input</span>
<span style="color: #666666;">// is a product of primes.</span>
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #0000ff;">void</span> induce <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>prime<span style="color: #008000;">&#40;</span>n<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; is prime, so it is a product of a single prime.&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;In order to prove that &quot;</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; is a product of primes&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>n <span style="color: #000040;">%</span> <span style="color: #0000dd;">13</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>we prove that &quot;</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000040;">/</span> <span style="color: #0000dd;">13</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; and 13 are products of primes.&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
			induce<span style="color: #008000;">&#40;</span>n<span style="color: #000040;">/</span><span style="color: #0000dd;">13</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			induce<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">13</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Having proven that &quot;</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000040;">/</span> <span style="color: #0000dd;">13</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; and 13 are products of primes,&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>we condlude that &quot;</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000040;">/</span> <span style="color: #0000dd;">13</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; * &quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #0000dd;">13</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>n <span style="color: #000040;">%</span> <span style="color: #0000dd;">11</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>we prove that &quot;</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000040;">/</span> <span style="color: #0000dd;">11</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; and 11 are products of primes.&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
			induce<span style="color: #008000;">&#40;</span>n<span style="color: #000040;">/</span><span style="color: #0000dd;">11</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			induce<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">11</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Having proven that &quot;</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000040;">/</span> <span style="color: #0000dd;">11</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; and 11 are products of primes,&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>we condlude that &quot;</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000040;">/</span> <span style="color: #0000dd;">11</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; * &quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #0000dd;">11</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>n <span style="color: #000040;">%</span> <span style="color: #0000dd;">7</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>we prove that &quot;</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000040;">/</span> <span style="color: #0000dd;">7</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; and 7 are products of primes.&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
			induce<span style="color: #008000;">&#40;</span>n<span style="color: #000040;">/</span><span style="color: #0000dd;">7</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			induce<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">7</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Having proven that &quot;</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000040;">/</span> <span style="color: #0000dd;">7</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; and 7 are products of primes,&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>we condlude that &quot;</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000040;">/</span> <span style="color: #0000dd;">7</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; * &quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #0000dd;">7</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>n <span style="color: #000040;">%</span> <span style="color: #0000dd;">5</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>we prove that &quot;</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000040;">/</span> <span style="color: #0000dd;">5</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; and 5 are products of primes.&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
			induce<span style="color: #008000;">&#40;</span>n<span style="color: #000040;">/</span><span style="color: #0000dd;">5</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			induce<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">5</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Having proven that &quot;</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000040;">/</span> <span style="color: #0000dd;">5</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; and 5 are products of primes,&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>we condlude that &quot;</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000040;">/</span> <span style="color: #0000dd;">5</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; * &quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #0000dd;">5</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>n <span style="color: #000040;">%</span> <span style="color: #0000dd;">3</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>we prove that &quot;</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000040;">/</span> <span style="color: #0000dd;">3</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; and 3 are products of primes.&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
			induce<span style="color: #008000;">&#40;</span>n<span style="color: #000040;">/</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			induce<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Having proven that &quot;</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000040;">/</span> <span style="color: #0000dd;">3</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; and 3 are products of primes,&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>we condlude that &quot;</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000040;">/</span> <span style="color: #0000dd;">3</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; * &quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #0000dd;">3</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>n <span style="color: #000040;">%</span> <span style="color: #0000dd;">2</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>we prove that &quot;</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000040;">/</span> <span style="color: #0000dd;">2</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; and 2 are products of primes.&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
			induce<span style="color: #008000;">&#40;</span>n<span style="color: #000040;">/</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			induce<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Having proven that &quot;</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000040;">/</span> <span style="color: #0000dd;">2</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; and 2 are products of primes,&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>we condlude that &quot;</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000040;">/</span> <span style="color: #0000dd;">2</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; * &quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #0000dd;">2</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>is a product of primes.&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #666666;">// prime():</span>
<span style="color: #666666;">// Determines if a number is prime</span>
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #0000ff;">bool</span> prime <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #666666;">// Variables</span>
	<span style="color: #0000ff;">bool</span> result<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span>  i<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Do work</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>n <span style="color: #000080;">&lt;=</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		result <span style="color: #000080;">=</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span> <span style="color: #666666;">// 1 is not prime</span>
&nbsp;
	<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>n <span style="color: #000080;">==</span> <span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">||</span> <span style="color: #008000;">&#40;</span>n <span style="color: #000080;">==</span> <span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		result <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span> <span style="color: #666666;">// Hard code 2 and 3</span>
&nbsp;
	<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>n <span style="color: #000040;">%</span> <span style="color: #0000dd;">2</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		result <span style="color: #000080;">=</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span> <span style="color: #666666;">// Get rid of evens</span>
&nbsp;
		<span style="color: #ff0000; font-style: italic;">/* All other cases are out, so now we check
		 to see if n is divisible by the odd
		 numbers from 3 on. */</span>
	<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
		i <span style="color: #000080;">=</span> <span style="color: #0000dd;">3</span><span style="color: #008080;">;</span>
		result <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span> <span style="color: #666666;">// Assume it's prime, then prove</span>
&nbsp;
		<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">true</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #ff0000; font-style: italic;">/* If i^2 is not a root of n, or if
			 n % i == 0. (Won't be larger than
			 the square.) */</span>
			<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>i <span style="color: #000040;">*</span> i <span style="color: #000080;">&gt;</span> n<span style="color: #008000;">&#41;</span> <span style="color: #000040;">||</span> <span style="color: #008000;">&#40;</span>n <span style="color: #000040;">%</span> i <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
				<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
			i <span style="color: #000040;">+</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span> <span style="color: #666666;">// Iterate by 2 to get odds</span>
		<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// End while</span>
&nbsp;
		<span style="color: #666666;">// Record the answer</span>
		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000040;">*</span> i <span style="color: #000080;">&gt;</span> n<span style="color: #008000;">&#41;</span>
			result <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">else</span>
			result <span style="color: #000080;">=</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// End if</span>
&nbsp;
&nbsp;
	<span style="color: #666666;">// Return the answer</span>
	<span style="color: #0000ff;">return</span> result<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div> ]]></content:encoded> <wfw:commentRss>http://jonlandrum.com/2012/04/16/inductive-reasoning-with-recursion/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Making a Queue From a Stack</title><link>http://jonlandrum.com/2012/04/10/making-a-queue-from-a-stack/</link> <comments>http://jonlandrum.com/2012/04/10/making-a-queue-from-a-stack/#comments</comments> <pubDate>Tue, 10 Apr 2012 23:31:25 +0000</pubDate> <dc:creator>Jonathan</dc:creator> <category><![CDATA[C/C++]]></category> <guid
isPermaLink="false">http://jonlandrum.com/?p=342</guid> <description><![CDATA[We&#8217;ve used stacks twice now (Writing a Drop-Out Stack and Using a Stack to Make a Checkbook Ledger), and they have already proved quite useful. Since a stack is so similar to a queue, let&#8217;s explore turning our stack into &#8230; <a
href="http://jonlandrum.com/2012/04/10/making-a-queue-from-a-stack/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>We&#8217;ve used stacks twice now (<a
href="http://jonlandrum.com/2012/04/05/writing-a-drop-out-stack/" title="Writing a Drop-Out Stack">Writing a Drop-Out Stack</a> and <a
href="http://jonlandrum.com/2012/04/09/using-a-stack-to-make-a-checkbook-ledger/" title="Using a Stack to Make a Checkbook Ledger">Using a Stack to Make a Checkbook Ledger</a>), and they have already proved quite useful. Since a stack is so similar to a <a
href="http://en.wikipedia.org/wiki/Queue_(abstract_data_type)" title="Article on Wikipedia">queue</a>, let&#8217;s explore turning our stack into one.</p><p>The first thing we need to change is our header file. I&#8217;ve renamed it <code>queue.h</code>, since that&#8217;s all it defines this time. There is really quite little to change in the header beyond changing the names of the methods. I have also changed the data type to be <code>int</code>, just because. As before, a more useful queue would be one that could handle generic objects, but that is beyond the scope of this activity.</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>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
</pre></td><td
class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;cstdlib&gt;</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;string&gt;</span>
&nbsp;
<span style="color: #339900;">#define SIZE 10</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> queue <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
	<span style="color: #ff0000; font-style: italic;">/* Instance Variables */</span>
	<span style="color: #0000ff;">int</span>  count<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span>  front<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span>  rear<span style="color: #008080;">;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
	<span style="color: #ff0000; font-style: italic;">/* Constructor */</span>
	queue<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #ff0000; font-style: italic;">/* Instance Methods */</span>
	<span style="color: #0000ff;">bool</span> isEmpty<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">void</span> enqueue<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> item<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span>  dequeue<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span>  first<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span>  size<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div><p>You&#8217;ll notice a couple of new variables, <code>front</code> and <code>rear</code>. These keep up with which cell of the array is the front of the queue and which is the rear. In implementing this queue, I have chosen a <a
href="http://en.wikipedia.org/wiki/Circular_buffer" title="Article on Wikipedia">circular array queue</a> as the structure. This structure made the most sense to me when studying data structures last semester. The only difference, really, is in how the <code>enqueue</code> and <code>dequeue</code> methods determine which cell of the array to use as their next location.</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>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
</pre></td><td
class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &quot;queue.h&quot;</span>
&nbsp;
&nbsp;
<span style="color: #0000ff;">int</span> data<span style="color: #008000;">&#91;</span>SIZE<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
&nbsp;
queue<span style="color: #008080;">::</span><span style="color: #007788;">queue</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	count  <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
	front  <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
	rear   <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> SIZE<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span>
		data<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color:#800080;">0.0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">bool</span> queue<span style="color: #008080;">::</span><span style="color: #007788;">isEmpty</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">bool</span> result<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>count <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
		result <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">else</span>
		result <span style="color: #000080;">=</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> result<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> queue<span style="color: #008080;">::</span><span style="color: #007788;">enqueue</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> item<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>count <span style="color: #000080;">&lt;</span> SIZE<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		data<span style="color: #008000;">&#91;</span>rear<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> item<span style="color: #008080;">;</span>
		rear <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>rear <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">%</span> SIZE<span style="color: #008080;">;</span>
		<span style="color: #000040;">++</span>count<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span>
		std<span style="color: #008080;">::</span><span style="color: #0000dd;">cerr</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Error: Attempt to enqueue to full queue<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> queue<span style="color: #008080;">::</span><span style="color: #007788;">dequeue</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">int</span> result <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>isEmpty<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		result <span style="color: #000080;">=</span> data<span style="color: #008000;">&#91;</span>front<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
		data<span style="color: #008000;">&#91;</span>front<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span>
		front <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>front <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">%</span> SIZE<span style="color: #008080;">;</span>
		<span style="color: #000040;">--</span>count<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span>
		std<span style="color: #008080;">::</span><span style="color: #0000dd;">cerr</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Error: Attempt to dequeue from an empty queue<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> result<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> queue<span style="color: #008080;">::</span><span style="color: #007788;">first</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">return</span> data<span style="color: #008000;">&#91;</span>front<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> queue<span style="color: #008080;">::</span><span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">return</span> count<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div><p>Beyond the few changes I have already mentioned, you can plainly see that a stack and a queue are quite similar. This is because they are practically the same structure. A stack can be thought of as a stack of plates, the top of the stack being both where you add new plates and where you retrieve a plate to use. A queue is like a line in a grocery store, the one who has been in line longest being the next one served.</p><p>But the two structures are arranged in the same order. The single difference is from where in the list you remove items. That is why it is so very easy to convert a stack into a queue and vice versa. Putting it to use is similarly simple, with our <code>main</code> method being almost identical to that from our stack program:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>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
</pre></td><td
class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &quot;queue.h&quot;</span>
&nbsp;
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">int</span>   count<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span>   item<span style="color: #008080;">;</span>
	queue myQueue<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;--------------------------------------------------------<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;               Programming a Queue in C++<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;--------------------------------------------------------<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;This program simulates a waiting line -- also called a<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;queue -- using a queue data structure.<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;This program only handles integers at the moment; other<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;data types would be trivial to implement.&quot;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>count <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> count <span style="color: #000080;">&lt;</span> SIZE<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>count<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Entry &quot;</span> <span style="color: #000080;">&lt;&lt;</span> count <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;: &quot;</span><span style="color: #008080;">;</span>
		<span style="color: #0000dd;">cin</span>  <span style="color: #000080;">&gt;&gt;</span> item<span style="color: #008080;">;</span>
		myQueue.<span style="color: #007788;">enqueue</span><span style="color: #008000;">&#40;</span>item<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Items in the list: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> myQueue.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>Now to dequeue those items:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>count <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> count <span style="color: #000080;">&lt;</span> SIZE<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>count<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Entry &quot;</span> <span style="color: #000080;">&lt;&lt;</span> count <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> myQueue.<span style="color: #007788;">dequeue</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div><p>As before, since we have three files to deal with, a <code>makefile</code> simplifies the process. And also as with the other files of this program, the makefile is nothing more than a search-and-replace change from the old program name to the new one:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td
class="code"><pre class="make" style="font-family:monospace;"><span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># Makefile for queue</span>
<span style="color: #339900; font-style: italic;">#</span>
&nbsp;
CC<span style="color: #004400;">=</span>g<span style="color: #004400;">++</span>
CFLAGS<span style="color: #004400;">=-</span>g <span style="color: #004400;">-</span>Wall
&nbsp;
queue<span style="color: #004400;">:</span> main<span style="color: #004400;">.</span>o queue<span style="color: #004400;">.</span>o
	<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CC</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CFLAGS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">-</span>o queue main<span style="color: #004400;">.</span>o queue<span style="color: #004400;">.</span>o
main<span style="color: #004400;">.</span>o<span style="color: #004400;">:</span> main<span style="color: #004400;">.</span>cpp queue<span style="color: #004400;">.</span>h
	<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CC</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CFLAGS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">-</span>c main<span style="color: #004400;">.</span>cpp
queue<span style="color: #004400;">.</span>o<span style="color: #004400;">:</span> queue<span style="color: #004400;">.</span>cpp queue<span style="color: #004400;">.</span>h
	<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CC</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CFLAGS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">-</span>c queue<span style="color: #004400;">.</span>cpp
&nbsp;
clean<span style="color: #004400;">:</span>
	rm <span style="color: #004400;">-</span>f queue <span style="color: #004400;">*.</span>o
&nbsp;
<span style="color: #339900; font-style: italic;"># End of makefile</span></pre></td></tr></table></div><p>Having done all of my <a
href="http://jonlandrum.com/2012/02/08/java-sentence-reverser/" title="Java Sentence Reverser">data structure programming in Java</a>, I am pleased to begin creating these same structures in C. This program worked flawlessly as designed, and can be expanded to be quite a useful addition to a larger application.</p><p>~Jonathan</p> ]]></content:encoded> <wfw:commentRss>http://jonlandrum.com/2012/04/10/making-a-queue-from-a-stack/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Using a Stack to Make a Checkbook Ledger</title><link>http://jonlandrum.com/2012/04/09/using-a-stack-to-make-a-checkbook-ledger/</link> <comments>http://jonlandrum.com/2012/04/09/using-a-stack-to-make-a-checkbook-ledger/#comments</comments> <pubDate>Tue, 10 Apr 2012 04:22:42 +0000</pubDate> <dc:creator>Jonathan</dc:creator> <category><![CDATA[C/C++]]></category> <guid
isPermaLink="false">http://jonlandrum.com/?p=341</guid> <description><![CDATA[The other day I wrote you about using a stack to make an undo button, and now I want to show you how you can use a stack to keep up with a running total in a ledger. This program &#8230; <a
href="http://jonlandrum.com/2012/04/09/using-a-stack-to-make-a-checkbook-ledger/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>The other day I wrote you about <a
href="http://jonlandrum.com/2012/04/05/writing-a-drop-out-stack/" title="Writing a Drop-Out Stack">using a stack to make an undo button</a>, and now I want to show you how you can use a stack to keep up with a running total in a ledger. This program still makes use of a <a
href="http://en.wikipedia.org/wiki/Stack_(abstract_data_type)" title="Article on Wikipedia">stack data structure</a> to organize the data, but there are a couple of new methods added:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td
class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> stack<span style="color: #008080;">::</span><span style="color: #007788;">addItem</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">float</span> item<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	push<span style="color: #008000;">&#40;</span>item<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	total <span style="color: #000040;">+</span><span style="color: #000080;">=</span> item<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">float</span> stack<span style="color: #008080;">::</span><span style="color: #007788;">returnTotal</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">return</span> total<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div><p>The first method, <code>addItem()</code>, extends the <code>push()</code> method by adding a running total to the items pushed. The second method, <code>returnTotal()</code>, simply gives the current total added. This is all there is to it. Everything else is a standard stack.</p><p>I&#8217;ve also split this program up into a <code>main.cpp</code> file &#8212; which contains the main method, a <code>checkbook.cpp</code> file &#8212; which defines the methods of the stack, and a <code>checkbook.h</code> file &#8212; which declares the methods and variables used by the stack. I have also created a <code>makefile</code> to make compiling easier. I&#8217;ll include it last. But first, here&#8217;s <code>checkbook.h</code>:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td
class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;cstdlib&gt;</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;string&gt;</span>
&nbsp;
<span style="color: #339900;">#define SIZE 10</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> stack <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
	<span style="color: #ff0000; font-style: italic;">/* Instance Variables */</span>
	<span style="color: #0000ff;">int</span>   count<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">float</span> total<span style="color: #008080;">;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
	<span style="color: #ff0000; font-style: italic;">/* Constructor */</span>
	stack<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #ff0000; font-style: italic;">/* Instance Methods */</span>
	<span style="color: #0000ff;">bool</span>  isEmpty<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">void</span>  push<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">float</span> item<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">float</span> pop<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">void</span>  addItem<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">float</span> item<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">float</span> returnTotal<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div><p>All of the headers go in this <code>.h</code> file, and this one header will be included in the other two files. Essentially, this code will be pasted into the head of each of those two files, so whatever is here will be repeated in both places at compile time.</p><p>This file won&#8217;t actually be compiled; it&#8217;s just used for definitions. As such, anything that has to be allocated cannot be included here or you&#8217;ll get strange errors. There is an array used for the stack, but it has to be declared in <code>checkbook.cpp</code> in order for the program to compile and link correctly. That file is the extension of this header, defining the bodies of the methods declared in this header file.</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>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
</pre></td><td
class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &quot;checkbook.h&quot;</span>
&nbsp;
&nbsp;
<span style="color: #0000ff;">float</span> data<span style="color: #008000;">&#91;</span>SIZE<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
&nbsp;
stack<span style="color: #008080;">::</span><span style="color: #007788;">stack</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	count <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
	total <span style="color: #000080;">=</span> <span style="color:#800080;">0.0</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> SIZE<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span>
		data<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color:#800080;">0.0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">bool</span> stack<span style="color: #008080;">::</span><span style="color: #007788;">isEmpty</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">bool</span> result<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>count <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
		result <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">else</span>
		result <span style="color: #000080;">=</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> result<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> stack<span style="color: #008080;">::</span><span style="color: #007788;">push</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">float</span> item<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>count <span style="color: #000080;">&lt;</span> SIZE<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		data<span style="color: #008000;">&#91;</span>count<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> item<span style="color: #008080;">;</span>
		<span style="color: #000040;">++</span>count<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span>
		std<span style="color: #008080;">::</span><span style="color: #0000dd;">cerr</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Error: Attempt to push to full stack<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">float</span> stack<span style="color: #008080;">::</span><span style="color: #007788;">pop</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">float</span> result <span style="color: #000080;">=</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>isEmpty<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #000040;">--</span>count<span style="color: #008080;">;</span>
		result <span style="color: #000080;">=</span> data<span style="color: #008000;">&#91;</span>count<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span>
		std<span style="color: #008080;">::</span><span style="color: #0000dd;">cerr</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Error: Attempt to pop from an empty stack<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> result<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> stack<span style="color: #008080;">::</span><span style="color: #007788;">addItem</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">float</span> item<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	push<span style="color: #008000;">&#40;</span>item<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	total <span style="color: #000040;">+</span><span style="color: #000080;">=</span> item<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">float</span> stack<span style="color: #008080;">::</span><span style="color: #007788;">returnTotal</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">return</span> total<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div><p>Now that our stack is declared and defined, we can use it in our main program.</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>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
</pre></td><td
class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &quot;checkbook.h&quot;</span>
&nbsp;
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">int</span>   count<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">float</span> item<span style="color: #008080;">;</span>
	stack myStack<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;---------------------------------------------------------<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;                      C++ Checkbook<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;---------------------------------------------------------<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;This program simulates a checkbook program. Enter values<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;for each checkbook entry as floating point numbers.<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>count <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> count <span style="color: #000080;">&lt;</span> SIZE<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>count<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Entry &quot;</span> <span style="color: #000080;">&lt;&lt;</span> count <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;: &quot;</span><span style="color: #008080;">;</span>
		<span style="color: #0000dd;">cin</span>  <span style="color: #000080;">&gt;&gt;</span> item<span style="color: #008080;">;</span>
		myStack.<span style="color: #007788;">addItem</span><span style="color: #008000;">&#40;</span>item<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Running total: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> myStack.<span style="color: #007788;">returnTotal</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>TOTAL: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> myStack.<span style="color: #007788;">returnTotal</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div><p>This program initializes a stack and then uses the methods of this stack to add numbers together. A stack isn&#8217;t totally necessary for this application, but this program could be extended to do further computations beyond simply adding numbers together. And since the structure is in place, it will be trivial to extend it in the future.</p><p>Now to compile it all. You can compile each file separately and then link them, or you can use a <code>makefile</code> to do all that for you. Additionally, defining a proper <code>makefile</code> has the added benefit of the <code>make clean</code> command, which removes all object code and executables created in the compilation process (useful while writing the program.)</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td
class="code"><pre class="make" style="font-family:monospace;"><span style="color: #339900; font-style: italic;">#</span>
<span style="color: #339900; font-style: italic;"># Makefile for checkbook</span>
<span style="color: #339900; font-style: italic;">#</span>
&nbsp;
CC<span style="color: #004400;">=</span>g<span style="color: #004400;">++</span>
CFLAGS<span style="color: #004400;">=-</span>g <span style="color: #004400;">-</span>Wall
&nbsp;
checkbook<span style="color: #004400;">:</span> main<span style="color: #004400;">.</span>o checkbook<span style="color: #004400;">.</span>o
	<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CC</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CFLAGS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">-</span>o checkbook main<span style="color: #004400;">.</span>o checkbook<span style="color: #004400;">.</span>o
main<span style="color: #004400;">.</span>o<span style="color: #004400;">:</span> main<span style="color: #004400;">.</span>cpp checkbook<span style="color: #004400;">.</span>h
	<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CC</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CFLAGS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">-</span>c main<span style="color: #004400;">.</span>cpp
checkbook<span style="color: #004400;">.</span>o<span style="color: #004400;">:</span> checkbook<span style="color: #004400;">.</span>cpp checkbook<span style="color: #004400;">.</span>h
	<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CC</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CFLAGS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">-</span>c checkbook<span style="color: #004400;">.</span>cpp
&nbsp;
clean<span style="color: #004400;">:</span>
	rm <span style="color: #004400;">-</span>f checkbook <span style="color: #004400;">*.</span>o
&nbsp;
<span style="color: #339900; font-style: italic;"># End of makefile</span></pre></td></tr></table></div><p>This program is a good platform for building stack-based applications that span multiple headers and files, and it was good practice in writing classes. I&#8217;m quite happy with the outcome.</p><p>~Jonathan</p> ]]></content:encoded> <wfw:commentRss>http://jonlandrum.com/2012/04/09/using-a-stack-to-make-a-checkbook-ledger/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Writing a Drop-Out Stack</title><link>http://jonlandrum.com/2012/04/05/writing-a-drop-out-stack/</link> <comments>http://jonlandrum.com/2012/04/05/writing-a-drop-out-stack/#comments</comments> <pubDate>Thu, 05 Apr 2012 12:17:01 +0000</pubDate> <dc:creator>Jonathan</dc:creator> <category><![CDATA[C/C++]]></category> <guid
isPermaLink="false">http://jonlandrum.com/?p=331</guid> <description><![CDATA[Last year in Data Structures we had to write what is known as a drop-out stack, which is exactly like a regular stack, only with one modification: when the contents of the stack reaches its maximum, the oldest item on &#8230; <a
href="http://jonlandrum.com/2012/04/05/writing-a-drop-out-stack/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>Last year in Data Structures we had to write what is known as a drop-out <a
href="http://en.wikipedia.org/wiki/Stack_(abstract_data_type)" title="Article on Wikipedia">stack</a>, which is exactly like a regular stack, only with one modification: when the contents of the stack reaches its maximum, the oldest item on the stack (the one at the bottom) &#8220;drops off&#8221; and is replaced by the next oldest item.</p><p>These types of stacks are frequently used in cases such as an undo button. You have, for instance, a ten-step history you can go back to. But when you make the eleventh change, the first one drops out and you can no longer go back that far. The reason many programs do not have infinite undo&#8217;s is because it eats up a lot of memory. So in the interest of being efficient, they limit how many steps you can recall.</p><p>I wrote this undo stack last night in about ten minutes using <a
href="http://jonlandrum.com/category/c/" title="Code I’ve written in C or C++ (or some other derivative of C).">C++</a>. The <code>stack_dropOut()</code> method only does two things: first, it counts through the stack from 0 to <code>SIZE</code> (defined as the maximum size of the stack), shuffling to the current position the next highest item. Then it decrements the item count by one. So if your limit is 10, it overwrites from the bottom &#8212; the first item &#8212; beginning with item 2, and goes all the way to item 9, and then it decrements the count. When the next item is pushed to the stack, it goes in position 10 (since the count is set to that position.)</p><p>I put all the contents of the stack into a separate header file in order to keep <code>main()</code> cleaner. So let&#8217;s go ahead and write that, and then worry about writing the stack:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>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
</pre></td><td
class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &quot;stack.h&quot;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	stack myStack<span style="color: #008080;">;</span>
	stack_init<span style="color: #008000;">&#40;</span>myStack<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> item<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Demonstrate the dropout</span>
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> c <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> c <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">15</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>c<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Enter a number to the stack: &quot;</span><span style="color: #008080;">;</span>
		<span style="color: #0000dd;">cin</span>  <span style="color: #000080;">&gt;&gt;</span> item<span style="color: #008080;">;</span>
		stack_push<span style="color: #008000;">&#40;</span>myStack, item<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		stack_toString<span style="color: #008000;">&#40;</span>myStack<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Demonstrate an empty collection exception</span>
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> c <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> c <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">11</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>c<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		stack_pop<span style="color: #008000;">&#40;</span>myStack<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		stack_toString<span style="color: #008000;">&#40;</span>myStack<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div><p>I kept <code>main()</code> incredibly brief, because I really just wanted a quick example program that demonstrated the two things I wanted to show: first, of course, the <code>stack_dropOut()</code> method when <code>stack_push()</code> is called too many times, and then I wanted to demonstrate what happens when the <code>stack_pop()</code> method is called when the stack is empty. A proper stack should throw an empty collection exception, but this works in a pinch.</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>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
</pre></td><td
class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#define SIZE 10</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
&nbsp;
<span style="color: #0000ff;">struct</span> stack <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">int</span> count<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> data<span style="color: #008000;">&#91;</span>SIZE<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">inline</span> <span style="color: #0000ff;">void</span> stack_init<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">struct</span> stack <span style="color: #000040;">&amp;</span> the_stack<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	the_stack.<span style="color: #007788;">count</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">inline</span> <span style="color: #0000ff;">void</span> stack_dropOut<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">struct</span> stack <span style="color: #000040;">&amp;</span> the_stack<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> c <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> c <span style="color: #000080;">&lt;</span> SIZE<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>c<span style="color: #008000;">&#41;</span>
		the_stack.<span style="color: #007788;">data</span><span style="color: #008000;">&#91;</span>c<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> the_stack.<span style="color: #007788;">data</span><span style="color: #008000;">&#91;</span>c <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #000040;">--</span>the_stack.<span style="color: #007788;">count</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">inline</span> <span style="color: #0000ff;">bool</span> stack_isEmpty<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">struct</span> stack <span style="color: #000040;">&amp;</span> the_stack<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span>the_stack.<span style="color: #007788;">count</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">inline</span> <span style="color: #0000ff;">void</span> stack_push<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">struct</span> stack <span style="color: #000040;">&amp;</span> the_stack, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> item<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>the_stack.<span style="color: #007788;">count</span> <span style="color: #000080;">==</span> SIZE<span style="color: #008000;">&#41;</span>
		stack_dropOut<span style="color: #008000;">&#40;</span>the_stack<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	the_stack.<span style="color: #007788;">data</span><span style="color: #008000;">&#91;</span>the_stack.<span style="color: #007788;">count</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> item<span style="color: #008080;">;</span>
	<span style="color: #000040;">++</span>the_stack.<span style="color: #007788;">count</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">inline</span> <span style="color: #0000ff;">int</span> stack_pop<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">struct</span> stack <span style="color: #000040;">&amp;</span> the_stack<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>stack_isEmpty<span style="color: #008000;">&#40;</span>the_stack<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Error: Empty Collection Exception&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">return</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #000040;">--</span>the_stack.<span style="color: #007788;">count</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span>the_stack.<span style="color: #007788;">data</span><span style="color: #008000;">&#91;</span>the_stack.<span style="color: #007788;">count</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">inline</span> <span style="color: #0000ff;">int</span> stack_peek<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">struct</span> stack <span style="color: #000040;">&amp;</span> the_stack<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span>the_stack.<span style="color: #007788;">data</span><span style="color: #008000;">&#91;</span>the_stack.<span style="color: #007788;">count</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">inline</span> <span style="color: #0000ff;">bool</span> stack_size<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">struct</span> stack <span style="color: #000040;">&amp;</span> the_stack<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span>the_stack.<span style="color: #007788;">count</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">inline</span> <span style="color: #0000ff;">void</span> stack_toString<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">struct</span> stack <span style="color: #000040;">&amp;</span> the_stack<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> c <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> c <span style="color: #000080;">&lt;</span> the_stack.<span style="color: #007788;">count</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>c<span style="color: #008000;">&#41;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Item &quot;</span> <span style="color: #000080;">&lt;&lt;</span> c <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> the_stack.<span style="color: #007788;">data</span><span style="color: #008000;">&#91;</span>c<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div><p>I&#8217;m really glad to get this one written in C++. The last one I wrote was in <a
href="http://jonlandrum.com/category/java/" title="Code I’ve written in Oracle’s Java programming language.">Java</a>, and it used <a
href="http://en.wikipedia.org/wiki/Generic_programming" title="Article on Wikipedia">generics</a> in order to accept anything to the stack. As such, it was quite a few files in length. This stack only accepts integers at the moment, but it can be extended to be generic later on, and it&#8217;s just a driver and a single header.</p><p>~Jonathan</p> ]]></content:encoded> <wfw:commentRss>http://jonlandrum.com/2012/04/05/writing-a-drop-out-stack/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Perfecting Combinatorics</title><link>http://jonlandrum.com/2012/04/02/perfecting-combinatorics/</link> <comments>http://jonlandrum.com/2012/04/02/perfecting-combinatorics/#comments</comments> <pubDate>Mon, 02 Apr 2012 19:01:52 +0000</pubDate> <dc:creator>Jonathan</dc:creator> <category><![CDATA[C/C++]]></category> <guid
isPermaLink="false">http://jonlandrum.com/?p=327</guid> <description><![CDATA[I&#8217;ve written you now three times on this subject, first in printing Pascal&#8217;s Triangle, then using that to find binomial combinations in the triangle, and finally I wrote you about making the combinatorics algorithm more efficient. Today I want to &#8230; <a
href="http://jonlandrum.com/2012/04/02/perfecting-combinatorics/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>I&#8217;ve written you now three times on this subject, first in <a
href="http://jonlandrum.com/2012/03/30/pascals-triangle/" title="Pascal's Triangle">printing Pascal&#8217;s Triangle</a>, then using that to <a
href="http://jonlandrum.com/2012/03/31/using-pascals-triangle-to-find-binomial-combinations/" title="Using Pascal's Triangle to Find Binomial Combinations">find binomial combinations in the triangle</a>, and finally I wrote you about <a
href="http://jonlandrum.com/2012/04/02/revisiting-combinatorics/" title="Revisiting Combinatorics">making the combinatorics algorithm more efficient</a>. Today I want to present the final step in the efficiency process: getting rid of recursive functions altogether.</p><p>Throughout my mathematics and computer science courses, extensive use has been made of factorial functions, particularly in probability and statistics. As such, it&#8217;s interesting to find an algorithm that cuts right to the chase and foregoes the long, arduous factorial calculations. Even doing these on a computer will severely slow your program down, as we have seen over the past week. What we need is an equation that will give us the answer straightaway, or at least give it to us after an iteration. And that&#8217;s just what we&#8217;ve got.</p><p><img
src="http://jonlandrum.com/wp-content/uploads/2012/04/binomial1.gif" alt="Perfecting binomial calculations" title="Perfecting binomial calculations" width="125" height="58" class="alignnone size-full wp-image-328" /></p><p>This is an algorithm similar to <a
href="http://jonlandrum.com/2012/02/22/what-sigma-notation-means-to-a-computer-scientist/" title="What Sigma Notation Means to a Computer Scientist">sigma notation</a>, only in this case we multiply in each cycle of the loop rather than add. By placing this set of instructions in a <code>for</code> loop we will produce our answer almost immediately. And this method will allow much larger combinations, as the number is simply multiplied up to a point, rather than being calculated as a factorial first. Another benefit: our code is now 73 lines shorter.</p><p>~Jonathan</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
</pre></td><td
class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// ==================================================</span>
<span style="color: #666666;">// Programmer:  Jonathan Landrum</span>
<span style="color: #666666;">// Date:        29 March 2012</span>
<span style="color: #666666;">// ==================================================</span>
<span style="color: #666666;">// Program:     combinations.cpp</span>
<span style="color: #666666;">// Purpose:     Calculates the number of combinations</span>
<span style="color: #666666;">//		that can be made with given input of</span>
<span style="color: #666666;">//		discrete items.</span>
<span style="color: #666666;">// Assumptions: - Input is an integer. Real input</span>
<span style="color: #666666;">//		  will be cast to the next lowest</span>
<span style="color: #666666;">//		  integer.</span>
<span style="color: #666666;">// ==================================================</span>
&nbsp;
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;string&gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
&nbsp;
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #666666;">// main():</span>
<span style="color: #666666;">// Calls factorial() and returns results.</span>
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #0000ff;">int</span> main <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
	<span style="color: #666666;">// Variables</span>
	<span style="color: #0000ff;">float</span> n, r<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span>   group, choose<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">long</span> <span style="color: #0000ff;">int</span> result <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Introduce the program</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;------------------------------------&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;-     Combinatorics Calculator     -&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;------------------------------------&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;              /n<span style="color: #000099; font-weight: bold;">\\</span>      n!&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;        nCr = | | = --------&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;              <span style="color: #000099; font-weight: bold;">\\</span>r/   r!(n-r)!&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;This program calculates how many&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;combinations can be made of a given&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;number of choices, and a given sized&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;group from which to choose.&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Get user input</span>
	<span style="color: #0000ff;">try</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">do</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Size of the group (n): &quot;</span><span style="color: #008080;">;</span>
&nbsp;
			<span style="color: #0000dd;">cin</span>  <span style="color: #000080;">&gt;&gt;</span> n<span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>n <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
		<span style="color: #0000ff;">do</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Number of choices (r): &quot;</span><span style="color: #008080;">;</span>
&nbsp;
			<span style="color: #0000dd;">cin</span>  <span style="color: #000080;">&gt;&gt;</span> r<span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>r <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
		group  <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span>n<span style="color: #008080;">;</span>
		choose <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span>r<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">catch</span> <span style="color: #008000;">&#40;</span>...<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Error: &lt;&lt; Unexpected Input &gt;&gt;&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>choose <span style="color: #000080;">&gt;</span> group<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #0000dd;">0</span> <span style="color: #000080;">&lt;&lt;</span> endl <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;=</span> choose<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span>
		result <span style="color: #000040;">*</span><span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">float</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>group <span style="color: #000040;">-</span> choose <span style="color: #000040;">+</span> i<span style="color: #008000;">&#41;</span><span style="color: #000040;">/</span>i<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> result <span style="color: #000080;">&lt;&lt;</span> endl <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// End main</span></pre></td></tr></table></div> ]]></content:encoded> <wfw:commentRss>http://jonlandrum.com/2012/04/02/perfecting-combinatorics/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Revisiting Combinatorics</title><link>http://jonlandrum.com/2012/04/02/revisiting-combinatorics/</link> <comments>http://jonlandrum.com/2012/04/02/revisiting-combinatorics/#comments</comments> <pubDate>Mon, 02 Apr 2012 05:31:13 +0000</pubDate> <dc:creator>Jonathan</dc:creator> <category><![CDATA[C/C++]]></category> <guid
isPermaLink="false">http://jonlandrum.com/?p=324</guid> <description><![CDATA[I&#8217;ve written twice this week about creating Pascal&#8217;s Triangle and using it to find binomial combinations. I now want to present the first of two more-efficient algorithms, given me by my instructor, Dr. Glenn Wiggins. I&#8217;ve mentioned before that iterative &#8230; <a
href="http://jonlandrum.com/2012/04/02/revisiting-combinatorics/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>I&#8217;ve written twice this week about <a
href="http://jonlandrum.com/2012/03/30/pascals-triangle/" title="Pascal's Triangle">creating Pascal&#8217;s Triangle</a> and <a
href="http://jonlandrum.com/2012/03/31/using-pascals-triangle-to-find-binomial-combinations/" title="Using Pascal's Triangle to Find Binomial Combinations">using it to find binomial combinations</a>. I now want to present the first of two more-efficient algorithms, given me by my instructor, Dr. Glenn Wiggins.</p><p>I&#8217;ve mentioned before that iterative and recursive solutions are fine, but if you can find an algorithmic solution you&#8217;ll be much better off. This solution still makes use of the <a
href="http://jonlandrum.com/2012/02/15/calculating-factorials-with-a-recursive-function/" title="Calculating Factorials with a Recursive Function"><code>factorial()</code> function</a> from before, but its use is much more limited.</p><p>The calculation is taken from a method for finding <a
href="http://en.wikipedia.org/wiki/Binomial_coefficient" title="Article on Wikipedia">Binomial coefficients</a>:</p><p><img
src="http://jonlandrum.com/wp-content/uploads/2012/04/binomial.gif" alt="Calculation for finding Binomial Coefficients" title="Calculation for finding Binomial Coefficients" width="389" height="58" class="alignnone size-full wp-image-325" /></p><p>With this algorithm and our <code>factorial()</code> function, returning our answer is a much faster affair. You will also notice that with successive improvements, larger numbers can be handled. That is because the less you use a recursive function, the more efficient your program will be as a whole. But there is actually an even better method, and I&#8217;ll show you that one next.</p><p>~Jonathan</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
</pre></td><td
class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// ==================================================</span>
<span style="color: #666666;">// Programmer:  Jonathan Landrum</span>
<span style="color: #666666;">// Date:        29 March 2012</span>
<span style="color: #666666;">// ==================================================</span>
<span style="color: #666666;">// Program:     combinations.cpp</span>
<span style="color: #666666;">// Purpose:     Calculates the number of combinations</span>
<span style="color: #666666;">//		that can be made with given input of</span>
<span style="color: #666666;">//		discrete items.</span>
<span style="color: #666666;">// Assumptions: - Input is an integer. Real input</span>
<span style="color: #666666;">//		  will be cast to the next lowest</span>
<span style="color: #666666;">//		  integer.</span>
<span style="color: #666666;">// Issues:      - The function factorial() slows down</span>
<span style="color: #666666;">//		  significantly for input greater</span>
<span style="color: #666666;">//		  than 23 due to the fact that they</span>
<span style="color: #666666;">//		  use recursion to calculate their</span>
<span style="color: #666666;">//		  results.</span>
<span style="color: #666666;">// ==================================================</span>
&nbsp;
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;string&gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
&nbsp;
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #666666;">// Function Prototypes</span>
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #0000ff;">long</span> <span style="color: #0000ff;">int</span> factorial <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span> <span style="color: #0000ff;">int</span> in<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
&nbsp;
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #666666;">// main():</span>
<span style="color: #666666;">// Calls factorial() and returns results.</span>
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #0000ff;">int</span> main <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
	<span style="color: #666666;">// Variables</span>
	<span style="color: #0000ff;">float</span> n, r<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span>   group, choose<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">long</span> <span style="color: #0000ff;">int</span> result <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Introduce the program</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;------------------------------------&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;-     Combinatorics Calculator     -&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;------------------------------------&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;              /n<span style="color: #000099; font-weight: bold;">\\</span>      n!&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;        nCr = | | = --------&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;              <span style="color: #000099; font-weight: bold;">\\</span>r/   r!(n-r)!&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;This program calculates how many&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;combinations can be made of a given&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;number of choices, and a given sized&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;group from which to choose.&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Get user input</span>
	<span style="color: #0000ff;">try</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">do</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Size of the group (n): &quot;</span><span style="color: #008080;">;</span>
&nbsp;
			<span style="color: #0000dd;">cin</span>  <span style="color: #000080;">&gt;&gt;</span> n<span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>n <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
		<span style="color: #0000ff;">do</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Number of choices (r): &quot;</span><span style="color: #008080;">;</span>
&nbsp;
			<span style="color: #0000dd;">cin</span>  <span style="color: #000080;">&gt;&gt;</span> r<span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>r <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
		group  <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span>n<span style="color: #008080;">;</span>
		choose <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span>r<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">catch</span> <span style="color: #008000;">&#40;</span>...<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Error: &lt;&lt; Unexpected Input &gt;&gt;&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>choose <span style="color: #000080;">&gt;</span> group<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #0000dd;">0</span> <span style="color: #000080;">&lt;&lt;</span> endl <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	result <span style="color: #000080;">=</span> group<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;=</span> choose <span style="color: #000040;">-</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span>
		result <span style="color: #000040;">*</span><span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>group <span style="color: #000040;">-</span> i<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #008000;">&#40;</span>result<span style="color: #000040;">/</span>factorial<span style="color: #008000;">&#40;</span>choose<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// End main</span>
&nbsp;
&nbsp;
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #666666;">// factorial():</span>
<span style="color: #666666;">//</span>
<span style="color: #666666;">// Recursive function.</span>
<span style="color: #666666;">// Returns int, accepts (int in).</span>
<span style="color: #666666;">// </span>
<span style="color: #666666;">// Calculates the factorial of the given number.</span>
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #0000ff;">long</span> <span style="color: #0000ff;">int</span> factorial <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span> <span style="color: #0000ff;">int</span> in<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">long</span> <span style="color: #0000ff;">int</span> result <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>in <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
		result <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">else</span>
		result <span style="color: #000080;">=</span> in <span style="color: #000040;">*</span> factorial<span style="color: #008000;">&#40;</span>in <span style="color: #000040;">-</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> result<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// End factorial</span></pre></td></tr></table></div> ]]></content:encoded> <wfw:commentRss>http://jonlandrum.com/2012/04/02/revisiting-combinatorics/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Using Pascal&#8217;s Triangle to Find Binomial Combinations</title><link>http://jonlandrum.com/2012/03/31/using-pascals-triangle-to-find-binomial-combinations/</link> <comments>http://jonlandrum.com/2012/03/31/using-pascals-triangle-to-find-binomial-combinations/#comments</comments> <pubDate>Sun, 01 Apr 2012 03:18:37 +0000</pubDate> <dc:creator>Jonathan</dc:creator> <category><![CDATA[C/C++]]></category> <guid
isPermaLink="false">http://jonlandrum.com/?p=319</guid> <description><![CDATA[I wrote the other day about calculating Pascal&#8217;s Triangle using C++, but I didn&#8217;t include all the interesting facts about Pascal&#8217;s Triangle. One of the more interesting ones is its use in Combinatorics as a sort of shortcut to finding &#8230; <a
href="http://jonlandrum.com/2012/03/31/using-pascals-triangle-to-find-binomial-combinations/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>I wrote the other day about <a
href="http://jonlandrum.com/2012/03/30/pascals-triangle/" title="Pascal's Triangle">calculating Pascal&#8217;s Triangle using C++</a>, but I didn&#8217;t include all the interesting facts about Pascal&#8217;s Triangle. One of the more interesting ones is its use in <a
href="http://en.wikipedia.org/wiki/Combinatorics" title="Article on Wikipedia">Combinatorics</a> as a sort of shortcut to finding binomial coefficients. If you have the first few rows of the triangle memorized, you don&#8217;t have to calculate the coefficients of a binomial equation, such as (<i>x</i> + <i>y</i>)<small><sup><i>n</i></sup></small>.</p><p>These coefficients are useful in statistics when calculating the binomial distribution. As such, one frequently finds himself performing this calculation:</p><p><img
src="http://jonlandrum.com/wp-content/uploads/2012/03/binomial.png" alt="Binomial Calculation" title="Binomial Calculation" width="147" height="51" class="alignnone size-full wp-image-320" /></p><p>Pascal&#8217;s Triangle is calculable with the <code>pascal()</code> function I wrote in the last post, but it is also calculable using this binomial calculation. This is the second method I talked about in that post, and it does use three calls to a <a
href="http://jonlandrum.com/2012/02/15/calculating-factorials-with-a-recursive-function/" title="Calculating Factorials with a Recursive Function"><code>factorial()</code> function</a>, as you can see.</p><p>I have not written this version to produce a Pascal Triangle. It can easily be made to do so, as this function calculates each coefficient just as the <code>pascal()</code> function does. Simply placing the call in a <code>for</code> loop will return successive values. Instead, this version returns an individual coefficient, which is also the value of a Binomial equation. This value is the number of combinations that can be made from a set of size <i>n</i> when making <i>r</i> choices.</p><p>If you have a set of size 4 &#8212; say, 4 apples &#8212; and you want to choose 3, how many different combinations can you make? Such a trivial equation is easy to solve: the answer is 4. But what about for larger sets and larger selections? An interesting thing about Pascal&#8217;s Triangle is you can immediately find your answer by selecting the row that is the same number as the size of the group, and the element of that row that is the same number as the size of the selection.</p><p><img
src="http://jonlandrum.com/wp-content/uploads/2012/03/pascal-triangle.png" alt="Calculating Binomial coefficients with Pascal's Triangle" title="Calculating Binomial coefficients with Pascal's Triangle" width="300" height="216" class="alignnone size-full wp-image-322" /></p><p>You begin counting with 0 in both cases, and that makes sense, as you can select 0 items from a set of size 0 in exactly 1 way: by selecting nothing at all. In the example above, you count down to row 4 (since there are four apples from which to choose), and you count over to the third number (since you are selecting three apples). The number in that position is your answer: 4. This program does this for you, using the <code>factorial()</code> function and the aforementioned binomial algorithm.</p><p>An interesting thing to note, if the number of choices is greater than the size of the group, the program returns 0. There is a simple explanation for this behavior. In our example above, if you wanted to instead pick 5 apples from the group of 4, you would get your wish 0 times. It cannot be done. There was actually a question quite similar to this on one of my tests in Probability and Statistics. If you think of the region beyond the triangle as being populated with 0&rsquo;s, then the result is perfectly plausable: row 4, column 5. And this program correctly returns 0 in this case. Combinatorics is a fascinating field of mathematics.</p><p>~Jonathan</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
</pre></td><td
class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// ==================================================</span>
<span style="color: #666666;">// Programmer:  Jonathan Landrum</span>
<span style="color: #666666;">// Date:        29 March 2012</span>
<span style="color: #666666;">// ==================================================</span>
<span style="color: #666666;">// Program:     combinations.cpp</span>
<span style="color: #666666;">// Purpose:     Calculates the number of combinations</span>
<span style="color: #666666;">//		that can be made with given input of</span>
<span style="color: #666666;">//		discrete items.</span>
<span style="color: #666666;">// Assumptions: - Input is an integer. Real input</span>
<span style="color: #666666;">//		  will be cast to the next lowest</span>
<span style="color: #666666;">//		  integer.</span>
<span style="color: #666666;">// Issues:      - The function factorial() slows down</span>
<span style="color: #666666;">//		  significantly for input greater</span>
<span style="color: #666666;">//		  than 23 due to the fact that it</span>
<span style="color: #666666;">//		  uses recursion to calculate its</span>
<span style="color: #666666;">//		  result.</span>
<span style="color: #666666;">// ==================================================</span>
&nbsp;
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;string&gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
&nbsp;
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #666666;">// Function Prototypes</span>
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #0000ff;">long</span> <span style="color: #0000ff;">int</span> factorial <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span> <span style="color: #0000ff;">int</span> in<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
&nbsp;
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #666666;">// main():</span>
<span style="color: #666666;">// Calls factorial() and returns results.</span>
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #0000ff;">int</span> main <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
	<span style="color: #666666;">// Variables</span>
	<span style="color: #0000ff;">float</span> n, r<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span>   group, choose<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Introduce the program</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;-----------------------------------&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;-     Combinations Calculator     -&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;-----------------------------------&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;              /n<span style="color: #000099; font-weight: bold;">\\</span>      n!&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;        nCr = | | = --------&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;              <span style="color: #000099; font-weight: bold;">\\</span>r/   r!(n-r)!&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;This program calculates how many&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;combinations can be made of a given&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;number of choices, and a given sized&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;group from which to choose.&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Get user input</span>
	<span style="color: #0000ff;">try</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">do</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Size of the group (n): &quot;</span><span style="color: #008080;">;</span>
&nbsp;
			<span style="color: #0000dd;">cin</span>  <span style="color: #000080;">&gt;&gt;</span> n<span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>n <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
		<span style="color: #0000ff;">do</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Number of choices (r): &quot;</span><span style="color: #008080;">;</span>
&nbsp;
			<span style="color: #0000dd;">cin</span>  <span style="color: #000080;">&gt;&gt;</span> r<span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>r <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
		group  <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span>n<span style="color: #008080;">;</span>
		choose <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span>r<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">catch</span> <span style="color: #008000;">&#40;</span>...<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Error: &lt;&lt; Unexpected Input &gt;&gt;&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>choose <span style="color: #000080;">&gt;</span> group<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #0000dd;">0</span> <span style="color: #000080;">&lt;&lt;</span> endl <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #008000;">&#40;</span>factorial<span style="color: #008000;">&#40;</span>group<span style="color: #008000;">&#41;</span><span style="color: #000040;">/</span><span style="color: #008000;">&#40;</span>factorial<span style="color: #008000;">&#40;</span>choose<span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> factorial<span style="color: #008000;">&#40;</span>group <span style="color: #000040;">-</span> choose<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// End main</span>
&nbsp;
&nbsp;
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #666666;">// factorial():</span>
<span style="color: #666666;">//</span>
<span style="color: #666666;">// Recursive function.</span>
<span style="color: #666666;">// Returns long int, accepts (long int).</span>
<span style="color: #666666;">// </span>
<span style="color: #666666;">// Calculates the factorial of the given number.</span>
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #0000ff;">long</span> <span style="color: #0000ff;">int</span> factorial <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span> <span style="color: #0000ff;">int</span> in<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">long</span> <span style="color: #0000ff;">int</span> result <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>in <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #008000;">&#41;</span>
		result <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">else</span>
		result <span style="color: #000080;">=</span> in <span style="color: #000040;">*</span> factorial<span style="color: #008000;">&#40;</span>in <span style="color: #000040;">-</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> result<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// End factorial</span></pre></td></tr></table></div> ]]></content:encoded> <wfw:commentRss>http://jonlandrum.com/2012/03/31/using-pascals-triangle-to-find-binomial-combinations/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> </channel> </rss>
