<?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; reverse</title>
	<atom:link href="http://talkbinary.com/tag/reverse/feed/" rel="self" type="application/rss+xml" />
	<link>http://talkbinary.com</link>
	<description>Programming Resources, Technology, Computers</description>
	<lastBuildDate>Wed, 15 Feb 2012 16:01:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</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; Printing a Sequence of Numbers in Reverse</title>
		<link>http://talkbinary.com/programming/c/c-recursion-printing-a-sequence-of-numbers-in-reverse/</link>
		<comments>http://talkbinary.com/programming/c/c-recursion-printing-a-sequence-of-numbers-in-reverse/#comments</comments>
		<pubDate>Sat, 07 Nov 2009 00:46:15 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[printing]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[reverse]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=2003</guid>
		<description><![CDATA[Printing a sequence of numbers in reverse Below is an example of how to print a sequence of numbers in reverse. Given n = 10 the following would be the result of printing a sequence of numbers in decreasing order as long as n > 0. 1 10 9 8 7 6 5 4 3<a class="moretag" href="http://talkbinary.com/programming/c/c-recursion-printing-a-sequence-of-numbers-in-reverse/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<h3 id="section-1">Printing a sequence of numbers in reverse</h3>
<hr />
Below is an example of how to print a sequence of numbers in reverse.</p>
<p>Given <em>n = 10</em> the following would be the result of printing a sequence of numbers in decreasing order as long as <em>n > 0</em>.</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;">10</span> <span style="color: #0000dd;">9</span> <span style="color: #0000dd;">8</span> <span style="color: #0000dd;">7</span> <span style="color: #0000dd;">6</span> <span style="color: #0000dd;">5</span> <span style="color: #0000dd;">4</span> <span style="color: #0000dd;">3</span> <span style="color: #0000dd;">2</span> <span style="color: #0000dd;">1</span> <span style="color: #0000dd;">0</span></pre></td></tr></table></div>

<p>The reverse order of this sequence would be the following:</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;">0</span> <span style="color: #0000dd;">1</span> <span style="color: #0000dd;">2</span> <span style="color: #0000dd;">3</span> <span style="color: #0000dd;">4</span> <span style="color: #0000dd;">5</span> <span style="color: #0000dd;">6</span> <span style="color: #0000dd;">7</span> <span style="color: #0000dd;">8</span> <span style="color: #0000dd;">9</span> <span style="color: #0000dd;">10</span></pre></td></tr></table></div>

<p>Using recursion, we will demonstrate how to reverse the order of a sequence by simply manipulating a few lines of code.</p>
<h3 id="section-2">Printing numbers using recursion</h3>
<hr />
To understand how to print a sequence of numbers in reverse, we first demonstrate a recursive function that displays a sequence of numbers in decreasing order using recursion.<br />
<br/></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> print<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: #666666;">//Terminating condition</span>
&nbsp;
     <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: #666666;">//Prints number n</span>
&nbsp;
     print<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;">//Calls itself with (n-1)</span>
&nbsp;
     <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span> <span style="color: #666666;">//Returns from the function</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The following code, simply prints out <em>n</em> while then calling the same print function on <em>n &#8211; 1</em> and stops when <em>n < 0</em>.  The code above is explained in more detail in the following tutorial: <a href="http://talkbinary.com/programming/c/introduction-to-recursion-in-c/">Introduction to Recursion in C++</a>.</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;">print<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>Produces:</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;">3</span> <span style="color: #0000dd;">2</span> <span style="color: #0000dd;">1</span> <span style="color: #0000dd;">0</span></pre></td></tr></table></div>

<p>As mentioned, the recursive function first prints out <em>n</em>, then calls itself on <em>n &#8211; 1</em> as long as <em>n > 0</em>. </p>
<h3 id="section-3">Printing Numbers in Reverse</h3>
<hr />
<p>Now, how would you print out the previous sequence of numbers but in reverse order while still using recursion?</p>
<p>Think about it. The difference from the previous solution and this question is the order in which the numbers are printed. This only requires a simple modification from the previous example.<br />
<span id="more-2003"></span><br />
<strong>Hint: Try moving your printing statement around your function.</strong> Go ahead and try it before you look at the solution.</p>
<p>By placing the printing statement after the function call, you delay printing until <em>n > 0</em> isn&#8217;t true which would be when <em>n = -1</em>. Therefore, this call would terminate, and return to the function call when <em>n = 0</em>, and print <em>0</em>. This function call would terminate, and return to the function call when <em>n = 1</em>, and print <em>1</em>, and so on.<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;">void</span> printrev<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: #666666;">//Terminating condition</span>
&nbsp;
    <span style="color: #666666;">//Previous location of our print statement</span>
&nbsp;
    printrev<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;">//Calls itself with (n-1)</span>
&nbsp;
    <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: #666666;">//Prints number n</span>
&nbsp;
    <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<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;">printrev<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>Produces:</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;">0</span> <span style="color: #0000dd;">1</span> <span style="color: #0000dd;">2</span> <span style="color: #0000dd;">3</span></pre></td></tr></table></div>

<p>A diagram further explaining the following modification is below.</p>
<p><center><a href="http://talkbinary.com/wp-content/uploads/2009/11/recursion.jpg"><img src="http://talkbinary.com/wp-content/uploads/2009/11/recursion-265x300.jpg" alt="recursion" title="recursion" width="265" height="300" class="aligncenter size-medium wp-image-2014" /></a><small>Click to enlarge</small></center></p>
<p><br/>This not only works by printing a sequence of numbers in reverse, it can also be applied to data structures such as vectors, string, linked lists, and others when using recursive functions.</p>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/c-recursion-printing-a-sequence-of-numbers-in-reverse/feed/</wfw:commentRss>
		<slash:comments>3</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-05-24 09:38:27 -->
