<?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>Talk Binary &#187; recursive</title>
	<atom:link href="http://talkbinary.com/tag/recursive/feed/" rel="self" type="application/rss+xml" />
	<link>http://talkbinary.com</link>
	<description>Programming Resources, Technology, Computers</description>
	<lastBuildDate>Mon, 06 Feb 2012 17:01:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>C++ Recursion &#8211; Reverse elements of a vector</title>
		<link>http://talkbinary.com/programming/c/c-recursion-reverse-elements-of-a-vector/</link>
		<comments>http://talkbinary.com/programming/c/c-recursion-reverse-elements-of-a-vector/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 22:09:48 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[c++ recursion]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[iteration]]></category>
		<category><![CDATA[iterative]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[recursive]]></category>
		<category><![CDATA[reverse]]></category>
		<category><![CDATA[Swapping]]></category>
		<category><![CDATA[vector]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=2327</guid>
		<description><![CDATA[Reversing the elements of a vector using recursion Below is an example of how to reverse the elements of a vector using recursion. This tutorial expects you to understand the following. C++ Recursion Example of reversing the elements of a vector Below is an example of how one would reverse the elements of a vector<a class="moretag" href="http://talkbinary.com/programming/c/c-recursion-reverse-elements-of-a-vector/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<h2 id="section-1">Reversing the elements of a vector using recursion</h2>
<p>Below is an example of how to reverse the elements of a vector using recursion. This tutorial expects you to understand the following.</p>
<p><a href="http://talkbinary.com/programming/c/introduction-to-recursion-in-c/">C++ Recursion</a></p>
<blockquote>
<h3 id="section-2">Example of reversing the elements of a vector</h3>
<p>Below is an example of how one would reverse the elements of a vector by simply intuition.</p>
<pre lang="c++">
// Our vector with size 5
[ X ] [ B ] [ A ] [ N ] [ Y ]
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ]

//Step 1: Swap entry 0 (first) with the entry n (last)
[ Y ] [ B ] [ A ] [ N ] [ X ]
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] 

//Step 2: Swap entry 1 with entry 3 (n -1)
[ Y ] [ N ] [ A ] [ B ] [ X ]
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ]

//Step 3: No swap needed for 3rd entry (or middle entry)
[ Y ] [ N ] [ A ] [ B ] [ X ]
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ]
</pre>
<p><br/><br />
Notice how once we arrive at the entry in the middle of the vector we don&#8217;t continue. Why? The values beyond this entry have already been swapped. Keep this in mind.</p>
</blockquote>
<h2 id="section-3">Reversing the elements of a vector using iteration</h2>
<p>Before we try making a recursive function to reverse the elements of a vector, let&#8217;s make one using iteration. By following the method described in the method above, it&#8217;s easy to see that we are simply swapping the ith and (nth &#8211; ith) variables <strong>(Example: the 0th and the 4th (4th &#8211; 0th) element)</strong>.</p>
<blockquote><h3 id="section-4">Reversing the elements of a vector using iteration</h3>
<p>The iterative solution for reversing the elements of a vector is below. Make sure you are trying all these functions if you haven&#8217;t before on your own.</p>
<pre lang="c++">
//We pass the vector by reference
void reverseElements(vector <int> &#038;vec) {

     //Temp variable for swapping
     int temp;

     int n = vec.size()-1;

     //Iterate vec.size()/2 times
     for ( int i = 0; i < vec.size()/2; i++ ) {

          // The swapping of elements
          temp = vec[i];
          vec[i] = vec[n-i];
          vec[n] = temp;

     }

     return;
}</pre>
<p><br/><br />
Make sure you understand this completely before moving forward. Try working it out by hand.</p>
</blockquote>
<p><span id="more-2327"></span></p>
<h2 id="section-1">Reversing the elements of a vector using recursion</h2>
<p>So how would we start? What would the function header be?</p>
<blockquote>
<h3 id="section-6">Function header for reversing elements of a vector</h3>
<p>So what would we need for our function header? We need the vector of course and also passed in by reference. Then we also need a way of knowing the two elements that are going to be swapped. </p>
<pre lang="c++">void revVector(vector <int> &#038;vec, int lo, int hi);

// Which would be called with the following //
vector <int> a;

// Populate a...

revVector(a, 0, a.size()-1);
</pre>
<p><br/><br />
We'll name this revVector for know and the two variables to be swapped lo and hi. How are we going to use these two variables? Every new recursive calls we will increase lo and decrease hi until our base case.</p>
</blockquote>
<p>So what would be our base case? </p>
<blockquote>
<h3 id="section-7">Base case for reversing elements of a vector</h3>
<p>Your first guess might be </p>
<pre lang="c++">if ( lo == hi ) return; </pre>
<p><br/>But, what if we have an even number of entries? This wouldn't be the case. Let's refine it.</p>
<pre lang="c++">if ( lo >= hi ) return; </pre>
<p><br/>Now that would take care of when the vector is of even or odd size. Why? Try figuring it out if you haven't.
</p></blockquote>
<p>Below is the rest of the function.</p>
<blockquote><h3 id="section-8">Solution</h3>
<pre lang="c++">void revVector(vector <int> &#038;vec, int lo, int hi) {

     // First base case //
     if ( lo >= hi ) return;

     // The swapping
     int temp;
     temp = vec[lo];
     vec[lo] = vec[hi];
     vec[hi] = temp;

     // Next recursive call increase lo, and decrease hi by 1
     revVector(vec, lo+1, hi-1);

     return;
}</pre>
</blockquote>
<h2 id="section-9">Going further</h2>
<ol>
<li>Can you make a helper function for this recursive function? [Hint: <strong><a href="http://talkbinary.com/programming/c/c-helper-functions/">C++ Helper Functions</a>]</strong></li>
<li>Can you write a function that does the swap which would be called within your recursive/iterative version?</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/c-recursion-reverse-elements-of-a-vector/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++ Recursion &#8211; Fibonacci</title>
		<link>http://talkbinary.com/programming/c/c-recursion-fibonacci/</link>
		<comments>http://talkbinary.com/programming/c/c-recursion-fibonacci/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 06:41:24 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[c++ fibonacci function]]></category>
		<category><![CDATA[c++ recursion]]></category>
		<category><![CDATA[fibonacci function]]></category>
		<category><![CDATA[fibonacci recursion]]></category>
		<category><![CDATA[fibonacci recursive]]></category>
		<category><![CDATA[fibonacci sequence]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[iteration]]></category>
		<category><![CDATA[iterative]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[recursive]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=2249</guid>
		<description><![CDATA[The Fibonacci Sequence Below is an example of how to create the recursive version of the Fibonacci sequence. But first of all, what is it? * This tutorial assumes you understand the following: Introduction to Recursion in C++ Fibonacci is a well known number sequence that models the growth of a rabbit population amongst other<a class="moretag" href="http://talkbinary.com/programming/c/c-recursion-fibonacci/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<h3 id="section-1">The Fibonacci Sequence</h3>
<hr />
Below is an example of how to create the recursive version of the Fibonacci sequence. But first of all, what is it?<br />
<br/><br />
* This tutorial assumes you understand the following:</p>
<ul>
<li><a href="http://talkbinary.com/programming/c/introduction-to-recursion-in-c/">Introduction to Recursion in C++</a></li>
</ul>
<blockquote><p>Fibonacci is a well known number sequence that models the growth of a rabbit population amongst other things found in nature. </p></blockquote>
<p>Below is the Fibonacci numbers computed up to the 11th term.</p>
<table border="1" bordercolor="black">
<tr border-color="white">
<td border-color="white">0</t></p>
<td border-color="white">1</td>
<td border-color="white">2</td>
<td border-color="white">3</td>
<td bordercolor="white">4</td>
<td bordercolor="white">5</td>
<td bordercolor="white">6</td>
<td bordercolor="white">7</td>
<td bordercolor="white">8</td>
<td bordercolor="white">9</td>
<td bordercolor="white">10</td>
<td bordercolor="white">11</td>
</tr>
<tr bordercolor="white">
<td>0</t></p>
<td>1</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>5</td>
<td>8</td>
<td>13</td>
<td>21</td>
<td>34</td>
<td>55</td>
<td>89</td>
</tr>
</table>
<h3 id="section-2">Iterative version of Fibonacci</h3>
<hr />
Before we create the recursive version of Fibonacci, let&#8217;s look at how to create the iterative version by looking at the formula.</p>
<blockquote><h3 id="section-3">Fibonacci Formula</h3>
<p>Fibonacci(0) = 0;<br />
Fibonacci(1) = 1;<br />
Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2);</p></blockquote>
<p>So how would we do an iterative version? I&#8217;ll walk you through the 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
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// The value of n is the Fibonacci number we want to compute //</span>
<span style="color: #0000ff;">int</span> fibonacci<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> 
&nbsp;
    <span style="color: #666666;">//Our two initial values</span>
    <span style="color: #0000ff;">int</span> fib1 <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">int</span> fib2 <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> 
&nbsp;
&nbsp;
    <span style="color: #666666;">//Our running value</span>
    <span style="color: #0000ff;">int</span> fibn <span style="color: #000080;">=</span> fib1 <span style="color: #000040;">+</span> fib2<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// Computing the nth value of Fibonacci //</span>
    <span style="color: #666666;">// Explained below //</span>
&nbsp;
   <span style="color: #0000ff;">return</span> fib<span style="color: #008080;">;</span></pre></td></tr></table></div>

<p><br/><br />
So how would we compute the value of the nth value of the Fibonacci sequence? We can do it with a for loop, to add UP to the value we are looking for. In other words, if we were doing it by hand, we&#8217;d do it as following:<br />
<span id="more-2249"></span></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
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">//Initial cases</span>
fib<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
fib<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
fib<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> fib<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> fib<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">0</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
fib<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> fib<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> fib<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span>
&nbsp;
fib<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> fib<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> fib<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">3</span><span style="color: #008080;">;</span>
&nbsp;
fib<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">5</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> fib<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> fib<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">3</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">2</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">5</span><span style="color: #008080;">;</span> 
&nbsp;
fib<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">6</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> fib<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">5</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> fib<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">5</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">3</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">8</span><span style="color: #008080;">;</span>
&nbsp;
..
&nbsp;
<span style="color: #007788;">fib</span><span style="color: #008000;">&#40;</span>n<span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> fib<span style="color: #008000;">&#40;</span>n <span style="color: #000040;">-</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> fib<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></pre></td></tr></table></div>

<p><br/><br />
You could think of fib(n), fib(n-1), and fib(n-2) being fib, fib1, and fib2 respectively in our code. Try coming up with a solution to compute the nth Fibonacci number. </p>
<p>The code to compute the Fibonacci sequence for iterative version is below.</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
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// The value of n is the Fibonacci number we want to compute //</span>
<span style="color: #0000ff;">int</span> fibonacci<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> 
&nbsp;
    <span style="color: #666666;">//Our two initial values</span>
    <span style="color: #0000ff;">int</span> fib1 <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">int</span> fib2 <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> 
&nbsp;
    <span style="color: #666666;">//Our running value</span>
    <span style="color: #0000ff;">int</span> fib <span style="color: #000080;">=</span> fib1<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// Computing the nth value of Fibonacci //</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;">1</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> n<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span> 
    <span style="color: #008000;">&#123;</span> 
        <span style="color: #666666;">// Fib(n) = fib(n-1) + fib(n-2);</span>
        fib <span style="color: #000080;">=</span> fib1 <span style="color: #000040;">+</span> fib2<span style="color: #008080;">;</span>
&nbsp;
        <span style="color: #666666;">// The next two numbers are set this way </span>
        <span style="color: #666666;">// to be used in the next computation of fib(n) </span>
        <span style="color: #666666;">// Work it out by hand if you don't understand it.</span>
&nbsp;
        <span style="color: #666666;">// Fib(n-1) = fib(n-2)</span>
        <span style="color: #666666;">// Fib(n-2) = fib</span>
        fib1 <span style="color: #000080;">=</span> fib2<span style="color: #008080;">;</span>
&nbsp;
        fib2 <span style="color: #000080;">=</span> fib<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
   <span style="color: #666666;">// Return result</span>
   <span style="color: #0000ff;">return</span> fib<span style="color: #008080;">;</span></pre></td></tr></table></div>

<p><br/></p>
<h3 id="section-4">Recursive version Fibonacci</h3>
<hr />
To create the recursive version of Fibonacci, we start like always with the base cases. In case you forgot, the formula for Fibonacci is below.</p>
<blockquote><h3 id="section-3">Fibonacci Formula</h3>
<p>Fibonacci(0) = 0;<br />
Fibonacci(1) = 1;<br />
Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2);</p></blockquote>
<p>So let&#8217;s start by creating the base case and function header.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> fibonacci<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>
&nbsp;
    <span style="color: #666666;">// Base cases</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> n <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> n <span style="color: #000080;">==</span> <span style="color: #0000dd;">1</span> <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>That was quite simple wasn&#8217;t it? An image describing the function calls are below.</p>
<p><center><a href="http://talkbinary.com/wp-content/uploads/2010/02/fib01.jpg"><img src="http://talkbinary.com/wp-content/uploads/2010/02/fib01.jpg" alt="" title="fib01" width="259" height="135" class="aligncenter size-full wp-image-2258" /></a></center></p>
<p>So how do we go on by computing the nth term of the Fibonacci sequence? Try it out on your own first then come back. If you translated the formula correctly into code, it should then look like the following.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> fibonacci<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>
&nbsp;
    <span style="color: #666666;">// Base cases</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> n <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> n <span style="color: #000080;">==</span> <span style="color: #0000dd;">1</span> <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">//This is returning fibonacci(n) = fibonacci(n-1) + fibonacci(n-2)</span>
    <span style="color: #0000ff;">else</span> 
         <span style="color: #0000ff;">return</span> fibonacci<span style="color: #008000;">&#40;</span>n<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> fibonacci<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>
&nbsp;
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><br/></p>
<h3 id="section-6">Explanation of Fibonacci using Recursion</h3>
<hr />
Yes, that is surprisingly it. Now you see why recursion can make our life a bit easier sometimes? If you don&#8217;t understand how it works, look at the following set of images. Try drawing them out by hand to understand how they work.<br />
<br/></p>
<p><center><br />
<a href="http://talkbinary.com/wp-content/uploads/2010/02/fib02.jpg"><img src="http://talkbinary.com/wp-content/uploads/2010/02/fib02.jpg" alt="" title="fib02" width="290" height="173" class="aligncenter size-full wp-image-2259" /></a></p>
<p><a href="http://talkbinary.com/wp-content/uploads/2010/02/fib03.jpg"><img src="http://talkbinary.com/wp-content/uploads/2010/02/fib03-300x219.jpg" alt="" title="fib03" width="300" height="219" class="aligncenter size-medium wp-image-2260" /></a></center></p>
<p>The following image shows you how the recursive calls would be made with larger n values.</p>
<p><center><a href="http://talkbinary.com/wp-content/uploads/2010/02/fib04.jpg"><img src="http://talkbinary.com/wp-content/uploads/2010/02/fib04-300x119.jpg" alt="" title="fib04" width="300" height="119" class="aligncenter size-medium wp-image-2261" /></a></center></p>
<p><br/></p>
<h3 id="section-7">Overview of Fibonacci Sequence using Recursion</h3>
<hr />
If you still don&#8217;t understand how it works, my best encouragement is to draw the function calls by hand. Try doing something similar to my drawings. Recursion is a tough topic so it takes some time to learn. Even I struggled with the subject. </p>
<p>If you have any questions or suggestions, feel free to post them below.</p>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/c-recursion-fibonacci/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>C++ Recursion &#8211; Factorial</title>
		<link>http://talkbinary.com/programming/c/c-recursion-factorial/</link>
		<comments>http://talkbinary.com/programming/c/c-recursion-factorial/#comments</comments>
		<pubDate>Tue, 26 Jan 2010 07:06:33 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[factorial]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[recursive]]></category>
		<category><![CDATA[summation]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=2025</guid>
		<description><![CDATA[Creating a factorial function Below is an example of how to calculate the factorial of n. This tutorial expects you to understand the following: C++ Recursion C++ Recursion &#8211; Summation Example: 1 factorial&#40;5&#41;; Result: 1 120 Iterative version of a function that calculates the factorial To understand how to calculate the factorial using recursion, let&#8217;s<a class="moretag" href="http://talkbinary.com/programming/c/c-recursion-factorial/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<h3 id="section-1">Creating a factorial function</h3>
<hr />
Below is an example of how to calculate the factorial of n. This tutorial expects you to understand the following: </p>
<ol>
<li><a href="http://talkbinary.com/programming/c/introduction-to-recursion-in-c/">C++ Recursion</a></li>
<li><a href="http://talkbinary.com/programming/c/c-recursion-summation/">C++ Recursion &#8211; Summation</a></li>
</ol>
<p>Example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">factorial<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">5</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>Result:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000dd;">120</span></pre></td></tr></table></div>

<h3 id="section-2">Iterative version of a function that calculates the factorial</h3>
<hr />
To understand how to calculate the factorial using recursion, let&#8217;s write a simple algorithm that will do it for us using iteration.<br />
<br/></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> factorial<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>
&nbsp;
     <span style="color: #666666;">//Initial value for the final value is 1</span>
     <span style="color: #0000ff;">int</span> factorial <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</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;">2</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;=</span> n<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> 
          factorial <span style="color: #000080;">=</span> factorial <span style="color: #000040;">*</span> n<span style="color: #008080;">;</span>
     <span style="color: #008000;">&#125;</span>
&nbsp;
     <span style="color: #0000ff;">return</span> factorial<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><span id="more-2025"></span></p>
<h3 id="section-3">Calculating factorial using recursion</h3>
<hr />
So how would we go by solving this using recursion? First of all we need a function header.</p>
<p><br/></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> factorial<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span></pre></td></tr></table></div>

<p><br/></p>
<p>Next, what would be our base case? When we are trying to calculate the factorial for 0, the result should be 1. </p>
<p><br/></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> factorial<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>
&nbsp;
     <span style="color: #666666;">//Return 1 when n is 0</span>
     <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> n <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><br/></p>
<p>Now, how would we go on by computing the factorial using recursion? Let&#8217;s come up with a formula by calculating the factorial of 2,3, and 4.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">factorial<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
factorial<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span> <span style="color: #000040;">*</span> factorial <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span> <span style="color: #000040;">*</span> <span style="color: #0000dd;">1</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span>
&nbsp;
factorial<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">3</span> <span style="color: #000040;">*</span> factorial <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">3</span> <span style="color: #000040;">*</span> <span style="color: #0000dd;">2</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">6</span><span style="color: #008080;">;</span>
&nbsp;
factorial<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">4</span> <span style="color: #000040;">*</span> factorial <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">4</span> <span style="color: #000040;">*</span> <span style="color: #0000dd;">6</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">24</span><span style="color: #008080;">;</span> 
&nbsp;
...
&nbsp;
<span style="color: #007788;">factorial</span><span style="color: #008000;">&#40;</span>n<span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> n <span style="color: #000040;">*</span> factorial<span style="color: #008000;">&#40;</span>n<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p><br/><br />
Translating the above formula to code, is then fairly trivial. This would allow us to apply our factorial formula to any positive integer.<br />
<br/></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;">int</span> factorial<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>
&nbsp;
     <span style="color: #666666;">//Return 1 when n is 0</span>
     <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> n <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
     <span style="color: #666666;">//factorial(n) = n * factorial(n-1);</span>
     <span style="color: #0000ff;">return</span> n <span style="color: #000040;">*</span> factorial<span style="color: #008000;">&#40;</span>n<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></pre></td></tr></table></div>

<p><br/></p>
<p>Have any questions? Feel free to ask below or request any other examples.</p>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/c-recursion-factorial/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++ Recursion &#8211; Summation</title>
		<link>http://talkbinary.com/programming/c/c-recursion-summation/</link>
		<comments>http://talkbinary.com/programming/c/c-recursion-summation/#comments</comments>
		<pubDate>Sat, 14 Nov 2009 04:51:55 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[recursive]]></category>
		<category><![CDATA[sum]]></category>
		<category><![CDATA[summation]]></category>
		<category><![CDATA[total]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=2040</guid>
		<description><![CDATA[Creating a sum function Below is an example of how to calculate the total of a sequence of numbers. In this case we will be calculating the sum of 1 + 2 + .. + 9 + 10. Example: 1 sum&#40;10&#41;; Result: 1 1 + 2 + 3 + 4 + 5 + 6 +<a class="moretag" href="http://talkbinary.com/programming/c/c-recursion-summation/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<h3 id="section-1">Creating a sum function</h3>
<hr />
Below is an example of how to calculate the total of a sequence of numbers. In this case we will be calculating the sum of 1 + 2 + .. + 9 + 10.</p>
<p>Example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">sum<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">10</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>Result:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000dd;">1</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">2</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">3</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">4</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">5</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">6</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">7</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">8</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">9</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">10</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">55</span></pre></td></tr></table></div>

<h3 id="section-2">Iterative version of a function that calculates the summation</h3>
<hr />
To understand how to calculate the sum using recursion, let&#8217;s write a simple algorithm that will do it for us using iteration.<br />
<br/></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">//Our initial total is zero</span>
<span style="color: #0000ff;">int</span> total <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">//We want the sum from 1 + 2 + ... + 9 + 10</span>
<span style="color: #0000ff;">int</span> n <span style="color: #000080;">=</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">//The following for loop will calculate the summation from 1 - n</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;">1</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;=</span> n<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> 
     total <span style="color: #000080;">=</span> total <span style="color: #000040;">+</span> i<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><span id="more-2040"></span></p>
<h3 id="section-3">Calculating the summation using recursion</h3>
<hr />
So how would we go by solving this using recursion? First of all we need a function header.<br />
<br/></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> sum<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span></pre></td></tr></table></div>

<p><br/></p>
<p>Next, if you remember from the <a href="http://talkbinary.com/programming/c/introduction-to-recursion-in-c/">basics of recursion</a>, our function needs a base case or <i>terminating condition</i>. What would it be in this case? When we are trying to sum the sequence consisting of only 0. We also don&#8217;t want to calculate the summation of negative numbers.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> sum<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> 
&nbsp;
     <span style="color: #666666;">//Return 0 when n is 0</span>
     <span style="color: #0000ff;">if</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: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><br/></p>
<p>Now, how would we go on by computing summation using recursion? Let&#8217;s come up with a formula by calculating the summation of 1, 2, and 3.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">summation<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
summation<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span> <span style="color: #000040;">+</span> summation<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">0</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
summation<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span> <span style="color: #000040;">+</span> summation<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">3</span><span style="color: #008080;">;</span>
&nbsp;
summation<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">3</span> <span style="color: #000040;">+</span> summation<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">3</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">3</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">6</span><span style="color: #008080;">;</span> 
&nbsp;
...
&nbsp;
<span style="color: #007788;">summation</span><span style="color: #008000;">&#40;</span>n<span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> n <span style="color: #000040;">*</span> summation<span style="color: #008000;">&#40;</span>n<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>Translating the following formula into code, is then fairly trivial. This would allow us to apply our summation formula to any positive integer.</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;">int</span> sum<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> 
&nbsp;
     <span style="color: #666666;">//Return 0 when n is 0</span>
     <span style="color: #0000ff;">if</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: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
     <span style="color: #0000ff;">else</span> 
          <span style="color: #0000ff;">return</span> n <span style="color: #000040;">+</span> sum<span style="color: #008000;">&#40;</span>n<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: #008000;">&#125;</span></pre></td></tr></table></div>

<p><br/><br />
The previous recursive function will work as follows (in our case, sum(3)).</p>
<p><center><a href="http://talkbinary.com/wp-content/uploads/2009/11/summation21.jpg"><img src="http://talkbinary.com/wp-content/uploads/2009/11/summation21-300x123.jpg" alt="summation2" title="summation2" width="300" height="123" class="aligncenter size-medium wp-image-2055" /></a>]</center></p>
<p>Have any questions? Feel free to ask below. </p>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/c-recursion-summation/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Introduction to Recursion in C++</title>
		<link>http://talkbinary.com/programming/c/introduction-to-recursion-in-c/</link>
		<comments>http://talkbinary.com/programming/c/introduction-to-recursion-in-c/#comments</comments>
		<pubDate>Sat, 21 Feb 2009 23:18:23 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[activation records]]></category>
		<category><![CDATA[base case]]></category>
		<category><![CDATA[c++ recursion]]></category>
		<category><![CDATA[display function]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[recursive]]></category>
		<category><![CDATA[stack]]></category>
		<category><![CDATA[terminating condition]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=1572</guid>
		<description><![CDATA[Recursion in C++ is simply a function that calls itself that terminates when a base case is met. ]]></description>
			<content:encoded><![CDATA[<p>Recursion in C++ is simply a function that calls itself that terminates when a base case is met. It&#8217;s very useful to learn how recursion for a couple of reasons.</p>
<ul>
<li>It&#8217;s easier to solve certain problems with recursion as the resulting code is usually shorter</li>
<li>Sometimes its the only way</li>
</ul>
<h3 id="section-1">Displaying numbers recursively</h3>
<p>So what&#8217;s an example of a recursive function? Let&#8217;s make one. Let&#8217;s start by creating a function that displays a number (it isn&#8217;t the best example, but it&#8217;s good enough to introduce you to recursion). </p>
<h4 id="section-2">Display Function</h4>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> display<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: #0000dd;">cout</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: #008080;">;</span>
     <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><br/>All this function does is simply print the number passed in. </p>
<h4 id="section-3">Recursive Display Function</h4>
<p>Now let&#8217;s have this function call itself but lets decrement the value being displayed each time.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> display<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: #0000dd;">cout</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: #008080;">;</span>
     display<span style="color: #008000;">&#40;</span>n<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: #666666;">//Will call display but with (n-1)</span>
     <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>So let&#8217;s say we executed display(10)</p>
<p>What would this display? It would display 10 9 8 7 6 5 4&#8230;and would never stop. Why? We never told the function when to terminate. This will either cause your program to run until you kill it, or may cause it to segmentation fault. In order to fix this, this calls for the addition of <strong>base case</strong>. <span id="more-1572"></span></p>
<blockquote><h3 id="section-4">Base Cases in Recursion</h3>
<p>In order for your recursive function to terminate you <strong>always</strong> need a base case. You may have multiple base cases. </p></blockquote>
<h4 id="section-5">Recursive Display Function with a Base Case</h4>
<p>Let&#8217;s make the function terminate when n is 0. Or better yet, let&#8217;s make it terminate when n is less than or equal to 0. (Why do you think this is better?)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> display<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> n <span style="color: #000080;">&lt;=</span> <span style="color: #0000dd;">0</span> <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">return</span><span style="color: #008080;">;</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; &quot;</span><span style="color: #008080;">;</span>
     display<span style="color: #008000;">&#40;</span>n<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: #666666;">//Will call display but with (n-1)</span>
     <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>So now, if we execute display(10), what would it display? </p>
<pre lang="c++">10 9 8 7 6 5 4 3 2 1 </pre>
<h3 id="section-6">How Recursion works in C++</h3>
<p>So how does recursion work in C++? Each <strong>function</strong> creates an activation record on the <strong>stack</strong>. As a function gets called, it gets added to the top of the stack. Until the function is terminated, is then taken off the stack. A visual example for our display function is below.</p>
<p><center><div id="attachment_1594" class="wp-caption alignnone" style="width: 245px"><a href="http://talkbinary.com/wp-content/uploads/2009/02/recursion.jpg"><img src="http://talkbinary.com/wp-content/uploads/2009/02/recursion-235x300.jpg" alt="Click to Enlarge" title="recursion" width="235" height="300" class="size-medium wp-image-1594" /></a><p class="wp-caption-text">(Click to Enlarge) </p></div></center></p>
<p>The first function called was display(10) which is the activation record seen at the bottom of our stack. If you notice, the following function keep getting called as long as the base case isn&#8217;t met. Up until n = -1, then the activation record of the functions are popped off.  </p>
<h3 id="section-7">Comments on Recursion</h3>
<p>What is shown previously is a very basic recursive algorithm. More recursion techniques will be discussed later. So even if you don&#8217;t understand the purpose of recursion just yet, don&#8217;t hesitate. You&#8217;ll understand why it&#8217;s important when you understand recursion is the only way to do certain problems. </p>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/introduction-to-recursion-in-c/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Fibonacci in C++</title>
		<link>http://talkbinary.com/programming/c/fibonacci-in-c/</link>
		<comments>http://talkbinary.com/programming/c/fibonacci-in-c/#comments</comments>
		<pubDate>Fri, 19 Dec 2008 05:23:37 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[dynamic programming]]></category>
		<category><![CDATA[fib]]></category>
		<category><![CDATA[fibonacci]]></category>
		<category><![CDATA[fibonacci sequence]]></category>
		<category><![CDATA[iterative]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[recursive]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=1351</guid>
		<description><![CDATA[Fibonacci is a well known number sequence that models the growth of a rabbit population amongst other things found in nature. The Fibonacci Numbers Sequence Below is the Fibonacci numbers computed up to the 11th term. 0 1 2 3 4 5 6 7 8 9 10 11 0 1 1 2 3 5 8<a class="moretag" href="http://talkbinary.com/programming/c/fibonacci-in-c/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<p>Fibonacci is a well known number sequence that models the growth of a rabbit population amongst other things found in nature. </p>
<h3 id="section-1">The Fibonacci Numbers Sequence</h3>
<p>Below is the Fibonacci numbers computed up to the 11th term.</p>
<table border="1" bordercolor="black">
<tr border-color="white">
<td border-color="white">0</t></p>
<td border-color="white">1</td>
<td border-color="white">2</td>
<td border-color="white">3</td>
<td bordercolor="white">4</td>
<td bordercolor="white">5</td>
<td bordercolor="white">6</td>
<td bordercolor="white">7</td>
<td bordercolor="white">8</td>
<td bordercolor="white">9</td>
<td bordercolor="white">10</td>
<td bordercolor="white">11</td>
</tr>
<tr bordercolor="white">
<td>0</t></p>
<td>1</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>5</td>
<td>8</td>
<td>13</td>
<td>21</td>
<td>34</td>
<td>55</td>
<td>89</td>
</tr>
</table>
<h3 id="section-2">The formula for Fibonacci</h3>
<p>Computing for Fibonacci is simple. The formula is below.</p>
<blockquote><p>Fibonacci (n) = Fibonacci(n-1) + Fibonacci(n-2);</p></blockquote>
<p>To computer for Fibonacci of 7, simply add the values of Fibonacci of 6 and Fibonacci of 5. In other words, the sum of the previous two values.</p>
<blockquote><p> Fibonacci(7) = Fibonacci(6) + Fibonacci(5);<br />
Fibonacci(7) = 8 + 5;<br />
Fibonacci(7) = 13;</p></blockquote>
<h3 id="section-3">Iterative version of Fibonacci</h3>
<p>This is an iterative version of Fibonacci.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> fib<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> n <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #000040;">||</span> n <span style="color: #000080;">==</span> <span style="color: #0000dd;">1</span> <span style="color: #008000;">&#41;</span> 
        <span style="color: #0000ff;">return</span> n<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">int</span> fib1 <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> 
    <span style="color: #0000ff;">int</span> fib2 <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">int</span> fib <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;">2</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> n<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span> 
    <span style="color: #008000;">&#123;</span>
        fib <span style="color: #000080;">=</span> fib1 <span style="color: #000040;">+</span> fib2<span style="color: #008080;">;</span>
        fib1 <span style="color: #000080;">=</span> fib2<span style="color: #008080;">;</span>
        fib2 <span style="color: #000080;">=</span> fib<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">return</span> fib<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><span id="more-1351"></span></p>
<h3 id="section-4">Recursive version of Fibononacci</h3>
<p>The recursive version of Fibonacci in C++ is below. This recursive function computes the sum of the previous two fibonacci numbers recursively.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> fib<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> n <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
     <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> n <span style="color: #000080;">==</span> <span style="color: #0000dd;">1</span> <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
     <span style="color: #0000ff;">return</span> fib<span style="color: #008000;">&#40;</span>n<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> fib<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>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>An image of how this recursive solution works is below. Each bubble is a function call. </p>
<p><center><img src="http://talkbinary.com/wp-content/uploads/2008/12/fib.jpg" alt="fib" title="fib" width="400" height="276" class="alignnone size-full wp-image-1360" /></center></p>
<h3 id="section-5">Dynamic Programming version of Fibonacci</h3>
<p>The dynamic programming version of Fibonacci in C++ is below. This snippet of code computes the fibonacci by traversing a vector.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">&nbsp;
vector <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span> fib<span style="color: #008080;">;</span>
fib.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
fib.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> n<span style="color: #008080;">;</span> <span style="color: #666666;">//N is the number of fibonacci numbers we are going to compute. </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;">2</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> n<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span> 
<span style="color: #008000;">&#123;</span>
      fib.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span> fib<span style="color: #008000;">&#91;</span>i<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000040;">+</span> fib<span style="color: #008000;">&#91;</span>i<span style="color: #000040;">-</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<h3 id="section-6">Finishing remarks</h3>
<p>So, which one do you think is the fastest version? </p>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/fibonacci-in-c/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced

Served from: talkbinary.com @ 2012-02-11 19:29:26 -->
