Mélange

gotjehiuh Messages postés 4 Date d'inscription mardi 24 août 2004 Statut Membre Dernière intervention 23 août 2005 - 24 août 2004 à 15:37
cs_djl Messages postés 3011 Date d'inscription jeudi 26 septembre 2002 Statut Membre Dernière intervention 27 novembre 2004 - 24 août 2004 à 16:41
j'ai deux tableau contenant des chiffres, et je voudrais les classer par ordre croissant dans un seul tableau mais je vois pas trop comment faire. est ce que quelqu'un peut m'aider svp.

3 réponses

cs_djl Messages postés 3011 Date d'inscription jeudi 26 septembre 2002 Statut Membre Dernière intervention 27 novembre 2004 7
24 août 2004 à 16:02
tu rassembles d'abord les tableaux en un seul et ensuite ti tri avec qsort

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

int compar2int( const void *a, const void *b );

int main()
{
int tab1[] = { 445, 545 ,56, 5,5 ,56 ,55 ,8 };
int tab2[] = { 7, 87, 78, 846, 5, 56, 8484 };
int *tab;
size_t size, size1, size2, i;

size1 = sizeof tab1 / sizeof *tab1;
size2 = sizeof tab2 / sizeof *tab2;
size = size1 + size2;

tab = malloc( size * sizeof(int) );

memcpy( tab, tab1, sizeof tab1 );
memcpy( tab + size1, tab2, sizeof tab2);

qsort( tab, size, sizeof(int), compar2int );

for( i = 0; i < size; i++ )
{
printf( "%d\n", tab[i] );
}

free( tab );
}

int compar2int( const void *a, const void *b )
{int ia *(int *)a, ib *(int *)b;
return ia > ib;
}
0
gotjehiuh Messages postés 4 Date d'inscription mardi 24 août 2004 Statut Membre Dernière intervention 23 août 2005
24 août 2004 à 16:30
j'ai pas trop compris le principe de qsort
0
cs_djl Messages postés 3011 Date d'inscription jeudi 26 septembre 2002 Statut Membre Dernière intervention 27 novembre 2004 7
24 août 2004 à 16:41
qsort effectue un "tri rapide" sur les elements du tableau passé en parametre, tu dois egalement lui fournir la fonction qui effectue la comparaison, ici compar2int

pour plus d'info, tout est expliqué ici

http://www.cplusplus.com/ref/cstdlib/qsort.html
0
Rejoignez-nous