#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
void sort(float sym[20],int no);
void group(float a[],int low,int high);
char str[20][20]; //global variables
void main()
{
float sym[10],sum,info,entro=0.000,avg=0.0,eff,redu;
int i,no,j,s[20],low,high;
clrscr();
for(i=0;i<no;i++)
{
sym[i]=0.0;
}
printf("\n\t Enter the number of symbols:-");
scanf("%d",&no);
do
{
for(i=0;i<no;i++)
{
printf("\n\n\t Enter the prob of a[%d]=",i);
scanf("%f",&sym[i]);
}
sum=0;
for(i=0;i<no;i++)
{
sum=sum+sym[i] ;
}
printf("sum:-%f" , sum) ;
i f (sum!=1.00)
printf("\n As the sum of probabilities is not equal to one please reenter the
probabilities") ;
}while(sum!=1) ;
sort(sym,no) ;
low=0;
high=no-1;
group(sym,low,high);
for(i=0;i<=no-1; i++)
{
printf("\n\n %f", sym[i]) ;
printf("\t %s", str [i]) ;
s[i]=strlen(str[i]);
printf("\t code l ength %d", s [i]) ;
info=((-(log(sym[i])))/log(2));
printf("\tinformation of x[%d]=%f",i+1,info) ;
entro=entro+(sym[i]*info);
avg+=((sym[i])*s[i]);
}
eff=((entro/avg)*100);
redu=100-eff;
printf("\n\n\t value of average length is given by %f", avg);
printf("\n\tentropy(H)=%f", entro) ;
printf("\n\t Efficiency=%f", eff) ;
printf("\n\t Redundancy=%f" , redu) ;
getch();
}
void sort(float sym[],int no)
{
int 1,j;
float temp1;
for(j=1;j<=no-1;j++)
{
for(i=0;i<=no-j-1;i++)
{
if(sym[i]<sym[i+1])
{
temp1=sym[i] ;
sym[i]=sym[i+1] ;
sym[i+1]=temp1;
}
}
}
printf("\n\t Elements in descending order are ") ;
for(i=0;i<no;i++)
{
printf("\n\t prob of x[%d]=%f", i, sym[i]) ;
}
}
void group(float sym[],int low,int high)
{
float sum2,half,no,newdiff,diff;
int pos,i,j;
if(low<high)
{
sum2=0 ;
for(i=low;i<=high;i++)
sum2=sum2+sym[i ] ;
half=sum2/2 ;
sum2=0 ;
sum2=sum2+sym[low];
diff=half-sum2;
if(diff<0)
diff=(-1)*diff;
for(i=low+1;i<=high;i++)
{
sum2=sum2+sym[i ] ;
newdiff=half-sum2;
if(newdiff<0)
newdiff=(-1)*newdiff;
if(newdiff<diff)
diff=newdiff;
else
break;
}
pos=i -1;
for (j=l ow ; j<=pos ; j++)
strcat(str[j],"0");
for(j=pos+1;j<=high;j++)
strcat(str [j], "1") ;
group(sym,low,pos) ;
group(sym, pos+1, high) ;
}
}
Output:
Enter the number of symbols:-7
Enter the prob of a[0]=0.4
Enter the prob of a[1]=0.1
Enter the prob of a[2]=0.1
Enter the prob of a[3]=0.1
Enter the prob of a[4]=0.08
Enter the prob of a[5]=0.08
Enter the prob of a[6]=0.04
sum:-0.900000
As the sum of probabilities is not equal to one please re-enter the probabilities
Enter the prob of a[0]=0.4
Enter the prob of a[1]=0.2
Enter the prob of a[2]=0.1
Enter the prob of a[3]=0.1
Enter the prob of a[4]=0.08
Enter the prob of a[5]=0.08
Enter the prob of a[6]=0.04
sum:-1.000000
Elements in descending order are
prob of x[0]=0.400000
prob of x[1]=0.200000
prob of x[2]=0.100000
prob of x[3]=0.100000
prob of x[4]=0.080000
prob of x[5]=0.080000
prob of x[6]=0.040000
0.400000 0 code length 1 information of x[1]=1.321928
0.200000 100 code length 3 information of x[2]=2.321928
0.100000 101 code length 3 information of x[3]=3.321928
0.100000 1100 code length 4 information of x[4]=3.321928
0.080000 1101 code length 4 information of x[5]=3.643856
0.080000 1110 code length 4 information of x[6]=3.643856
0.040000 1111 code length 4 information of x[7]=4.643856
value of average length is given by 2.500000
entropy(H)=2.426314
Efficiency=97.052551
No comments:
Post a Comment