<?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>Fri, 30 Jul 2010 18:22:57 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.0.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[Functions]]></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 [...]]]></description> <content:encoded><![CDATA[<h2>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>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>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>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>Reversing the elements of a vector using recursion</h2><p>So how would we start? What would the function header be?</p><blockquote><h3>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>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>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>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><h3  class="related_post_title">Related Posts</h3><ul
class="related_post"><li><a
href="http://talkbinary.com/programming/c/c-recursion-fibonacci/" title="C++ Recursion &#8211; Fibonacci">C++ Recursion &#8211; Fibonacci</a> (0)</li><li><a
href="http://talkbinary.com/programming/c/c-recursion-power-continued/" title="C++ Recursion &#8211; Power Continued">C++ Recursion &#8211; Power Continued</a> (0)</li><li><a
href="http://talkbinary.com/programming/c/c-recursion-power/" title="C++ Recursion &#8211; Power">C++ Recursion &#8211; Power</a> (1)</li><li><a
href="http://talkbinary.com/programming/c/c-recursion-palindrome/" title="C++ Recursion &#8211; Palindrome">C++ Recursion &#8211; Palindrome</a> (0)</li><li><a
href="http://talkbinary.com/programming/c/c-recursion-factorial/" title="C++ Recursion &#8211; Factorial">C++ Recursion &#8211; Factorial</a> (0)</li><li><a
href="http://talkbinary.com/programming/c/c-recursion-summation/" title="C++ Recursion &#8211; Summation ">C++ Recursion &#8211; Summation </a> (2)</li><li><a
href="http://talkbinary.com/programming/c/introduction-to-recursion-in-c/" title="Introduction to Recursion in C++">Introduction to Recursion in C++</a> (6)</li><li><a
href="http://talkbinary.com/programming/c/special-functions/fibonacci-in-c/" title="Fibonacci in C++">Fibonacci in C++</a> (3)</li><li><a
href="http://talkbinary.com/programming/c/c-recursion/" title="C++ Recursion">C++ Recursion</a> (0)</li><li><a
href="http://talkbinary.com/programming/c/c-recursion-binary-search/" title="C++ Recursion &#8211; Binary Search">C++ Recursion &#8211; Binary Search</a> (0)</li></ul>]]></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 reverse a sequence of numbers printed using a recursive function in C++. 10 9 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 9 10 Printing numbers using recursion To understand how to print [...]]]></description> <content:encoded><![CDATA[<h3>Printing a sequence of numbers in reverse</h3><hr
/> Below is an example of how to reverse a sequence of numbers printed using a recursive function in C++.</p><pre>10 9 8 7 6 5 4 3 2 1</pre><pre>1 2 3 4 5 6 7 8 9 10</pre><h3>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.<br
/> <br/><br/></p><pre lang="c++">void print(int n) {
    if ( n <= 0 ) return; //Terminating condition

     cout << n << " "; //Prints number n

     print(n-1); //Calls itself with (n-1)

     return; //Returns from the function
}</pre><p>The code above is explained in <a
href="http://talkbinary.com/programming/c/introduction-to-recursion-in-c/">Introduction to Recursion in C++</a>.</p><p>Example:</p><pre> print(3); </pre><p>Produces:</p><pre>3 2 1</pre><h3>Printing Numbers in Reverse</h3><hr
/> Now, how would you print out the previous sequence of numbers but in reverse order using recursion?<br
/> <br/><br
/> Think about it. The difference from the previous question and this one is the order in which the numbers are printed. This only requires a simple modification from the previous example.<br
/> <br/><span
id="more-2003"></span><br
/> Hint: Try moving your printing statement around your function.<br/></p><h3  class="related_post_title">Related Posts</h3><ul
class="related_post"><li><a
href="http://talkbinary.com/programming/c/c-recursion/" title="C++ Recursion">C++ Recursion</a> (0)</li><li><a
href="http://talkbinary.com/programming/c/recursion-examples/" title="C++ Recursion Examples">C++ Recursion Examples</a> (0)</li><li><a
href="http://talkbinary.com/programming/c/c-recursion-palindrome/" title="C++ Recursion &#8211; Palindrome">C++ Recursion &#8211; Palindrome</a> (0)</li><li><a
href="http://talkbinary.com/programming/c/c-recursion-reverse-elements-of-a-vector/" title="C++ Recursion &#8211; Reverse elements of a vector">C++ Recursion &#8211; Reverse elements of a vector</a> (0)</li><li><a
href="http://talkbinary.com/programming/c/c-recursion-greatest-common-divisor/" title="C++ Recursion &#8211; Greatest Common Divisor ">C++ Recursion &#8211; Greatest Common Divisor </a> (0)</li><li><a
href="http://talkbinary.com/programming/c/c-recursion-printing-elements-of-a-vector/" title="C++ Recursion &#8211; Printing Elements of a Vector">C++ Recursion &#8211; Printing Elements of a Vector</a> (0)</li><li><a
href="http://talkbinary.com/programming/c/c-recursion-power-continued/" title="C++ Recursion &#8211; Power Continued">C++ Recursion &#8211; Power Continued</a> (0)</li><li><a
href="http://talkbinary.com/programming/c/c-recursion-power/" title="C++ Recursion &#8211; Power">C++ Recursion &#8211; Power</a> (1)</li><li><a
href="http://talkbinary.com/programming/c/c-recursion-factorial/" title="C++ Recursion &#8211; Factorial">C++ Recursion &#8211; Factorial</a> (0)</li><li><a
href="http://talkbinary.com/programming/c/c-recursion-summation/" title="C++ Recursion &#8211; Summation ">C++ Recursion &#8211; Summation </a> (2)</li></ul>]]></content:encoded> <wfw:commentRss>http://talkbinary.com/programming/c/c-recursion-printing-a-sequence-of-numbers-in-reverse/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced) (user agent is rejected)
Database Caching 25/76 queries in 0.294 seconds using disk

Served from: talkbinary.com @ 2010-07-31 10:17:11 -->