Admin Admin
Nombre de messages : 138 Age : 38 Date d'inscription : 02/09/2006
| Sujet: Fusionner le contenu de deux tableaux Jeu 5 Oct - 14:32 | |
| - Code:
-
#include<stdio.h>
#define N(a) (sizeof (a)/sizeof *(a))
static void print (char const *s, int const a[], size_t n) { size_t i;
printf ("%s: ", s); for (i = 0; i < n; i++) { printf ("%d ", a[i]); } printf ("\n"); }
int main(void) { int a[] = { 2, 5, 6, 8, 9, 11 }; int b[] = { 3, 6, 7, 9, 12 }; int c[N(a) + N(b)]; size_t ic = 0; { size_t ia = 0; size_t ib = 0;
print ("a", a + ia, N(a) - ia); print ("b", b + ib, N(b) - ib); print ("c", c, ic); printf ("\n");
while (ia != N(a) || ib != N(b)) { #if 1 /* Elimination des doublons */ if (a[ia] == b[ib]) { if (ia < N(a)) { c[ic] = a[ia]; ia++; } if (ib < N(b)) { ib++; } } else #endif if (a[ia] < b[ib]) { if (ia < N(a)) { c[ic] = a[ia]; ia++; } } else { if (ib < N(b)) { c[ic] = b[ib]; ib++; } } ic++; print ("a", a + ia, N(a) - ia); print ("b", b + ib, N(b) - ib); print ("c", c, ic); printf ("\n"); } }
print ("c", c, ic); return 0; }
si - Code:
-
#if 1 est remplacé par - Code:
-
#if 0 Les doublons présents seront supprimés. | |
|