Thursday, 15 July 2010

c - Find the union of two sets of 10 digit numbers -



c - Find the union of two sets of 10 digit numbers -

i'm trying find union of 2 sets of 10 digit numbers, i'm passing along 3 int arrays: first, second, , comp (this hold union set).

so far, i've added first , sec 1 array. thinking finding matching indices in comp[] filter through deleting them. figure there's much easier way. can give me hint?

basically i'm taking

first[] = [1,2,3,4,5,6,7,8,9,10]; second[] = [1,2,3,4,11,12,13,14,15,16];

and want homecoming

comp[] = [5,6,7,8,9,10,11,12,13,14,15,16];

the numbers won't in order.

int compound(int first[],int second[],int comp[]){ int i=0; int indicies[20]; for(int j = 0; j<size; j++){ comp[i]=first[j]; i++; } for(int k = 0; k<size; k++){ comp[i]=second[k]; i++; } int z=0; for(int l = 0; l<size*2; l++){ for(int m = 0; m<size*2; m++){ if(comp[l]==comp[m]){ indicies[z]=m; z++; }}} homecoming 0; }

a first step (nearly) sorting.

sort both input-sets (unless know sorted).

then iterate on both @ 1 time (two indices) , add together elements output fulfill criteria (seems union minus intersection, in one).

bonus: output-set sorted.

c

No comments:

Post a Comment