• 04-16-2009
blueshift1980
I'm working on a problem that counts the frequency of letters. I basically have the logic correct (it's from my textbook so I'm going to assume it should run ok. However, when it goes to print out the frequency, the numbers are way too high (in the millions).

Here's the code:

Code:

```#include <iostream> using namespace std; int main(){ char ch; int freqLetter[200]; int LineCounter = 0; for(ch = 'a'; ch<= 'z'; ch++ ){         freqLetter[ch] = 0; // init array         cout<<"Enter some lower case letters (CTRL-Z to end):";         while(cin>>ch){             freqLetter[ch]= freqLetter[ch]+1;             }             cout<<"\n\n\n\t\tLetter Frequency "<<endl<<endl;             for(ch = 'a'; ch <= 'z'; ch++){                 cout<<" "<<ch<<" = "<<freqLetter[ch];                 if(LineCounter >= 5){                     cout<<endl;                     LineCounter = 0;} //end if                 LineCounter++;} // end for             cout<<endl;             return 0;     } }```
I'm assuming the code given is correct in the fact that incrementing a char will procede to the next letter (incrementing a gives b, incrementing b gives c).
Thanks for the help!
• 04-16-2009
CornedBee
Think about the values ch takes in the course of your program. Hint: you're using it for several things at once.
• 04-16-2009
laserlight
You did not initialise all the relevant counts to 0 before accessing the array arbitrarily.
• 04-16-2009
matsp
that's because you should end your first for-loop wraps your entire code, instead of just the zeroing of the freqletter. So you get "rubbish" (or possibly some initialized value to show that you are using uninitialized variables).

Code:

`int freqLetter[200] = { 0 };`
would be a much shorter way to initialize your array.

You probably should also check that your read character is in the range 'a' .. 'z' before accessing the array freqLetter[ch] - if you are unlucky, it will be a negative value (since char can be either signed or unsigned (up to the compiler which it is), and anything that has the topmost bit of a signed char will be treated as negative. So for exampel å would be a negative character in this case - and that is DEFINITELY outside your array!].

--
Mats
• 04-16-2009
blueshift1980
Quote:

Originally Posted by CornedBee
Think about the values ch takes in the course of your program. Hint: you're using it for several things at once.

Isn't the first for loop setting the count of each element to 0? That is what you guys mean correct?
Thanks for the super quick reply!
• 04-16-2009
laserlight
Quote:

Originally Posted by blueshift1980
Isn't the first for loop setting the count of each element to 0? That is what you guys mean correct?

Yes, at least for the elements with indices 'a' to 'z', but the problem is that you do not just access those initialised elements on each iteration of the loop; you may also access those that will only be initialised on a later iteration. This suggests that you need to rethink the structure of what you implemented.
• 04-16-2009
blueshift1980
Quote:

Originally Posted by matsp
that's because you should end your first for-loop wraps your entire code, instead of just the zeroing of the freqletter. So you get "rubbish" (or possibly some initialized value to show that you are using uninitialized variables).

Code:

`int freqLetter[200] = { 0 };`
would be a much shorter way to initialize your array.

You probably should also check that your read character is in the range 'a' .. 'z' before accessing the array freqLetter[ch] - if you are unlucky, it will be a negative value (since char can be either signed or unsigned (up to the compiler which it is), and anything that has the topmost bit of a signed char will be treated as negative. So for exampel å would be a negative character in this case - and that is DEFINITELY outside your array!].

--
Mats

Thanks matsp! I get it now. I knew not to trust what was in the book! Thanks for the quick reply, I will so be back to this forum constantly for help.