Dev - C++ - PROGRAMACI{ON EN C++

 
Vista:

PROGRAMACI{ON EN C++

Publicado por Problema Con Un CÓdigo En C++ (2 intervenciones) el 26/02/2006 01:13:38
HOLA A TODOS TENGO UN PROBLEMA CON EL SIGUIENTE CÓDIGO, LO QUE TRATO DE LOGRAR ES QUE NO SE REPITAN LOS NÚMEROS QUE CONFORMAN EL VETOR PERO NO ME ESTA FUNCIONANDO NECESITO IDEAS.

GRACIAS

#include <iostream.h>
#include <iomanip.h>
#include<cstdlib>
#include<ctime>
int main()
{

srand(time(0));//establece la semilla para el generador de números aleatorios
int vect[2],i,n,a;

//Genera el Vector Flag
a=0;
if(a==0)
{
for (i=0;i<=1;i++)
{
n=1+rand()%45;
vect[i]=n;
}
}

for (i=0;i<=1;i++)
{
if(vect[i]==vect[i+1])

cout<<"Se repite"<<endl;
a=0;
}

//Imprime el Vector flag
cout<<" "<<endl;
for (i=0;i<2;i++)
{
cout<<setw(3)<<i<<setw(5)<<vect[i]<<endl;

}

char r;
cout<<" "<<endl;
cout<<"Desea Continuar: "<<endl;
cin>>r;

if ((r=='s')||(r=='S'))
{
cout<<" "<<endl;
main();
}
cout<<" "<<endl;
return 0;
}

Archivo adjunto ( Número de descargas: 0 )
cpp1.cpp
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:PROGRAMACI{ON EN C++

Publicado por -.- (10 intervenciones) el 26/02/2006 06:45:51
Aca te doy un ejemplo como puedes hacerlo:
hice una funcion que le pasaba como parametros el vector, el contador del numero de elementos y el numero aleatorio ,para que verifique si ya existe y muestre un mensaje, en caso contrario le asignaba al vector el numero aleatorio generado ,, ^_^

#include <iostream.h>
#include <time.h>
#include <stdlib.h>

bool esRepetido(int arreglo[5],int i,int n)
{bool repetido=false;
for(int j=0;j<i;j++)
if(arreglo[j]==n)
{repetido=true;
break;}

return repetido;

}

int main()
{
srand(time(NULL));
int arreglo[5];
int n;
for(int i=0;i<5;i++)
{
//numero aleatorio de 0 a 9
n=rand()%10;

if(esRepetido(arreglo,i,n))
{
//si se repite disminuimos i para retroceder
cout<<"Repetido\n";
i--;
}
else
arreglo[i]=n;

}

cout<<endl<<"\nMostrando el arreglo\n";
for(int i=0;i<5;i++)
cout<<arreglo[i]<<endl;



system("pause");
return 0;
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:PROGRAMACI{ON EN C++

Publicado por noel solw (7 intervenciones) el 27/02/2006 09:54:36
// program sorteo.cpp
// written in borland c++, ver 4.52
// 2/5/2005

#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>

const int N = 20;

void Init(int *a)
{
for(int i = 0;i < N;i++)
a[i] = i + 1;
} // INIT

void Sorteo(int *a)
{
randomize();
for(int i = N-1;i >= 0;i--)
{
int k = random(i);
cout << a[k];
if(i)
cout << ',';
for(int j = k;j < N-1;j++)
a[j] = a[j+1];
}
cout << endl << endl;
} // SORTEO

int main()
{
int a[N];
Init(a);
Sorteo(a);
cout << "end of program - good bye ! ! ! " << endl;
return 0;
} // MAIN
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar