Generar numeros Aleatorios y sacar el promedio de Estos
[cpp]</p><p>#include < iostream.h ><br /><%%KEEPWHITESPACE%%> #include < iomanip.h > //para presentacion con formato<br /><%%KEEPWHITESPACE%%> #include < stdlib.h ><br /><%%KEEPWHITESPACE%%> #include < time.h ><br /><%%KEEPWHITESPACE%%> int main(){<br /><%%KEEPWHITESPACE%%> const int tam_max=20;<br /><%%KEEPWHITESPACE%%> int aleatorios[tam_max];<br /><%%KEEPWHITESPACE%%> int i,suma=0;<br /><%%KEEPWHITESPACE%%> float promedio;<br /><%%KEEPWHITESPACE%%> srand((unsigned)time(NULL));<br /><%%KEEPWHITESPACE%%> for(i=0;i< tam_max;i++){<br /><%%KEEPWHITESPACE%%> aleatorios[i]=rand(); //asigna el numero<br /><%%KEEPWHITESPACE%%> cout< < "aleatorio generado: " //presenta el numero<br /><%%KEEPWHITESPACE%%> < < setw(5)< < aleatorios[i]< < endl;<br /><%%KEEPWHITESPACE%%> }<br /><%%KEEPWHITESPACE%%> for(i=0;i< tam_max;)<br /><%%KEEPWHITESPACE%%> suma+=aleatorios[i++]; //suma los elementos<br /><%%KEEPWHITESPACE%%> promedio=(float)suma/tam_max;<br /><%%KEEPWHITESPACE%%> cout< < endl< < "el promedio es: "< < promedio< < endl;<br /><%%KEEPWHITESPACE%%> cin.get();<br /><%%KEEPWHITESPACE%%> return 0;<br /><%%KEEPWHITESPACE%%> }</p><p>[/cpp]
0 comentarios :
Publicar un comentario