Entropy code in C++
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<math.h>
void main()
{
int c;
float p[5];
float H=0.0;
clrscr();
char str[5];
cout<<"Enter text: ";
cin>>str;
for(int i=0;i<strlen(str);i++)
{
cout<<"\n Enter probabilty of "<<str[i];
cin>>p[i];
H+=(-1)*p[i]*(log(p[i])/log(2.0));
}
cout<<"\n Entropy of the given message : "<<H;
getch();
}
#include<conio.h>
#include<string.h>
#include<math.h>
void main()
{
int c;
float p[5];
float H=0.0;
clrscr();
char str[5];
cout<<"Enter text: ";
cin>>str;
for(int i=0;i<strlen(str);i++)
{
cout<<"\n Enter probabilty of "<<str[i];
cin>>p[i];
H+=(-1)*p[i]*(log(p[i])/log(2.0));
}
cout<<"\n Entropy of the given message : "<<H;
getch();
}
Comments
Post a Comment