Add to Favorites    Make Home Page 2307 Online  
 Language Categories  
 Our Services  

Home » C++ Home » Algorithms Home » Using Shannon's algorithm for code assignment to characters of varying probabilities.

A D V E R T I S E M E N T

Search Projects & Source Codes:

Title Using Shannon's algorithm for code assignment to characters of varying probabilities.
Author Abhishek H. Dwivedi
Author Email yarrows [at] indiatimes.com
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.
Category C++ » Algorithms
Hits 366426
Code Select and Copy the Code
#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 "); 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 ! "); } 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(" "); part(1,n); printf("The codes are "); for(i=0;i<n;i++) { for(j=0;j<10;j++) printf("%c ",c[i][j]); printf(" "); } 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++; } }

Related Source Codes

Script Name Author
Moving ball screen saver karlmarx
The Classic Game of Snake & Ladder Lakshmi Narayana .A
Railway seat reservation question which comes in sapient VyomWorld
To calculate percentile Ravi Mathur
Send to folder ANIMESH SAHU
Analog clock and calendar Nazia & Rida
HIGH/LOW GAME MOLLY ARORA
Data structure (stack Implimentation) Swapnil B Adsure
Memory Game AnirudhSanyal
Easy Calc Anirudh Sanyal
GK Quiz Anirudh Sanyal
Hangman Game Manish Jain
Snakeman Manish Jain
Full month Calendar Nigi
Cursor shapes nigi

A D V E R T I S E M E N T




Google Groups Subscribe to SourceCodesWorld - Techies Talk
Email:

Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions | Important Websites
Copyright ©2003-2024 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=730


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review