Reputation: 97
i'm trying to make a Shannon code maker from input text, and i have some troubles...
so, there is some simple shit-code
int main()
{
string x="this is very big text.";
int temp;
int N = x.length();
int *mass = new int [N];
then counting symbols in text;
then counting symbols that used from ASCII table;
creating new 2 massives with symbols and counters of symbol, but their dimention is quite smaller;
delete old char massive by delete mass;
sort 'em by counters and counting their cumulative probability;
double * cumulative = new double (k);
double temp=int_mass[0];
cumulative[0]=0;
for (int i=1; i<k; i++)
{
temp=int_mass[i-1];
cumulative[i]=cumulative[i-1]+temp/N;
}
cout'ing all 3 massives
double a,b,n;
n=N;
for (int i=0; i<k; i++)
{
b=int_mass[i];
b/=n;
cout<<char_mass[i]<<" ";
cout<<b<<" "; //**__**__**
cout<<cumulative[i]<<endl;
}
so, i have some troubles. if text is small, then i catch unhandled exception at praogram's termination. if text is big, about 100+ symbols, i have exception at __**__.
do you have any suggestions, why it happens?
sorry for big code, it my first commit on StackOverFlow.
Upvotes: 0
Views: 532
Reputation: 56479
Use []
instead of ()
to new
arrays:
double * cumulative = new double [k];
^ ^
(k)
just makes a single place in memory and initializes it to k
instead of making an array with size k
.
Use []
for deleting arrays:
delete [] mass;
^^
You're using k
, but I can't see where you initialized it ?!
for (int i=0; i<k; i++)
It's better to use std::vector
instead of self defined arrays to avoid above problems.
Upvotes: 5