123 Eng

Engineering the engineers™


Latest Jobs   Forum Map

 


Home
Source Codes
Engineering Colleges

Training  Reports
Seminar Reports
Placement Papers

Forums

   Computer Science / IT
   Electronics
   Electrical
   Mechanical
   Chemical
   Civil

   CAT / MBA

   GMAT / Foreign MBA
Latest Jobs

Engineering Jobs / Technical Jobs
Management Jobs

Sitemap
Terms of use

Displaying  Source Code(s)  
 

 
Using Shannon's algorithm for code assignment to characters of varying probabilities.

--------------------------------------------------------------------------------

Description : This code gives the binary codes to each of the characters having different probabilities of occurence,using the Shannon's algorithm. User must enter the probabilities .Make sure that the sum of all probablities is <= to 1. The character with the highest probability of occurence will be assigned the code of least length, while that of the lowest probability will be assigned the longest length. Before the codes are assigned, the characters are sorted in descending order. Hence, the results displayed also are in descending order.

#include<stdio.h>
#include<conio.h>
#include<math.h>
int temp,i,j,col=0,n=100,count=0,b[10][2];
float a[10],sum=100;
char c[10][10];
void main()
{
void part(int,int);
clrscr();
printf("Enter number of characters ");
while(n>10)
{
printf("(Not more than 10) ");
scanf("%d",&n);
}
while(sum>1)
{
printf("Enter their probabilities of occurences<BR>);
sum=0;
for(i=1;i<=n;i++)
{
printf(".");
scanf("%f",&a[i]);
while(a[i]>=1)
a[i]=a[i]/10;
sum=sum+a[i];
if(sum>1)
break;
}
if(sum>1)
printf("Sum of probabilities must be less than or equal to 1 !<BR>);
}
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
c[i][j]='*';
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[j]<a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}}}
printf("
<BR>);
part(1,n);
printf("The codes are <BR>);
for(i=0;i<n;i++)
{
for(j=0;j<10;j++)
printf("%c ",c[i][j]);
printf("<BR>);
}
getch();
}
void part(int low,int high)
{
int min=100,k,z,sum=0,dif,sum1=0,l=0;
z=(high-low);
z=abs(z);
for(j=0;j<10;j++)
for(i=0;i<2;i++)
b[j][i]=0;
if(z>1)
{
count++;
for(i=low-1;i<high;i++)
sum=sum+a[i];
sum1=a[low-1];
sum=sum-a[low-1];
for(i=low;i<=high;i++)
{
dif=sum-sum1;
sum1=a[i]+sum1;
sum=sum-a[i];
b[l][0]=abs(dif);
b[l][1]=i;
l++;
}
for(i=0;i<l;i++)
if(min>b[i][0])
{
min=b[i][0];
k=b[i][1];
}
for(i=low-1;i<k;i++)
c[i][col]='0';
for(i=k;i<high;i++)
c[i][col]='1';
col++;
part(low,k);
part(k+1,high);
}
else
{
c[low][col]='1';
c[high][col]='0';
if(c[high+1][col]=='*'&&high<=9)
while(c[high+1][col]=='*')
col--;
col++;
}
}

 

 

Contribute content or training reports / feedback / Comments
job placement papers
All rights reserved © copyright 123ENG