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:
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).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; } }
Thanks for the help!