<?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; base case</title>
	<atom:link href="http://talkbinary.com/tag/base-case/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; Printing Elements of a Vector</title>
		<link>http://talkbinary.com/programming/c/c-recursion-printing-elements-of-a-vector/</link>
		<comments>http://talkbinary.com/programming/c/c-recursion-printing-elements-of-a-vector/#comments</comments>
		<pubDate>Sun, 21 Feb 2010 08:05:33 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[base case]]></category>
		<category><![CDATA[element]]></category>
		<category><![CDATA[helper function]]></category>
		<category><![CDATA[printing function]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[recursive functions]]></category>
		<category><![CDATA[vector]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=2169</guid>
		<description><![CDATA[Printing elements of a Vector using Recursion Below is an example of how to print the elements of a vector using recursion. Later, we&#8217;ll print the elements in reverse. Why is this useful? You can apply this principle later to more complex data structures such as Binary Search Trees, or Linked Lists. This tutorial assumes<a class="moretag" href="http://talkbinary.com/programming/c/c-recursion-printing-elements-of-a-vector/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<h3 id="section-1">Printing elements of a Vector using Recursion</h3>
<hr />
Below is an example of how to print the elements of a vector using recursion. Later, we&#8217;ll print the elements in reverse. Why is this useful? You can apply this principle later to more complex data structures such as Binary Search Trees, or Linked Lists.<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</a></li>
<li><a href="http://talkbinary.com/programming/c/c-recursion-printing-a-sequence-of-numbers-in-reverse/2/">C++ Recursion &#8211; Printing a Sequence of Numbers in Reverse</a> </li>
</ul>
<h3 id="section-2">Iterative version for printing elements of a vector</h3>
<hr />
To understand how to print elements of a vector using recursion, let&#8217;s write a simple algorithm that will do it for us using iteration.<br />
<br/><br/></p>
<pre lang="c++">
void printVector(vector <int> vec) { 

     for ( int i = 0; i < vec.size(); i++ ) { 

          //Printing ith element  followed by a space
          cout << vec[i] << " ";

     }

     //Printing end line
     cout << endl;
}</pre>
<p><br/><span id="more-2169"></span></p>
<h3 id="section-3">Recursive function for printing elements of a vector</h3>
<hr />
So, how do we go from iteration to recursion? Well if you remember how to <a href="http://talkbinary.com/programming/c/introduction-to-recursion-in-c/">Print a Sequence of Numbers using Recursion</a> without a vector, you know at every recursive call we print an number larger than the previous. In this case we want to print out an element in the vector in increasing order. So what would be our function header?<br />
<br/></p>
<pre lang="c++">void printVector(vector <int> vec) { </pre>
<p><br/><br />
Would this work? Well if you think about it, which element would you print? Using iteration, we used the <b><i>i</b></i> variable to print the <b><i>ith</b></i> element in the vector. Using recursion, we would simply pass this variable, so our function header would be the following.<br />
<br/></p>
<pre lang="c++">void printVector(vector <int> vec, int i) { </pre>
<p><br/><br />
Now, using the knowledge of <a href="http://talkbinary.com/programming/c/introduction-to-recursion-in-c/">Printing a Sequence of Numbers using Recursion</a> we could apply this to printing a sequence of elements in a vector.</p>
<pre lang="c++">void printVector(vector<int> vec, int i) { 

     //Returning when i is larger or equal to the size
    //of the vector
     if ( i >= vec.size() ) {
          return;
     }

     //Printing the ith element of the vector
     cout << vec[i] << " ";

     //Calling the function with a larger i
     printVector(vec, i+1);
}</pre>
<p><br/><br />
Using this function would be done using the following.<br />
<br/></p>
<pre lang="c++">vector <int > vec; 

//Populate vector...

//Calling the printVector function
printVector(vec, 0);</pre>
<p><br/></p>
<p>Have any questions? Feel free to ask below or request any other examples.</p>
<h3 id="section-4">Going further</h3>
<hr />
<ul>
<li>1. How would you make this function's base case take care of another case where your program might crash? (Hint: What is your function was called with a different i value?)</li>
<li>2. How would you make this function be called by simply passing in the vector and not the element to be printed first (in our example, 0). (Hint: Use <a href="http://talkbinary.com/programming/c/c-helper-functions/">Helper functions</a>)</li>
<li>3. How would you print the elements of the vector in reverse? (Hint: <a href="http://talkbinary.com/programming/c/c-recursion-printing-a-sequence-of-numbers-in-reverse/">Printing a sequence of numbers in reverse</a>)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/c-recursion-printing-elements-of-a-vector/feed/</wfw:commentRss>
		<slash:comments>0</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>
	</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:26:45 -->
