The BOTTOM LINE Quote Of The Day

The BOTTOM LINE Quote Of The Day

Don't Ever Tell GOD How BIG Your Problems are.
Just Tell Your Problems How BIG your GOD is ;)

Tuesday, May 8, 2012

Arithmetic Coding



#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 127 
#define SIZE 100 




struct element
{
  char c;
  float pr;
  float rng;
  float UL;
  float LL;     
}el[MAX];


int size;
char msg[SIZE];
float range = 1.00;
float UP;
float DOWN;
float enc;
int str_len;


void make_table()
{
  int i;  
  printf(" Enter the size of elements : "); 
  scanf("%d",&size);
  fflush(stdin);
  for(i=0;i<size;i++) 
  {
    printf("\n #%d Enter character and its probablity (decreasing order) : ",i+1); 
    scanf("%c %f",&el[i].c,&el[i].pr);  
    fflush(stdin); 
    el[i].LL = ((i==0) ? 0.0 :el[i-1].UL);
    el[i].UL = ((i==size-1) ? 1.0 : (el[i].LL + el[i].pr));         
  }
  getch();    
}


void show_table(int num)
{
  printf("\t\tENCODER TABLE\n\tFrequency\tRange\n");   
  for(int i=0;i<size;i++)
  {    
    printf("\n");
    if(i==num)
    { printf("*"); }     
    printf("%c\t%f\t%f-%f",el[i].c,el[i].pr,el[i].LL,el[i].UL);       
  }   
}


int find_char(int num)
{
  int i=0;
  if(num != -1)
  {
   while(el[i].c != msg[num])
   { i++; }
   
  }
  else
  {
    while(!(enc <= el[i].UL && enc >= el[i].LL))
    { i++; }      
  }
  return i;
}


void modify_table(int num)
{
  range = el[num].UL - el[num].LL;
  for(int i=0;i<size;i++)
  {
    el[i].LL = ((i==0) ? el[num].LL : el[i-1].UL) ;     
    el[i].UL = ((i==size-1) ? (el[0].LL+range) : (el[i].LL + (el[i].pr*range)));    
  }  
}


void refresh_table()
{
  for(int i=0;i<size;i++)
  {
    el[i].LL = ((i==0) ? 0.0 :el[i-1].UL);
    el[i].UL = ((i==size-1) ? 1.0 : (el[i].LL + el[i].pr));       
  }   
}
void encode_text()
{
  fflush(stdin);   
  printf("\n Enter the string to be encoded : ");
  gets(msg);   
  fflush(stdin);
  int i=0;
  int j;
  while(msg[i] != '\0')
  {
    printf("\n\n\n Word Checked : %c \n",msg[i]);           
    j = find_char(i);     
    show_table(j);  
    modify_table(j);  
    getch();    
    i++;
  } 
  DOWN = el[j].LL;
  UP = el[j].UL;
  str_len = i;
  printf("\n\n The encoded range : %f-%f for a string of %d characters",DOWN,UP,str_len);
  refresh_table();
}


void decode_text()
{
  int j;   
  printf("\n Enter the encoded value : "); 
  scanf("%f",&enc);    
  fflush(stdin);
  printf("\n\n ... Decoding Text ... \n\n");   
  while(str_len)
  {
    j = find_char(-1);     
    printf("\n\n Word decoded : %c",el[j].c);
    enc = (enc - el[j].LL)/(el[j].UL - el[j].LL);       
    printf("\n New encoded value : %f",enc);
    str_len--;     
  }
}


int main()
{
 int cho;
 do
 {   
 printf("\n\n\n\t MENU \t\n"); 
 printf("\n1. Create Table ");
 printf("\n2. Encode Text "); 
 printf("\n3. Decode Text "); 
 printf("\n\n Choose the option : "); 
 scanf("%d",&cho);
 switch(cho)
 {
   case 1  : make_table();  break;
   case 2  : encode_text();  break;
   case 3  : decode_text();  break;
   default : break;             
 } 
 }while(cho>=1 && cho<=3);
 printf("\n\n\t Thanks for using the Program !! \t\n\n"); 
 return 0;  
}

LZW Encoding



#include<stdio.h>
#include<fstream.h>
#include<string.h>
#include<conio.h>
#include <iostream.h>
#define MAX 128




char dictionary[MAX][10];
int dictionarylen=-1;


bool finddict(char str[])
{
    if(str[1]=='\0' && str[0]<128)
            return 1;
    for(int i=0;i<=dictionarylen;i++)
    {
        if(!strcmp(str,dictionary[i]))
            return 1;
    }
    return 0;
}


void adddict(char str[],char ch)
{
    int i;
    for(int i=0;i<strlen(str);i++)
     cout<<"\n'"<<str[i]<<"'\t'"<<str[i]<<"'("<<(int)str[i]<<")";              
    if(ch == ' ')
     cout<<"\n' '";
    else
     cout<<"\nEOF";       
    dictionarylen++;    
    strcpy(dictionary[dictionarylen],str);
     cout<<"\t\t\t\t'"<<dictionary[dictionarylen]<<"'("<<dictionarylen+128<<")";
}


int findindex(char str[])
{
    if(strlen(str)==1 && str[0]<128)
            return int(str[0]);
    for(int i=0;i<=dictionarylen;i++)
    {
        if(!strcmp(str,dictionary[i]))
            return i+128;
    }
}


int main()
{
    char ch;
    char str[10];
    int i,indexloc=-1;
    cout<<"The Sender Dictionary Has 127 ASCII characters initially\nReading Text from .txt file";     
    FILE *fp;
    fp=fopen("input.txt","r");
    cout<<"\nRead\tTransmitted(Code)\tExtended Dictionary";
    cout<<"\n----------------------------------------------------";
    while(ch!=EOF)
    {
        i=0;       
        while(1)
        {
            ch=fgetc(fp);
            if(ch==' ' || ch==EOF)
               break;
            
            str[i]=ch;
            i++;          
        }
        str[i]='\0';


        if(!finddict(str))
        {
            adddict(str,ch);
        }
        else
        {
            indexloc=findindex(str);
            for(int i=0;i<strlen(str);i++)
             cout<<"\n'"<<str[i]<<"'";              
            if(ch == ' ')
             cout<<"\n' '";
            else
             cout<<"\nEOF";
            cout<<"\t"<<str<<"("<<indexloc<<")";            
        }
    }
    fclose(fp);    
    getch();
    return 0;
    
}

Dynamic Huffman Coding


#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<math.h>
#define max 50 
#define ascmax 130


struct tree
{
  int posn;     
  char ct;
  int frq;     
  int rgt;     
  int lft;       
}tr[ascmax+max];






int size =2;
int epos;




void init_node(char tmp)
{
  tr[0].posn = 1;
  tr[0].ct = '~'; 
  tr[0].lft = 1;
  tr[0].rgt = 2;
  tr[0].frq = 1; 
  
  tr[1].posn = 2*tr[0].posn;
  tr[1].ct = 'E';
  tr[1].frq = 0;
  
  tr[2].posn = 2*tr[0].posn+1;
  tr[2].ct = tmp;
  tr[2].frq = 1;
    
  tr[1].lft = tr[2].lft = -1;
  tr[1].rgt = tr[2].rgt = -1;  


  epos = 1;
  
  printf("\n '%c'\t\t '%c'",tmp,tmp);
  printf("\n Traversing :- ('E',0) ('%c',1)",tmp); 
  printf("\n The Tree is in weight order\n"); 
}






void add_node(char ctc)
{
 char bits[max];
 int bsize=0,ps,j; 

 printf("\n '%c'\t\t '%c' ",ctc,ctc);
 int tp = tr[epos].posn;
 while(tp > 1)
 {
   bits[bsize] = 48+(tp%2);
   tp /= 2;      
   ++bsize;         
 }  
 for(tp=bsize-1;tp>=0;tp--)
  printf("%c",bits[tp]); 

 ++size;
 tr[size] = tr[epos];
 tr[size].posn = 2*tr[epos].posn;
 tr[size].lft = -1;    
 tr[size].rgt = -1;    



 tr[epos].ct = '^';
 tr[epos].frq = 1;    
 tr[epos].lft = size;    
 tr[epos].rgt = size+1;    




 epos = size;

 ++size;

 tr[size].posn = tr[epos].posn+1;
 tr[size].ct = ctc;
 tr[size].frq = 1;
 tr[size].lft = -1;
 tr[size].rgt = -1;  

 ps = (tr[size].posn/2)/2;
  

  
  while(ps >= 1)
  {
   j = 0;
   while(tr[j].posn != ps)
    j++;
   
   tr[j].frq += 1;         
   ps /= 2;           
  }  



}




void update_node(char ctc)
{
  char bits[max];
 int bsize=0;    

  int i=1,j;
  int ps;
  while(ctc != tr[i].ct)
   i++; 
    
  tr[i].frq += 1;
  
  ps = tr[i].posn/2;
  

  
  while(ps >= 1)
  {
   j = 0;
   while(tr[j].posn != ps)
    j++;
   

   tr[j].frq += 1;         
   ps /= 2;           
  }  
  
  printf("\n '%c'\t\t   ",ctc);
 int tp = tr[i].posn;
 while(tp > 1)
 {
   bits[bsize] = 48+(tp%2);
   tp /= 2;      
   ++bsize;         
 }  
 for(tp=bsize-1;tp>=0;tp--)
  printf("%c",bits[tp]); 

}


void reset_node(int nm)
{
 ///printf("\n\n %c/%d(pos=%d)",tr[nm].ct,tr[nm].frq,tr[nm].posn);    
 if(tr[nm].ct == 'E')
  epos = nm;
  
 if(tr[nm].lft != -1)
 {
   //printf("\n Lft Case");            
   tr[tr[nm].lft].posn = 2*tr[nm].posn;
   reset_node(tr[nm].lft);        
 }

 if(tr[nm].rgt != -1 )
 {
   //printf("\n Rgt Case");
   tr[tr[nm].rgt].posn = 2*tr[nm].posn+1;
   reset_node(tr[nm].rgt);      
 }
     
}


void balance_tree()
{
  //printf("\n%d",tr[epos].posn);
  struct tree tn;
  double lvl;
  int i,ilvl,j,ind;
  int optnsize;
  int init; 
  //= tr[epos].posn;
  
  struct outptn
  {
   int psn;
   int frq;      
  }optn[ascmax+max];
  
 do
 {
  optnsize = 0;
  //printf("\n %d %d %c",epos,tr[epos].posn,tr[epos].ct);
  
  lvl = log((double)tr[epos].posn)/log (2);
  ilvl = (int) lvl;
  init = tr[epos].posn;
  
 // printf("\n level=%d ",ilvl);
  while(ilvl>0)
  {
    for(i=init;i<=pow(2,ilvl+1)-1;i++)
    {
      j = 0;
      while(j<=size && tr[j].posn != i)
       j++;    
      if(j<=size)
      {
        optn[optnsize].psn = j;
        optn[optnsize].frq = tr[j].frq;
        ++optnsize;         
      }        
    }    
    --ilvl;     
    init = pow(2,ilvl);          
  }
    
  optn[optnsize].psn = 0;
  optn[optnsize].frq = tr[0].frq;
  ++optnsize;      
  
  ind = -1;
  printf("\n Traversing :-");
  for(i=0;i<optnsize;i++)  
  {
    printf(" ('%c',%d)",tr[optn[i].psn].ct,optn[i].frq);
     if(i < optnsize-1 && optn[i].frq > optn[i+1].frq && ind == -1)
       ind = i;   
  } 
  
  if(ind != -1)
  {
   printf("\n The Tree is not in weight order");     
   
   int ind2 = ind+1;
   
   while(optn[ind2].frq == optn[ind2+1].frq)
    ind2++;
   
   while(optn[ind].frq == optn[ind-1].frq)
    ind--;
    
   printf("\n Swapping ('%c',%d) and ('%c',%d)",tr[optn[ind].psn].ct,optn[ind].frq,tr[optn[ind2].psn].ct,optn[ind2].frq);
   
   int tmp = tr[optn[ind].psn].posn;
     tmp /= 2;
     int k=0;
   
     while(tr[k].posn != tmp)
      k++;
      
   tmp = tr[optn[ind2].psn].posn;
     tmp /= 2;
     int l=0;
     while(tr[l].posn != tmp)
      l++;
     
      
     if(tr[optn[ind].psn].posn%2 == 0)
     {
      tmp = tr[k].lft;                           
      tr[k].lft = optn[ind2].psn;
      if(tr[optn[ind2].psn].posn%2 == 0)
      {
       tr[l].lft = tmp;  
       tr[tr[l].lft].posn = 2*tr[l].posn;
      }
      else
      { 
       tr[l].rgt = tmp;
       tr[tr[l].rgt].posn = 2*tr[l].posn+1;
      }
      tr[tr[k].lft].posn = 2*tr[k].posn;
     }
     
     else
     {
      tmp = tr[k].rgt;                           
      tr[k].rgt = optn[ind2].psn;
      if(tr[optn[ind2].psn].posn%2 == 0)
      {
       tr[l].lft = tmp;  
       tr[tr[l].lft].posn = 2*tr[l].posn;
      }
      else
      { 
       tr[l].rgt = tmp;
       tr[tr[l].rgt].posn = 2*tr[l].posn+1;
      }
      tr[tr[k].rgt].posn = 2*tr[k].posn+1;
     }
     
     

   reset_node(optn[ind].psn);
   reset_node(optn[ind2].psn);     
   
   for(j=size;j>=0;j--) 
   {
     if(tr[j].ct == '~' || tr[j].ct == '^') 
       tr[j].frq = tr[tr[j].lft].frq + tr[tr[j].rgt].frq ;              
   }    
  }
  else
   printf("\n The Tree is in weight order");  
  optnsize = 0;
 }while(ind != -1);
 printf("\n");
}




int main()
{
 char msg[max]; 
 printf("\n Enter the string : "); 
 gets(msg);
 printf("\n Dynamic Huffman Coding \n");
 printf("\n Input\t\t Output");
 printf("\n -----\t\t ------\n");
  int i=1;
 int j; 
 init_node(msg[0]);
 while(msg[i]!='\0')
 {
   j=i;
   do
   {
     --j;  
   }while(j>=0 && msg[j] != msg[i]);     
   if(j>=0)
    update_node(msg[i]);
   else
    add_node(msg[i]);
   balance_tree();
  i++;   
  getch(); 
 }
 getch();
 return 0;   
}

Run Length CODEC


#include<stdio.h>
#include<conio.h>
#include<string.h>
#define max 20


char cstr[max];
char dstr[max];
char str[max];




int main()
{
 int opt;   
 int ret;
 int i,j,k,len;
 int pos;
 int ind;
 do
 {
  printf("\n 1. Compress the file");
  printf("\n 2. DeCompress the file");
  printf("\n 3. Display Compressed file");
  printf("\n 4. Display DeCompressed file");        
  printf("\n\nEnter the option : ");
  scanf("%d",&opt);     
  fflush(stdin); 
  switch(opt)
  {
    case 1: printf("\n Enter string : ");
            gets(str);
            fflush(stdin); 
            len = 0;
            ind = 0;
            i=0;
            do
            {
              pos = i;
              do
              {
                i++;
                ind++;  
              }while(str[pos] == str[i]);
              
              if(ind < 3)
              {
                for(j=0;j<ind;j++) 
                 cstr[len++] = str[i-1];       
              }
              else
              {
                cstr[len++] = '!';
                cstr[len++] = str[i-1];
                cstr[len++] = 48+ind;  
              }              
              ind = 0;            
                                            
            }while(i != strlen(str));
            
            cstr[len] = '\0';
            printf("\n\n\t The Text Data has been Compressed \n");            
            break;
    case 2: len = 0;                   
            i=0;
            do
            {
              if(cstr[i] == '!')
              {
                ind = cstr[i+2]-48;
                for(j=0;j<ind;j++)
                 dstr[len++] = cstr[i+1];
                i += 3;         
              }
              else
              {
                dstr[len++] = cstr[i++];  
              }
                                            
            }while(i != strlen(cstr));
            
            dstr[len] = '\0';
            printf("\n\n\t The Compressed Text Data has been DeCompressed \n");  
            break;
    case 3: printf("\n The Compressed string : ");
            puts(cstr);
            break;
    case 4: printf("\n The DeCompressed string : ");
            puts(dstr);
    default: break; 
         
   }
 }while(opt != 0);   
 getch();   
 return 0;   
}

Saturday, May 5, 2012

Floyd-Warshall Algorithm 2


#include<conio.h>
#include<stdio.h>
#define INFNITY 999
int a[10][10],n;



voidprintArray(){
inti,j;
for(i=0;i<n;i++)
    {
for(j=0;j<n;j++)
        {
printf("%d\t",a[i][j]);
        }
printf("\n");
    }
}
voiduAdjacencyMatrix(){
inti,j,c=1,w;
for(i = 0;i < n; i++)
    {
for(j = 0;j < i; j++)
        {
a[i][j] = INFNITY ;
a[j][i] = INFNITY ;

        }
a[i][i] = 0;   
    }

while(c!=-1)
    {
printf ("\ni , j , w :");           
scanf("%d%d%d",&i,&j,&w);
a[i][j]=w;
     //printf ("c? :");
scanf("%d",&c);
    }
printArray();
getch();
}

int main()
{
int d[10][10], da[10][10],i,j,l,m,k;
printf("enter the number of vertices : ");
scanf("%d",&n);
uAdjacencyMatrix();
for(i=0;i<n;i++)
 {
for(j=0;j<n;j++)
                 {
d[i][j]=a[i][j];
                 }
 }

for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
                 {
for( j=0;j<n;j++)
                     {
if(d[i][j]<d[i][k]+d[k][j])
                                 {
da[i][j]=d[i][j];
                                 }
else
                                 {
da[i][j]=d[i][k]+d[k][j];
                                 }
                     }
                 }
for(l=0;l<n;l++)
                 {
for(m=0;m<n;m++)
                                 {
d[l][m]=da[l][m];
                                 }
                 }
}                
for(l=0;l<n;l++)
                 {
printf("\n");
for(m=0;m<n;m++)
                                 {
printf("%d\t\t",da[l][m]);
                                 }
                 }                
getch();
return(0);
}

Job Sequencing Problem 2


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct jobs
{
                intid,deadl,profit;
}j[10],jf[10],temp;
void sort(int count)
{
                inti,k;
                for(i=0;i<count;i++)
                {
                                for(k=0;k<count-(i+1);k++)
                                {
                                                if(j[k].profit<j[k+1].profit)
                                                {
                                                                temp=j[k];
                                                                j[k]=j[k+1];
                                                                j[k+1]=temp;
                                                }
                                }             
                }
}
intgetpos(int index)
{
                int i=index-1;
                while(i>=0)
                {
                                if(jf[i].id==0)
                                return i;
                                i--;
                }
                return -1;                            
}
int main()
{
                inti,k,count,jfcount=0;
                printf("Count : ");
                scanf("%d",&count);
                fflush(stdin);
                for(i=0;i<count;i++)
                {
                printf("Enter details of %d job : ",i+1);
                scanf("%d %d %d",&j[i].id,&j[i].deadl,&j[i].profit);
                jf[i].id=0;
                jf[i].deadl=0;
                jf[i].profit=0;
               
                }
               
                sort(count);
                for(i=0;i<count;i++)
                {
                                k=getpos(j[i].deadl);
                                if(k!=-1)
                                jf[k]=j[i];
                                jfcount++;
                }
                printf("\nOrder of job sequence is : \n",i+1);
               
                for(i=0;i<jfcount-1;i++)
                {
                                printf("id:%d,deadline:%d,profit:%d\n",jf[i].id,jf[i].deadl,jf[i].profit);
                }
                getch();
return(0);

}

Thursday, May 3, 2012

Longest Common Sub-sequence 2


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
char a[10],b[10];
int count[10][10],dir[10][10];

void backtrack(int i, int j)
{
if(i!=0 && j!=0)
  {
if(dir[i][j] == 3)
    {
backtrack(i-1,j-1);
printf("%c",a[i-1]);        
    }     
else if(dir[i][j] == 2)
backtrack(i-1,j);
else
backtrack(i,j-1);
  }  
}

int main()
{
                int i,j,k,count1,count2;
                printf("\n Enter 1st string : ");
gets(a);
fflush(stdin);
printf("\n Enter 2nd string : ");
gets(b);
                count1 = strlen(a);
    count2 = strlen(b);

                for(i=1;i<=count1;i++)
                count[i][0]=0;
                for(i=1;i<=count2;i++)
                count[0][i]=0;
                for(i=1;i<=count1;i++)
                {
                                for(j=1;j<=count2;j++)
                                {
                                                if(a[i-1]==b[j-1])
                                                {
                                                                count[i][j]=count[i-1][j-1]+1;
                                                                dir[i][j]=3;
                                                }
                                                else if(count[i][j-1]>=count[i-1][j])
                                                {
                                                                count[i][j]=count[i][j-1];
                                                                dir[i][j]=1;
                                                }
                                                else
                                                {
                                                                count[i][j]=count[i-1][j];
                                                                dir[i][j]=2;
                                                }
                                }
                }
                char temp;
                i=0;
                while(i<count2)
                {
                printf("%c\t",b[i]);
                i++;
    }
                printf("\n");
for(i=1;i<=count1;i++)
                {
printf("%c",a[i-1]);
                                for(j=1;j<=count2;j++)
                                {
                                                if(dir[i][j]==1)
                                                temp='-';
                                                else if(dir[i][j]==2)
                                                temp='|';
                                                else if(dir[i][j]==3)
                                                temp='\\';
                                                printf("%c%d\t",temp,count[i][j]);

                                }
                                printf("\n");
                }             
printf("The LCS is : ");
backtrack(count2,count1);          
getch();
return(0);

}