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
#include <stdlib.h>
#include <time.h>
#include<iostream>
int main()
{
int num,i;
srand(time(NULL));
printf("numero al azar entre 1 y 9 \n");
for (i=1;i<=4;i++)
{
num = 1 + rand() % (1 - 9);
printf("%d",num);
printf("\n");
}
}
Mod: Los códigos deben ir en etiquetas GeSHi
#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
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v(4);
srand(time(NULL)); >:D >:D >:D
for (int i = 0; i < 4; ++i)v[i] = 1 + rand() % (1 - 9); >:D >:D >:D
ordena(v); >:D >:D >:D >:D
for (int i = 0; i < 4; ++i) cout << v[i] << " ";
cout << endl;
}
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;
}
}
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
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:
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
:http://www.cplusplus.com/reference/cstdlib/srand/
Además de necesitar las librerias, las funciones se declaran antes del main :D
Prueba asi:
#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;
}
El ejercicio es asi
PERO
toca sin usar VECTORES Y ESTO
>:D >:D >:D x[4]; >:D >:D >:D >:D
#include <cstdlib> //para usar rand() enerador de numeros aleatorios
#include <ctime> //para usar time()
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int x[4];
int mx, temp,n=5;
int i , j ;
cout << "\t\t\t\n Generacion y ordenamiento de numeros aleatorios \n";
cout << "\n\n Se han generado los valores aleatorios en el rango 1 a 9 \n";
srand((unsigned)time(0)); //establce una "semilla"
for(int i=1; i<=4; i++)
{
x[i] = ( rand() % 9 ) + 1;
cout << x[i] << "\n";
}
for( i = 1 ; i < ( n - 1 ) ; i++)
for( j = i + 1 ; j < n ; j++)
if( x[i] < x[j] )
{
temp = x[i];
x[i] = x[j];
x[j] = temp;
}
cout << "\n\n Numeros ordenados de mayor a menor:" ;
for( i = 1; i < n ; i++)
cout << "\n "<< x[i] ;
for( i = 0 ; i < ( n - 1 ) ; i++)
for( j = i + 1 ; j < n ; j++)
if( x[i] > x[j] )
{
temp = x[i];
x[i] = x[j];
x[j] = temp;
}
cout << "\n\n Numeros ordenados de menor a Mayor:" ;
for( i = 1 ; i < n ; i++)
cout << "\n " << x[i] ;
}
Mod: Los códigos deben ir en etiquetas GeSHi