Function calculating the probability of a letter in an sentence

Posted by Mike on Stack Overflow See other posts from Stack Overflow or by Mike
Published on 2010-05-05T22:35:21Z Indexed on 2010/05/05 22:38 UTC
Read the original article Hit count: 193

Filed under:
|
|
|

I have a function that is supposed to calculate the number of times a letter occurs in a sentence, and based on that, calculate the probability of it occurring in the sentence. To accomplish this, I have a sentence:

The Washington Metropolitan Area is the most educated and affluent metropolitan area in the United States.

An array of structures, containing the letter, the number of times it occurs, and the probability of it occurring, with one structure for each letter character and an additional structure for punctuation and spaces:

struct letters
{
  char letter;
  int occur;
  double prob;
}box[53];

This is the function itself:

void probability(letters box[53], int sum
{
     cout<<sum<<endl<<endl;
     for(int c8=0;c8<26;c8++)
     {      
       box[c8].prob = (box[c8].occur/sum);
       cout<<box[c8].letter<<endl;
       cout<<box[c8].occur<<endl;
       cout<<box[c8].prob<<endl<<endl;
     }
}

It correctly identifies that there are 90 letters in the sentence in the first line, prints out the uppercase letter as per the structure in the second line of the for loop, and prints out the number of times it occurs. It continually prints 0 for the probability. What am I doing wrong?

© Stack Overflow or respective owner

Related posts about c++

Related posts about probability