1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| #include<stdlib.h>
#include<stdio.h>
void init_tab(unsigned int** tab, unsigned int* len) {
if ((tab == NULL) || (len == NULL)) { return; }
unsigned int ftab[] = {1, 1, 5, 8, 9, 9, 6, 4, 3, 8 , 2, 7, 7, 9, 5, 4, 1, 10, 6, 11, 6};
unsigned int flen = (sizeof(ftab) / sizeof(ftab[0])), pos = 0;
(*tab) = malloc(flen * sizeof(unsigned int));
(*len) = flen;
for(; pos < flen; ++pos) {
(*tab)[pos] = ftab[pos];
}
}
void print_tab(unsigned int* tab, unsigned int len) {
if (tab == NULL) { return; }
unsigned int pos = 0;
for(; pos < len; ++pos) {
printf("%d ", tab[pos]);
}
printf("\n");
}
int main(int argc, char* argv[])
{
unsigned int* tab = NULL;
unsigned int* new_tab = NULL;
unsigned int len = 0, index = 1, pos = 1, search = 0;
unsigned char is_found = 0;
init_tab(&tab, &len);
print_tab(tab, len);
for(pos = 1; pos < len; ++pos) {
for(search = 0, is_found = 0; ((search < index) && (!is_found)); ++search) {
if (tab[search] == tab[pos]) {
is_found = 1;
}
}
if (!is_found) {
tab[index] = tab[pos];
++index;
}
}
new_tab = realloc(tab, index * sizeof(unsigned int));
if (new_tab == NULL) { return 0; }
tab = new_tab;
len = index;
print_tab(tab, index);
free(new_tab);
return 0;
} |
Partager