June 20, 2006

A C program which prints itself

main(){char *s="main(){char *s=%c%s%c;printf(s,34,s,34,10);}";printf(s,34,s,34,10);}

cool declaration of 's'.
see here 34 is for ' " ' and 10 for newline.

June 15, 2006

removing duplicates from array

One simple way to do it is sort the array by some efficient algos ie O(N*log(N)).

rather than that you can create a new associative array with indexes as the value of the given array elements .

so to remove duplicate in, say,

A[]={1,3,5,3,8,1,3,5,8,5,5,4}
we create a new associative array B[] such that

for (i=0; i< style="color: rgb(51, 51, 255);"> B[A[i]] = 1;

now you can get the array of keys having value '1' in A
ie
A = getkey(B,1)
where getkey will return the array of keys where value of B[key]=1
so A will not have the duplicates.

swapping two variable without using temp

This is very trivial stuff. Still I thought it could be helpful to someone.

Let two variables to swap be 'a' and 'b'

so,
step 1) a = a + b
step 2) b = a - b
step 3) a = a - b