Foro de elhacker.net

Programación => Ejercicios => Mensaje iniciado por: creiko en 30 Marzo 2016, 19:21 pm



Título: Realice un programa que me genere 4 números aleatorios (Entre 1 y 9) y me los or
Publicado por: creiko en 30 Marzo 2016, 19:21 pm
Realice un programa que me genere 4 números aleatorios (Entre 1 y 9) y me los ordene de mayor a menor.

bueno ya tengo los 4 numero generados aleatoriamente
necesito ayuda en el orden de menor a  mayor


Código
  1. #include <stdlib.h>
  2. #include <time.h>
  3. #include<iostream>
  4.  
  5.  
  6. int main()
  7. {
  8.    int num,i;
  9.    srand(time(NULL));
  10.  
  11.  
  12.  
  13.     printf("numero al azar entre 1 y 9 \n");
  14. for (i=1;i<=4;i++)
  15.     {
  16.  
  17.        num = 1 + rand() %  (1 - 9);
  18.        printf("%d",num);
  19.        printf("\n");
  20.        }
  21.  
  22. }
  23.  
  24.  


Mod: Los códigos deben ir en etiquetas GeSHi


Título: Re: Realice un programa que me genere 4 números aleatorios (Entre 1 y 9) y me los or
Publicado por: LaiaxanIV en 30 Marzo 2016, 20:33 pm
Código:
#include <iostream>
#include <vector>
using namespace std;


void ordena(vector<int>& v) {
    for (int i = 1; i < v.size(); ++i) {
        int x = v[i];
        int j = i;
        while (j > 0 and v[j - 1] > x) {
            v[j] = v[j - 1];
            --j;
        }
        v[j] = x;
    }
}

int main() {
   srand(time(NULL));
   vector<int> v(4);

   for (int i = 0; i < 4; ++i)v[i] = 1 + rand() %  (1 - 9);
   ordena(v);
   for (int i = 0; i < 4; ++i) cout << v[i] << " ";
   cout << endl;

}

Mira si compila y funciona, lo he hecho de cabeza y no lo he probado


Título: Re: Realice un programa que me genere 4 números aleatorios (Entre 1 y 9) y me los or
Publicado por: creiko en 30 Marzo 2016, 21:01 pm
Código
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.  
  8.   vector<int> v(4);
  9.   srand(time(NULL));   >:D >:D >:D
  10.   for (int i = 0; i < 4; ++i)v[i] = 1 + rand() %  (1 - 9);  >:D >:D >:D
  11.   ordena(v);  >:D >:D >:D >:D
  12.   for (int i = 0; i < 4; ++i) cout << v[i] << " ";
  13.   cout << endl;
  14.  
  15. }
  16. void ordena(vector<int>& v) {
  17.    for (int i = 1; i < v.size(); ++i) {
  18.        int x = v[i];
  19.        int j = i;
  20.        while (j > 0 and v[j - 1] > x) {
  21.            v[j] = v[j - 1];
  22.            --j;
  23.        }
  24.        v[j] = x;
  25.    }
  26. }
  27.  
  28.  
MARCA ESTOS ERRORES
[Error] 'time' was not declared in this scope
[Error] 'srand' was not declared in this scope
[Error] 'rand' was not declared in this scope
[Error] 'ordena' was not declared in this scope




Mod: Los códigos deben ir en etiquetas GeSHi


Título: Re: Realice un programa que me genere 4 números aleatorios (Entre 1 y 9) y me los or
Publicado por: Shell Root en 30 Marzo 2016, 21:29 pm
No se nada de esto, pero creería que necesita las librerías para usar las funciones en las que te genera errores, para este caso agrega lo siguiente:

Código
  1. #include <stdlib.h>     /* srand, rand */
  2. #include <time.h>       /* time */

:http://www.cplusplus.com/reference/cstdlib/srand/


Título: Re: Realice un programa que me genere 4 números aleatorios (Entre 1 y 9) y me los or
Publicado por: LaiaxanIV en 30 Marzo 2016, 23:33 pm
Además de necesitar las librerias, las funciones se declaran antes del main :D
Prueba asi:
Código:
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <time.h>
using namespace std;


void ordena(vector<int>& v) {
    for (int i = 1; i < v.size(); ++i) {
        int x = v[i];
        int j = i;
        while (j > 0 and v[j - 1] > x) {
            v[j] = v[j - 1];
            --j;
        }
        v[j] = x;
    }
}

int main() {
   srand(time(NULL));
   vector<int> v(4);

   for (int i = 0; i < 4; ++i)v[i] = 1 + rand() %  (1 - 9);
   ordena(v);
   for (int i = 0; i < 4; ++i) cout << v[i] << " ";
   cout << endl;

}


Título: Re: Realice un programa que me genere 4 números aleatorios (Entre 1 y 9) y me los or
Publicado por: creiko en 30 Marzo 2016, 23:41 pm
El ejercicio es asi
PERO
toca sin usar VECTORES Y ESTO
 >:D >:D >:D  x[4];    >:D >:D >:D >:D

Código
  1. #include <cstdlib> //para usar rand() enerador de numeros aleatorios
  2. #include <ctime> //para usar time()
  3. #include <iostream>
  4. #include <conio.h>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.  
  10. int x[4];
  11. int mx, temp,n=5;
  12. int i , j ;
  13.  
  14. cout << "\t\t\t\n Generacion y ordenamiento de numeros aleatorios \n";
  15. cout << "\n\n Se han generado los valores aleatorios en el rango 1  a 9 \n";
  16. srand((unsigned)time(0)); //establce una "semilla"
  17.  
  18.  
  19.  
  20. for(int i=1; i<=4; i++)
  21. {
  22. x[i] = ( rand() % 9 ) + 1;
  23. cout << x[i] << "\n";
  24. }
  25.  
  26.  
  27. for( i = 1 ; i < ( n - 1 ) ; i++)
  28. for( j = i + 1 ; j < n ; j++)
  29. if( x[i] < x[j] )
  30. {
  31. temp = x[i];
  32. x[i] = x[j];
  33. x[j] = temp;
  34. }
  35.  
  36.  
  37. cout << "\n\n Numeros ordenados de mayor a menor:" ;
  38. for( i = 1; i < n ; i++)
  39. cout << "\n "<< x[i] ;
  40.  
  41. for( i = 0 ; i < ( n - 1 ) ; i++)
  42. for( j = i + 1 ; j < n ; j++)
  43. if( x[i] > x[j] )
  44. {
  45. temp = x[i];
  46. x[i] = x[j];
  47. x[j] = temp;
  48. }
  49.  
  50.  
  51.  
  52. cout << "\n\n Numeros ordenados de menor a Mayor:" ;
  53. for( i = 1 ; i < n ; i++)
  54. cout << "\n " << x[i] ;
  55.  
  56. }


Mod: Los códigos deben ir en etiquetas GeSHi