<?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; printing</title>
	<atom:link href="http://talkbinary.com/tag/printing/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; 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:01:35 -->
