C/Visual C - Necesito ayuda con arrays

 
Vista:

Necesito ayuda con arrays

Publicado por David (2 intervenciones) el 15/01/2009 12:10:56
Hola, me gustaria saber como puedo buscar en un array el elemento mayor numerico. Es decir tengo un array tengo un array con votos por ejemplo votos[1,2,0,5,3,4] y quiero sacar el mayor y la unica forma que he consegido es a lo bruto. es decir:
if(votos[0]>votos[1] && votos[0]>votos[2] &&.......
mejor=votos[0];
if(votos[1]>votos[2] && votos[1]>votos[3]&&.............
mejor=votos[1];
...........
...
.............
.......
....
con esos 5 o 6 if consigo sacar el mejor pero el problema es cuando quiero sacar todos los mejores es decir sacar una lista con los mas votados. por favor necesito ayuda aunque solo sea asesoramiento
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

qsort

Publicado por jose (21 intervenciones) el 15/01/2009 23:34:55
// PROBA ESTO, FUNCION QSORT:..

#include <stdlib.h>
#include <stdio.h>

int ORDEN;

/* comparar */
int comparar( const void *arg1,
const void *arg2 )
{
if( *(int *)arg1 < *(int * )arg2) return -ORDEN;
else if( *(int *)arg1 > *(int * )arg2) return ORDEN;
else return 0;
}


/* main */
void main()
{

int i;
int votos[10]; // 0 a 9

votos[0] = 3;
votos[1] = 5;
votos[2] = 10;
votos[3] = 1;
votos[4] = 21;
votos[5] = 31;
votos[6] = 55;
votos[7] = 100;
votos[8] = 15;
votos[9] = 25;


// Mostrar.
for( i=0; i<10; i++ )
{
printf( "votos[%i] = %i ", i, votos[i] );
}


// Sorteo ASC.
ORDEN = 1;
qsort( votos, 10, sizeof(int), comparar );
printf( " Sorteo ASC... " );


for( i=0; i<10; i++ )
{
printf( "votos[%i] = %i ", i, votos[i] );
}

// - - - - - - - - - - - - - - -


// Sorteo DESC.
ORDEN = -1;
qsort( votos, 10, sizeof(int), comparar );
printf( " Sorteo DESC... " );


// Mostrar.
for( i=0; i<10; i++ )
{
printf( "votos[%i] = %i ", i, votos[i] );
}

}
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