Friday, 6 April 2012

Approach #1 for generate the sequence

 #include<stdio.h>
#include<math.h>
main()
{
int n;
long long int num,i,x,j;
long int cnt=0;
printf("\n enter the number of bits :");
scanf("%d",&n);
num=pow(2,n);
printf("\n %lld",num);
for (i=0;i<num;i++)
{
    for (j=0;j<num;j++)
    {
    x=j;
    cnt=0;

    while (x)
        {
        if (x&1)
        cnt++;
        x=x>>1;
        }
    if (cnt==i)
        printf("\n %lld : %ld",j,cnt);
    }
}   
return 1;
}

Sunday, 1 April 2012

Generate this Sequence.

Your objective is to print all unsigned integers less than N, where N is the largest unsigned integer that can be represented by the number of bits specified by the user. The order in which the numbers should appear is decided by the following rules.
  1. 0 appear first, since its binary representation have zero ones.
  2. 1,2,4,8,.... appear next in the increasing order. Note that their binary representation have exactly one one. 
  3. 3,5,6,9,.... appear next in the increasing order. Note that their binary representation have exactly two one. 
  4. And so on.
One obvious approach is first store all the possible numbers in an array and then sort them based on the number of ones in their binary representation. But this is not what we need since we may need around 16GB of memory to store the numbers if inputs as small as 32. Another approach is to scan this 2^32 numbers 32 times, which may not be a big deal for our processors but still this in not the efficient way of doing it. we can either use some arithmetic relation between numbers of this sequence to find next number if any number in the sequence is given, or we can use bit-wise operators to develop an algorithm to find the next number for any given number.

Answer to Implement hash search in C

Here we use shift- folding as the hashing function and chaining for overflow handling.


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

#define MAXLEN 80
#define HASHSIZE 23             // some prime val.
#define SHIFTBY 3               // each group size in hashing.

typedef struct node node;
typedef char *type;

struct node {
   int val;
   char *key;
   node *next;
};
node *h[HASHSIZE];


int hGetIndex(char *key) {
   /*
    * returns index into hashtable applying hash function.
    * uses shift-folding followed by mod function for hashing.
    */
   int i, n, finaln=0;
   char *keyptr;
   for(keyptr=key; *keyptr; finaln+=n)
       for(i=0, n=0; i<SHIFTBY && *keyptr; ++i, ++keyptr)
              n = n*10 + *keyptr;
   finaln %= HASHSIZE;

   return finaln;
}

void hInsert( char *key, int val) {
    /*
     * insert s in hashtable h.
     * use shift-folding followed by mod function for hashing.
     * does NOT check for duplicate insertion.
     */
   node *ptr = (node *)malloc(sizeof(node));
   int index = hGetIndex(key);

   ptr->key = strdup(key);
   ptr->val = val;
   ptr->next = h[index];

   h[index] = ptr;
   printf("h[%d] = %s.\n", index, key);
}

int hGetVal( char *key) {
   /*
    * returns val corresponding to key if present in h else −1.
    */
   node *ptr;
    ptr=h[hGetIndex(key)];
   while (1)
   {
   if (ptr==NULL || strcmp(ptr->key, key)==0)
       break;
    ptr=ptr->next;
    }
   if(ptr)
       return ptr->val;
   return -1;
}

void printHash() {
    /*
     * print the hashtable rowwise.
     */
   int i;
   node *ptr;

   for(i=0; i<HASHSIZE; ++i) {
       printf("%d: ", i);
       for(ptr=h[i]; ptr; ptr=ptr->next)
              printf("%s=%d ", ptr->key, ptr->val);
       printf("\n");
   }
}

int main() {
    char s[MAXLEN];
    int i = 0;
    for (i=0;i<HASHSIZE;i++)
        h[i]=NULL;
    i=0;
    printf("Enter the string to be hashed: ");
    gets(s);

    while(*s) {
       hInsert( s, i++);
       printf("Enter the string to be hashed(enter to end): ");
       gets(s);
    }
    printHash();
    printf("Enter the string to be searched: ");
    gets(s);

    while(*s) {
        i=hGetVal(s);
        if (i!=-1)
               printf("%s was inserted at number %d.\n", s,i);
    else
        printf("%s was not inserted in the hashtable",s);
       
       printf("\nEnter the string to be searched(enter to end): ");
       gets(s);
    }

    return 0;
}