<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" version="2.0">

<channel>
	<title>Código C++</title>
	
	<link>http://codigoc.org</link>
	<description>Ayuda para tu tarea en C++</description>
	<lastBuildDate>Sat, 04 Sep 2010 23:34:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/codigoc" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="codigoc" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">codigoc</feedburner:emailServiceId><feedburner:feedburnerHostname xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>Centrar texto automáticamente sin gotoxy</title>
		<link>http://codigoc.org/417-centrar-texto-automaticamente-sin-gotoxy</link>
		<comments>http://codigoc.org/417-centrar-texto-automaticamente-sin-gotoxy#comments</comments>
		<pubDate>Sat, 04 Sep 2010 23:34:08 +0000</pubDate>
		<dc:creator>THEbatzuk</dc:creator>
				<category><![CDATA[Programas]]></category>
		<category><![CDATA[cadenas]]></category>
		<category><![CDATA[string.h]]></category>

		<guid isPermaLink="false">http://codigoc.org/?p=417</guid>
		<description><![CDATA[Pues si, en este post vamos a hacer un programa en c++ que lee una frase y luego la muestra centrada en la pantalla, todo eso ¡sin usar gotoxy! wow. Bueno, usando gotoxy sería mas fácil, pero este blog promueve el no uso de la librería conio.h (Cómo dejar de usar conio.h) así que lo [...]]]></description>
			<content:encoded><![CDATA[<p>Pues si, en este post vamos a hacer un <strong>programa en c++</strong> que lee una frase y luego la muestra centrada en la pantalla, todo eso ¡<strong>sin usar gotoxy</strong>! wow.</p>
<p>Bueno, usando <em>gotoxy</em> sería mas fácil, pero este blog promueve el no uso de la <strong>librería conio.h</strong> (<a href="http://codigoc.org/274-como-dejar-de-usar-conio-h">Cómo dejar de usar conio.h</a>) así que lo vamos a hacer como les dije.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include&lt;iostream&gt;</span>
<span style="color: #339900;">#include&lt;string.h&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">char</span> str<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">100</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Una frase: &quot;</span><span style="color: #008080;">;</span> <span style="color: #0000dd;">cin</span>.<span style="color: #007788;">getline</span><span style="color: #008000;">&#40;</span>str, <span style="color: #0000dd;">100</span>, <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">int</span> numofch <span style="color: #000080;">=</span> <span style="color: #0000dd;">strlen</span><span style="color: #008000;">&#40;</span>str<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</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><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">40</span><span style="color: #000040;">-</span><span style="color: #008000;">&#40;</span>numofch<span style="color: #000040;">/</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
        <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; &quot;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> str<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cin</span>.<span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</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></div></div>

<p>A la consola le caben 80 caracteres a lo largo, su mitad son 40; enseguida tenemos que averiguar el numero de caracteres que tiene la frase que se acaba de introducir y dividirlo entre dos. Supongamos que la frase es &#8220;hola&#8221;:</p>
<ol>
<li>&#8216;Hola&#8217; tiene 4 caracteres.</li>
<li>La mitad de la consola es 40.</li>
<li>Si a los 40 le restamos la mitad de los caracteres de la frase, obtenemos el punto en donde debe comenzar la frase para quedar centrada. 40 &#8211; (4/2) = 38</li>
<li>Ahora con un ciclo damos 38 espacios y luego imprimimos la frase.</li>
</ol>
<p>Si quieren utilizar <em>gotoxy</em>, el proceso es el mismo hasta el último paso, donde guardarían el valor obtenido en una variable y luego lo pondrían en el lugar adecuado de la función.</p>
<p>La función <strong>strlen</strong> está en la librería <em>string.h</em> y nos devuelve el número de caracteres que tiene una cadena, viene de <em>string length.</em></p>
<p>A propósito me he dado cuenta que no he hablado casi nada de la librería <a href="http://codigoc.org/tag/string-h">string.h</a>, voy a ver si hago unos cuantos posts sobre ella.</p>
<hr />
<p><small>Post escrito en <a href="http://codigoc.org">Código C++</a> © 2010. |
<a href="http://codigoc.org/417-centrar-texto-automaticamente-sin-gotoxy">Permalink</a> |
<a href="http://codigoc.org/417-centrar-texto-automaticamente-sin-gotoxy#comments">Sin comentarios</a>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://codigoc.org/417-centrar-texto-automaticamente-sin-gotoxy/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Declarar variable dentro de un ciclo for</title>
		<link>http://codigoc.org/409-declarar-variable-dentro-de-un-ciclo-for</link>
		<comments>http://codigoc.org/409-declarar-variable-dentro-de-un-ciclo-for#comments</comments>
		<pubDate>Wed, 01 Sep 2010 23:04:23 +0000</pubDate>
		<dc:creator>THEbatzuk</dc:creator>
				<category><![CDATA[Práctica]]></category>
		<category><![CDATA[for]]></category>
		<category><![CDATA[variables]]></category>

		<guid isPermaLink="false">http://codigoc.org/?p=409</guid>
		<description><![CDATA[En el programa anterior use la &#8220;técnica&#8221; de declarar una variable dentro de un ciclo for y luego de publicarla me di cuenta de que algunas personas podrían encontrar eso algo confuso. Es casi exactamente lo mismo escribir esto: int i=0; for(i=0; i&#60;=10; i++) { printf("%i", i); } &#8230;que esto: for(int i=0; i&#60;=10; i++) { [...]]]></description>
			<content:encoded><![CDATA[<p>En el programa anterior use la &#8220;técnica&#8221; de declarar una variable dentro de un <a href="http://codigoc.org/tag/for">ciclo for</a> y luego de publicarla me di cuenta de que algunas personas podrían encontrar eso algo confuso.</p>
<p>Es casi exactamente lo mismo escribir esto:<br />
<code>int i=0;<br />
for(i=0; i&lt;=10; i++)<br />
{<br />
printf("%i", i);<br />
}</code><br />
&#8230;que esto:<br />
<code>for(int i=0; i&lt;=10; i++)<br />
{<br />
printf("%i", i);<br />
}</code></p>
<p>La única diferencia es que en el primero &#8216;i&#8217; es una <a href="http://codigoc.org/tag/variables">variable</a> como cualquier otra y en el segundo &#8216;i&#8217; solo sirve dentro del <strong>ciclo for</strong>. Esto mas que ser una desventaja es una ventaja, ya que te permite usar la misma variable en todos lo ciclos que quieras en el mismo programa. Yo en lo personal siempre uso &#8216;i&#8217; como la variable que controla cualquier ciclo, si llegara a haber un ciclo dentro de otro, uso &#8216;ii&#8217;.</p>
<hr />
<p><small>Post escrito en <a href="http://codigoc.org">Código C++</a> © 2010. |
<a href="http://codigoc.org/409-declarar-variable-dentro-de-un-ciclo-for">Permalink</a> |
<a href="http://codigoc.org/409-declarar-variable-dentro-de-un-ciclo-for#comments">Sin comentarios</a>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://codigoc.org/409-declarar-variable-dentro-de-un-ciclo-for/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Factorial de forma sencilla en C++</title>
		<link>http://codigoc.org/405-factorial-de-forma-sencilla-en-c</link>
		<comments>http://codigoc.org/405-factorial-de-forma-sencilla-en-c#comments</comments>
		<pubDate>Sun, 29 Aug 2010 23:02:34 +0000</pubDate>
		<dc:creator>THEbatzuk</dc:creator>
				<category><![CDATA[Programas]]></category>
		<category><![CDATA[factorial]]></category>

		<guid isPermaLink="false">http://codigoc.org/?p=405</guid>
		<description><![CDATA[Ya vimos como calcular el factorial de un número c++ en forma recursiva, sin embargo hay una forma mucho mas fácil de entender y de aplicar (pero menos eficiente). Vamos viendo: Si el factorial de un número es la multiplicación de cada número desde 1 hasta ese número (por ejemplo factorial de 4 = 1*2*3*4 [...]]]></description>
			<content:encoded><![CDATA[<p>Ya vimos como calcular el <strong>factorial de un número c++</strong> en <a href="http://codigoc.org/370-recursividad-aplicada-factorial">forma recursiva</a>, sin embargo hay una forma mucho mas fácil de entender y de aplicar (<em>pero menos eficiente</em>). Vamos viendo:</p>
<p>Si el factorial de un número es la multiplicación de cada número desde 1 hasta ese número (<em>por ejemplo factorial de 4 = 1*2*3*4 = 24</em>), entonces es muy sencillo crear un ciclo de 1 hasta el número pedido en c++ para hacer el cálculo.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include&lt;iostream&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> num,fact<span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;::FACTORIAL::&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Introduce un numero: &quot;</span><span style="color: #008080;">;</span> <span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> num<span style="color: #008080;">;</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;">2</span><span style="color: #008080;">;</span> i<span style="color: #000080;">&lt;=</span>num<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        fact <span style="color: #000080;">=</span> fact <span style="color: #000040;">*</span> i<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Su factorial es: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> fact<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cin</span>.<span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><span style="color: #0000dd;">cin</span>.<span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</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></div></div>

<p>¿Entonces porqué el ciclo empieza en 2? Si comenzara en 1, el proceso para el numero 3 (<em>por poner un ejemplo</em>) sería así:</p>
<ul>
<li>fact = fact * i -&gt; fact = 1 * 1 -&gt; fact = 1</li>
<li>fact = fact * i -&gt; fact = 1 * 2 -&gt; fact = 2</li>
<li>fact = fact * i -&gt; fact = 2 * 3 -&gt; fact = 6</li>
</ul>
<p>Como ven, el primer paso es totalmente innecesario.</p>
<hr />
<p><small>Post escrito en <a href="http://codigoc.org">Código C++</a> © 2010. |
<a href="http://codigoc.org/405-factorial-de-forma-sencilla-en-c">Permalink</a> |
<a href="http://codigoc.org/405-factorial-de-forma-sencilla-en-c#comments">3 comentarios</a>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://codigoc.org/405-factorial-de-forma-sencilla-en-c/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Recursividad aplicada: Torres de Hanoi</title>
		<link>http://codigoc.org/397-recursividad-aplicada-torres-de-hanoi</link>
		<comments>http://codigoc.org/397-recursividad-aplicada-torres-de-hanoi#comments</comments>
		<pubDate>Tue, 24 Aug 2010 05:21:06 +0000</pubDate>
		<dc:creator>THEbatzuk</dc:creator>
				<category><![CDATA[Programas]]></category>
		<category><![CDATA[hanoi]]></category>
		<category><![CDATA[recursividad]]></category>

		<guid isPermaLink="false">http://codigoc.org/?p=397</guid>
		<description><![CDATA[Este juego matemático es clásico de los problemas de programación. Hoy vamos a ver cómo calcular el número de movimientos necesarios para resolver el juego según el número de discos, de forma recursiva en C++. #include&#60;stdio.h&#62; int hanoi&#40;int n&#41; &#123; if&#40;n==1&#41; return 1; else return 2 * hanoi&#40;n-1&#41; + 1; &#125; int main&#40;&#41; &#123; int [...]]]></description>
			<content:encoded><![CDATA[<p>Este juego matemático es clásico de los <strong>problemas de programación</strong>. Hoy vamos a ver cómo calcular el número de movimientos necesarios para resolver el juego según el número de discos, de <strong>forma recursiva en C++</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include&lt;stdio.h&gt;</span>
<span style="color: #0000ff;">int</span> hanoi<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;">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>
    <span style="color: #0000ff;">else</span>
        <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">2</span> <span style="color: #000040;">*</span> hanoi<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> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> disc, mov<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;::TORRES DE HANOI::<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Numero de discos: &quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><span style="color: #0000dd;">scanf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;%i&quot;</span>,<span style="color: #000040;">&amp;</span>disc<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>Movimientos necesarios: %i<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, hanoi<span style="color: #008000;">&#40;</span>disc<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</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></div></div>

<p>Otro algoritmo raro. Lo que sabemos es que si tenemos 1 disco el número de movimientos es 1 y que ese es nuestro caso base (<em>si n==1 retorna 1</em>), a partir de ahí el número de movimientos se puede calcular si multiplicamos por dos el número de movimientos para <em>n-1</em> y le sumamos 1, o sea: <em>2 + hanoi(n-1) + 1</em></p>
<ul>
<li>Si son 2 discos, entonces 2 * hanoi(1) + 1 = 2 * 1 + 1 = 3</li>
<li>Si son 3 discos, entonces 2 * hanoi(2) + 1 = 2 * 3 + 1 = 7</li>
<li>etc</li>
</ul>
<hr />
<p><small>Post escrito en <a href="http://codigoc.org">Código C++</a> © 2010. |
<a href="http://codigoc.org/397-recursividad-aplicada-torres-de-hanoi">Permalink</a> |
<a href="http://codigoc.org/397-recursividad-aplicada-torres-de-hanoi#comments">Sin comentarios</a>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://codigoc.org/397-recursividad-aplicada-torres-de-hanoi/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Recursividad aplicada: Máximo Común Divisor</title>
		<link>http://codigoc.org/393-recursividad-aplicada-maximo-comun-divisor</link>
		<comments>http://codigoc.org/393-recursividad-aplicada-maximo-comun-divisor#comments</comments>
		<pubDate>Sat, 21 Aug 2010 05:05:45 +0000</pubDate>
		<dc:creator>THEbatzuk</dc:creator>
				<category><![CDATA[Programas]]></category>
		<category><![CDATA[recursividad]]></category>

		<guid isPermaLink="false">http://codigoc.org/?p=393</guid>
		<description><![CDATA[Ooootro post sobre recursividad, pero no se preocupen ya solo queda este y otro. Esta vez vamos a calcular el máximo común divisor de dos números de forma recursiva en c++. #include&#60;stdio.h&#62; int MCD&#40;int x, int y&#41; &#123; if&#40;y==0&#41; return x; else return MCD&#40;y, x%y&#41;; &#125; int main&#40;&#41; &#123; int num1=0,num2=0; printf&#40;&#34;::MAXIMO COMUN DIVISOR::\n&#34;&#41;; printf&#40;&#34;Introduce [...]]]></description>
			<content:encoded><![CDATA[<p>Ooootro post sobre <strong>recursividad</strong>, pero no se preocupen ya solo queda este y otro. Esta vez vamos a calcular el <strong>máximo común divisor</strong> de dos números de <strong>forma recursiva en c++</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include&lt;stdio.h&gt;</span>
<span style="color: #0000ff;">int</span> MCD<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">int</span> y<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>y<span style="color: #000080;">==</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
        <span style="color: #0000ff;">return</span> x<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">else</span>
        <span style="color: #0000ff;">return</span> MCD<span style="color: #008000;">&#40;</span>y, x<span style="color: #000040;">%</span>y<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> num1<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span>,num2<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;::MAXIMO COMUN DIVISOR::<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Introduce el primer numero: &quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><span style="color: #0000dd;">scanf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;%i&quot;</span>,<span style="color: #000040;">&amp;</span>num1<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Introduce el segundo numero: &quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><span style="color: #0000dd;">scanf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;%i&quot;</span>,<span style="color: #000040;">&amp;</span>num2<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>El resultado es: %i<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, MCD<span style="color: #008000;">&#40;</span>num1, num2<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</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></div></div>

<p>Este es uno de esos <strong>algoritmos recursivos</strong> raros o, mejor dicho, difíciles de comprender, mi cerebro estuvo un buen rato echando humo tratando de comprender la lógica con que lo armaron y mi conclusión es que funciona de pura rana. Bueno, al final si supe como funciona pero no porque.</p>
<hr />
<p><small>Post escrito en <a href="http://codigoc.org">Código C++</a> © 2010. |
<a href="http://codigoc.org/393-recursividad-aplicada-maximo-comun-divisor">Permalink</a> |
<a href="http://codigoc.org/393-recursividad-aplicada-maximo-comun-divisor#comments">Sin comentarios</a>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://codigoc.org/393-recursividad-aplicada-maximo-comun-divisor/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Recursividad aplicada: Serie de Fibonacci</title>
		<link>http://codigoc.org/386-recursividad-aplicada-serie-de-fibonacci</link>
		<comments>http://codigoc.org/386-recursividad-aplicada-serie-de-fibonacci#comments</comments>
		<pubDate>Wed, 18 Aug 2010 05:03:32 +0000</pubDate>
		<dc:creator>THEbatzuk</dc:creator>
				<category><![CDATA[Programas]]></category>
		<category><![CDATA[fibonacci]]></category>
		<category><![CDATA[recursividad]]></category>

		<guid isPermaLink="false">http://codigoc.org/?p=386</guid>
		<description><![CDATA[0, 1, 1, 2, 3, 5, 8, 13, 21, &#8230;, la serie de Fibonacci es bastante interesante e incluso aparece en la naturaleza. Comienza con un 0, luego un 1 y a partir de ahí cada número es la suma de los dos siguientes. Eso último nos indica recursividad. Veamos como mostrar n números de [...]]]></description>
			<content:encoded><![CDATA[<p>0, 1, 1, 2, 3, 5, 8, 13, 21, &#8230;, la serie de Fibonacci es bastante interesante e incluso aparece <a href="http://thebatzuk.org/2010/01/fibonacci-en-todas-partes.html">en la naturaleza</a>. Comienza con un 0, luego un 1 y a partir de ahí cada número es la suma de los dos siguientes. Eso último nos indica <strong>recursividad</strong>. Veamos como mostrar <em>n</em> números de la <strong>serie de Fibonacci</strong> de <strong>forma recursiva en C++</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include&lt;stdio.h&gt;</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>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>n<span style="color: #000080;">&lt;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span>
        <span style="color: #0000ff;">return</span> n<span style="color: #008080;">;</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>
<span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> num<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span>,res<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;::NUMEROS DE FIBONACCI::<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Introduce el numero de numeros: &quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><span style="color: #0000dd;">scanf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;%i&quot;</span>,<span style="color: #000040;">&amp;</span>num<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</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;">0</span><span style="color: #008080;">;</span>i<span style="color: #000080;">&lt;=</span>num<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        res <span style="color: #000080;">=</span> fibonacci<span style="color: #008000;">&#40;</span>i<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;%i  &quot;</span>, res<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</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></div></div>

<p>Es la primera vez que vemos un la llamada a la función dentro de un ciclo. Eso es porque la función calcula el <em>n-ésimo</em> número de la serie, y para mostrarlos todos tenemos que hacer un ciclo que repita la función tantas veces como sea necesario.</p>
<hr />
<p><small>Post escrito en <a href="http://codigoc.org">Código C++</a> © 2010. |
<a href="http://codigoc.org/386-recursividad-aplicada-serie-de-fibonacci">Permalink</a> |
<a href="http://codigoc.org/386-recursividad-aplicada-serie-de-fibonacci#comments">Un comentario</a>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://codigoc.org/386-recursividad-aplicada-serie-de-fibonacci/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Recursividad aplicada: Convertir Decimal a Binario</title>
		<link>http://codigoc.org/380-recursividad-aplicada-convertir-decimal-a-binario</link>
		<comments>http://codigoc.org/380-recursividad-aplicada-convertir-decimal-a-binario#comments</comments>
		<pubDate>Sun, 15 Aug 2010 04:43:56 +0000</pubDate>
		<dc:creator>THEbatzuk</dc:creator>
				<category><![CDATA[Programas]]></category>
		<category><![CDATA[binario]]></category>
		<category><![CDATA[recursividad]]></category>

		<guid isPermaLink="false">http://codigoc.org/?p=380</guid>
		<description><![CDATA[Siguiendo con esto de la recursividad hoy les traigo un nuevo programín, Convertir Decimal a Binario de forma recursiva. #include&#60;stdio.h&#62; void binario&#40;int n&#41; &#123; if &#40;n!=0&#41; &#123; binario&#40;n/2&#41;; printf&#40;&#34;%i&#34;,n%2&#41;; &#125; &#125; int main&#40;&#41; &#123; int num=0; printf&#40;&#34;::CONVERTIR DECIMAL A BINARIO::\n&#34;&#41;; printf&#40;&#34;Introduce un numero: &#34;&#41;;scanf&#40;&#34;%i&#34;,&#38;num&#41;; //Pedir variable num printf&#40;&#34;\t&#34;&#41;;binario&#40;num&#41;;printf&#40;&#34;\n&#34;&#41;; //Llamar la función return 0; &#125; Está [...]]]></description>
			<content:encoded><![CDATA[<p>Siguiendo con esto de la <strong>recursividad</strong> hoy les traigo un nuevo programín, <strong>Convertir Decimal a Binario</strong> de <strong>forma recursiva</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include&lt;stdio.h&gt;</span>
<span style="color: #0000ff;">void</span> binario<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: #000040;">!</span><span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        binario<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: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;%i&quot;</span>,n<span style="color: #000040;">%</span><span style="color:#800080;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> num<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;::CONVERTIR DECIMAL A BINARIO::<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Introduce un numero: &quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><span style="color: #0000dd;">scanf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;%i&quot;</span>,<span style="color: #000040;">&amp;</span>num<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">//Pedir variable num</span>
    <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>binario<span style="color: #008000;">&#40;</span>num<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">//Llamar la función</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></div></div>

<p>Está basado en el método que usamos para convertir un número decimal a binario &#8220;a mano&#8221;: dividir el número entre dos hasta que quede 0 y apuntar los residuos del último al primero.</p>
<hr />
<p><small>Post escrito en <a href="http://codigoc.org">Código C++</a> © 2010. |
<a href="http://codigoc.org/380-recursividad-aplicada-convertir-decimal-a-binario">Permalink</a> |
<a href="http://codigoc.org/380-recursividad-aplicada-convertir-decimal-a-binario#comments">Sin comentarios</a>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://codigoc.org/380-recursividad-aplicada-convertir-decimal-a-binario/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Recursividad aplicada: Factorial</title>
		<link>http://codigoc.org/370-recursividad-aplicada-factorial</link>
		<comments>http://codigoc.org/370-recursividad-aplicada-factorial#comments</comments>
		<pubDate>Thu, 12 Aug 2010 02:58:55 +0000</pubDate>
		<dc:creator>THEbatzuk</dc:creator>
				<category><![CDATA[Programas]]></category>
		<category><![CDATA[factorial]]></category>
		<category><![CDATA[recursividad]]></category>

		<guid isPermaLink="false">http://codigoc.org/?p=370</guid>
		<description><![CDATA[Hola de nuevo, ¿cuánto tiempo pasó? mmm no sé pero bueno&#8230; lo importante es que ya estoy de vuelta y ahora sí me voy a dar el tiempo de actualizar este blog más seguido, pero basta de cosas personales. No me quiero meter en rollos de explicar lo que es la recursividad (googleen y diviértanse [...]]]></description>
			<content:encoded><![CDATA[<p>Hola de nuevo, ¿cuánto tiempo pasó? mmm no sé pero bueno&#8230; lo importante es que ya estoy de vuelta y ahora sí me voy a dar el tiempo de actualizar este blog más seguido, pero basta de cosas personales.</p>
<p>No me quiero meter en rollos de explicar lo que es la <strong>recursividad</strong> (<em>googleen y diviértanse leyendo</em>), lo que quiero es mostrarles algunos ejemplos muy sencillos de dónde podemos aplicar la recursividad. Hoy toca <strong>calcular el factorial</strong> de un numero <strong>de forma recursiva</strong>.</p>
<p>Es el código recursivo más fácil que se pueden encontrar:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include&lt;stdio.h&gt;</span>
<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>
  <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>n<span style="color: #000080;">&lt;</span><span style="color: #0000dd;">2</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: #0000ff;">else</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>
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">int</span> num<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
  <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;::CALCULAR FACTORIAL::<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Introduce un numero: &quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><span style="color: #0000dd;">scanf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;%i&quot;</span>,<span style="color: #000040;">&amp;</span>num<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">//Pedir variable num</span>
  <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>El resultado es: %i<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, factorial<span style="color: #008000;">&#40;</span>num<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">//Llama la funcion e imprime resultado</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></div></div>

<h3>Explicación</h3>
<p> El caso base es que cuando <em>n</em> valga 1 o 0 retorna un 1, de lo contrario retorna la multiplicación de n * el factorial del numero anterior <em>n-1</em>. Supongamos que introducimos el número 3, cuyo factorial es 6 (<em>3*2*1 = 6</em>).</p>
<ol>
<li><strong>n=3</strong> No entra al caso base. Guardamos para después la operación 3 * factorial(2)</li>
<li><strong>n=2</strong> No entra al caso base. Guardamos para después la operación 2 * factorial(1)</li>
<li><strong>n=1</strong> Entra al caso base. Retorna 1, por lo tanto factorial(1) = 1</li>
<li>Hacemos la última operación que guardamos  2 * factorial(1) = 2 * 1 = 2, por lo tanto factorial(2) = 2</li>
<li>Hacemos la siguiente operación que guardamos  3 * factorial(2) = 3 * 2 = 6</li>
<li><strong>El factorial es 6</strong></li>
</ol>
<p>¡Uh que fácil!</p>
<hr />
<p><small>Post escrito en <a href="http://codigoc.org">Código C++</a> © 2010. |
<a href="http://codigoc.org/370-recursividad-aplicada-factorial">Permalink</a> |
<a href="http://codigoc.org/370-recursividad-aplicada-factorial#comments">4 comentarios</a>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://codigoc.org/370-recursividad-aplicada-factorial/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Obtener numeros aleatorios en C++ (rand, srand)</title>
		<link>http://codigoc.org/354-obtener-numeros-aleatorios-en-c-rand-srand</link>
		<comments>http://codigoc.org/354-obtener-numeros-aleatorios-en-c-rand-srand#comments</comments>
		<pubDate>Wed, 25 Nov 2009 22:45:03 +0000</pubDate>
		<dc:creator>THEbatzuk</dc:creator>
				<category><![CDATA[Teoría]]></category>
		<category><![CDATA[rand]]></category>

		<guid isPermaLink="false">http://codigoc.org/?p=354</guid>
		<description><![CDATA[Es algo muy frecuente, cuando ya dominas todo eso de pedir y almacenar datos, ahora tu profesor te pedirá que tus programas generen números aleatorios para automatizar el proceso de llenar arreglos y todo eso. Así que lo primero que tenemos que hacer es incluir la librería: #include&#60;stdlib.h&#62; Luego inicializar los números aleatorios incluyendo esto: [...]]]></description>
			<content:encoded><![CDATA[<p>Es algo muy frecuente, cuando ya dominas todo eso de pedir y almacenar datos, ahora tu profesor te pedirá que tus programas <strong>generen números aleatorios</strong> para automatizar el proceso de llenar arreglos y todo eso.</p>
<p>Así que lo primero que tenemos que hacer es incluir la librería:<br />
<code>#include&lt;stdlib.h&gt;</code><br />
Luego inicializar los números aleatorios incluyendo esto:<br />
<code>srand(time(NULL));</code><br />
Luego guardar el número aleatorio en alguna parte:<br />
<code>num=rand();</code></p>
<p>Eso es básicamente. Para ajustar el<strong> rango de número aleatorios</strong> podemos hacer varias cosas.</p>
<p>Número aleatorios entre <strong>0 y 50</strong>:<br />
<code>num=rand()%51;</code></p>
<p>Número aleatorios entre<strong> 1 y 100</strong>:<br />
<code>num=1+rand()%(100-1);</code></p>
<p>Número aleatorios entre <strong>250 y 420</strong>:<br />
<code>num=250+rand()%(420-250);</code></p>
<p>De <strong>forma general</strong> es:<br />
<code>variable = limite_inferior + rand() % (limite_superior - limite_inferior) ;</code></p>
<p>Así que un programa que muestre 10 números aleatorios entre 1 y 10 quedaría así:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include&lt;stdlib.h&gt;</span>
<span style="color: #339900;">#include&lt;iostream&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> num,c<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">srand</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">time</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>c<span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>c<span style="color: #000080;">&lt;=</span><span style="color: #0000dd;">10</span><span style="color: #008080;">;</span>c<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        num<span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #000040;">+</span><span style="color: #0000dd;">rand</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">%</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">10</span><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: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span>num<span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot; &quot;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000dd;">cin</span>.<span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<hr />
<p><small>Post escrito en <a href="http://codigoc.org">Código C++</a> © 2009. |
<a href="http://codigoc.org/354-obtener-numeros-aleatorios-en-c-rand-srand">Permalink</a> |
<a href="http://codigoc.org/354-obtener-numeros-aleatorios-en-c-rand-srand#comments">4 comentarios</a>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://codigoc.org/354-obtener-numeros-aleatorios-en-c-rand-srand/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Histograma con números aleatorios y asteriscos</title>
		<link>http://codigoc.org/349-histograma-con-numeros-aleatorios-y-asteriscos</link>
		<comments>http://codigoc.org/349-histograma-con-numeros-aleatorios-y-asteriscos#comments</comments>
		<pubDate>Tue, 24 Nov 2009 02:36:57 +0000</pubDate>
		<dc:creator>THEbatzuk</dc:creator>
				<category><![CDATA[Programas]]></category>
		<category><![CDATA[arreglos]]></category>
		<category><![CDATA[rand]]></category>

		<guid isPermaLink="false">http://codigoc.org/?p=349</guid>
		<description><![CDATA[Bueno, tal vez el título quedó muy poco descriptivo, pero la idea es pedir un número de personas, luego se le pediría a cada persona su peso pero nosotros solo vamos a generar un número aleatorio en determinado rango. Luego se clasificaría a esas personas en rangos de peso y al final mostrar una tabla [...]]]></description>
			<content:encoded><![CDATA[<p>Bueno, tal vez el título quedó muy poco descriptivo, pero la idea es pedir un número de personas, luego se le pediría a cada persona su peso pero nosotros solo vamos a generar un número aleatorio en determinado rango. Luego se clasificaría a esas personas en rangos de peso y al final mostrar una tabla que muestre la frecuencia de cada rango con asteriscos, así:</p>
<p><code>51-60 ***<br />
61-70 **<br />
71-80 ***<br />
80-91 *<br />
91-mas *****</code></p>
<p>El código en C++ sería así:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include&lt;stdlib.h&gt;</span>
<span style="color: #339900;">#include&lt;iostream&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> n,c,c2,peso<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">500</span><span style="color: #008000;">&#93;</span>,cat<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">5</span><span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #008000;">&#123;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#125;</span>,lim<span style="color: #008080;">;</span>
    <span style="color: #666666;">// n -&gt; numero de personas</span>
    <span style="color: #666666;">// c y c2 para ciclos</span>
    <span style="color: #666666;">// peso[500] arreglo para guardar el peso de las personas</span>
    <span style="color: #666666;">// cat[5] arreglo para guardar el número de personas en cada categoria de peso</span>
    <span style="color: #0000dd;">srand</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">time</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// Inicializa los números aleatorios</span>
    <span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;Introduce el número de personas: &quot;</span><span style="color: #008080;">;</span><span style="color: #0000dd;">cin</span><span style="color: #000080;">&gt;&gt;</span>n<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>c<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>c<span style="color: #000080;">&lt;=</span>n<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>c<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        peso<span style="color: #008000;">&#91;</span>c<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #0000dd;">51</span><span style="color: #000040;">+</span><span style="color: #0000dd;">rand</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">%</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">120</span><span style="color: #000040;">-</span><span style="color: #0000dd;">51</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">//guarda un número aleatorio del 51 al 120 en cada elemento del arreglo</span>
        <span style="color: #666666;">// según su valor se aumenta un elemento de nuestro otro arreglo</span>
        <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>peso<span style="color: #008000;">&#91;</span>c<span style="color: #008000;">&#93;</span><span style="color: #000080;">&gt;</span><span style="color: #0000dd;">50</span> <span style="color: #000040;">&amp;&amp;</span> peso<span style="color: #008000;">&#91;</span>c<span style="color: #008000;">&#93;</span><span style="color: #000080;">&lt;=</span><span style="color: #0000dd;">60</span><span style="color: #008000;">&#41;</span>
            cat<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #000040;">++</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>peso<span style="color: #008000;">&#91;</span>c<span style="color: #008000;">&#93;</span><span style="color: #000080;">&gt;</span><span style="color: #0000dd;">60</span> <span style="color: #000040;">&amp;&amp;</span> peso<span style="color: #008000;">&#91;</span>c<span style="color: #008000;">&#93;</span><span style="color: #000080;">&lt;=</span><span style="color: #0000dd;">70</span><span style="color: #008000;">&#41;</span>
            cat<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #000040;">++</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>peso<span style="color: #008000;">&#91;</span>c<span style="color: #008000;">&#93;</span><span style="color: #000080;">&gt;</span><span style="color: #0000dd;">70</span> <span style="color: #000040;">&amp;&amp;</span> peso<span style="color: #008000;">&#91;</span>c<span style="color: #008000;">&#93;</span><span style="color: #000080;">&lt;=</span><span style="color: #0000dd;">80</span><span style="color: #008000;">&#41;</span>
            cat<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#93;</span><span style="color: #000040;">++</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>peso<span style="color: #008000;">&#91;</span>c<span style="color: #008000;">&#93;</span><span style="color: #000080;">&gt;</span><span style="color: #0000dd;">80</span> <span style="color: #000040;">&amp;&amp;</span> peso<span style="color: #008000;">&#91;</span>c<span style="color: #008000;">&#93;</span><span style="color: #000080;">&lt;=</span><span style="color: #0000dd;">90</span><span style="color: #008000;">&#41;</span>
            cat<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#93;</span><span style="color: #000040;">++</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>peso<span style="color: #008000;">&#91;</span>c<span style="color: #008000;">&#93;</span><span style="color: #000080;">&gt;</span><span style="color: #0000dd;">90</span><span style="color: #008000;">&#41;</span>
            cat<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#93;</span><span style="color: #000040;">++</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>c<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>c<span style="color: #000080;">&lt;=</span><span style="color: #0000dd;">4</span><span style="color: #008080;">;</span>c<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">switch</span><span style="color: #008000;">&#40;</span>c<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #666666;">// segun cada caso imprime algo diferente</span>
            <span style="color: #0000ff;">case</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">:</span>
                <span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;51-60<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">case</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">:</span>
                <span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;61-70<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">case</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">:</span>
                <span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;71-80<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">case</span> <span style="color: #0000dd;">3</span><span style="color: #008080;">:</span>
                <span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;81-90<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">case</span> <span style="color: #0000dd;">4</span><span style="color: #008080;">:</span>
                <span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;90-  <span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
        lim<span style="color: #000080;">=</span>cat<span style="color: #008000;">&#91;</span>c<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// no podemos usar un arreglo como limite del ciclo (la verdad</span>
                   <span style="color: #666666;">// no se porque), entonces lo pasamos a una variable normal</span>
        <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>c2<span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>c2<span style="color: #000080;">&lt;=</span>lim<span style="color: #008080;">;</span>c2<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
                <span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;*&quot;</span><span style="color: #008080;">;</span>
        <span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000dd;">cin</span>.<span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><span style="color: #0000dd;">cin</span>.<span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// detiene la ejecución al final (es como getch();)</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>A propósito, este programa fue una petición especial de un usuario. </p>
<hr />
<p><small>Post escrito en <a href="http://codigoc.org">Código C++</a> © 2009. |
<a href="http://codigoc.org/349-histograma-con-numeros-aleatorios-y-asteriscos">Permalink</a> |
<a href="http://codigoc.org/349-histograma-con-numeros-aleatorios-y-asteriscos#comments">Sin comentarios</a>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://codigoc.org/349-histograma-con-numeros-aleatorios-y-asteriscos/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
