### progress but not quite

Hey So I have made progress but I'm not quite there. I give a function a word. This word is converted to upper case. The word is then indexed through. Each position(call it i) is subtracted from 65 then incremented. So 'A' corresponds to 0 when I output. The letters. So I gave the function the word "haha" so for output I get 7 0 7 0. So 7 is H 0 is A. But I don't really want this. I want it to be an array of 26 initialized to 0. So instead of 7 0 7 0 it will just increment position 0 two times. It will increment position 7 two times. So lets pretend we are starring at the array

| 2 | | | | | | | 2 |.....to 26.

Above the first 2 corresponds to A the seventh position corresponds to H.
So really it is just an array that tally's the letters. So I can display the frequency of each occurrence of each letter in the alphabet.

This is my code any ideas are appreciated.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879`` ``````#include #include #include using namespace std; //Function to remove garbage off word void onlyAlpha(string& str1) { string newStr1 =""; newStr1.reserve(str1.size()); for(string::size_type i = 0; i < str1.size(); i++) { if ( isalpha( str1[i] ) ) newStr1.push_back( str1[i] ); } str1 = newStr1; } //A function to determine frequency of letters void fillFrequency(string str1, int freqcount[], const int alphabet) { int position = 0; for(string::size_type i = 0; i < str1.size(); i++) { str1[i] = toupper(str1[i]); freqcount[position++] = str1[i]-65; } } //Function to fill new array with new words. void funfun(string str1[], const int isize, int& countWord) { for(int i = 0; i < isize; i++) { onlyAlpha(str1[i]); countWord++; } } void main() { const int isize = 3; string str1[] = {"!!HELLO","WHAT###", "%%left"}; string test = "haha"; const int size = 100; string wordsminus[size]; int wordCount = 0; const int alphabet = 26; int freqcount[alphabet]; funfun(str1,isize,wordCount); onlyAlpha(test); fillFrequency(test, freqcount, alphabet); for(int i = 0; i < isize; i++) { cout << str1[i]<< "\t"; } cout << wordCount << endl; for(int j = 0; j < alphabet; j++) { cout << freqcount[j] << "\t"; } system("pause"); } ``````
On line 30, instead of filling 'freqcount' linearly, you can fill it depending on what the character of the string is.

 `` `` ``++(freqcount[str1[i] - 'A']);``
I just tried your idea I didn't get anything different in the output. I"m so lost it is this one step that's the problem. How do I get the frequency into the array to increment you know like is there is "B" and "B" the frequency of position 1 will be 2.
Thanks for input though dude.
What I gave you should do the trick.

Here it is, working. http://ideone.com/9NqOpN

Unless this isn't what you want?
Last edited on
No that is exactly what I want for the array. But you have a few things I don't understand.
for instance
` memset(freqcount, 0, 26*sizeof(int));`

what does this mean? Also why does the output have haha? Just curious I appreciate you helping me. I have not learned this memset thing. Is there a way to do it with out it?
never mind I see the test thing for haha now. What about memset though? This is exactly what I have and no dice dude.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677`` ``````#include #include #include using namespace std; //Function to remove garbage off word void onlyAlpha(string& str1) { string newStr1 =""; newStr1.reserve(str1.size()); for(string::size_type i = 0; i < str1.size(); i++) { if ( isalpha( str1[i] ) ) newStr1.push_back( str1[i] ); } str1 = newStr1; } //A function to determine frequency of letters void fillFrequency(string str1, int freqcount[], const int alphabet) { int position = 0; for(string::size_type i = 0; i < str1.size(); i++) { str1[i] = toupper(str1[i]); ++(freqcount[str1[i] - 'A']); } } //Function to fill new array with new words. void funfun(string str1[], const int isize, int& countWord) { for(int i = 0; i < isize; i++) { onlyAlpha(str1[i]); countWord++; } } void main() { const int isize = 3; string str1[] = {"!!HELLO","WHAT###", "%%left"}; string test = "haha"; const int size = 100; string wordsminus[size]; int wordCount = 0; const int alphabet = 26; int freqcount[alphabet]; funfun(str1,isize,wordCount); onlyAlpha(test); fillFrequency(test, freqcount, alphabet); for(int i = 0; i < isize; i++) { cout << str1[i]<< "\t"; } cout << wordCount << endl; for(int j = 0; j < alphabet; j++) { cout << freqcount[j] << "\t"; } system("pause"); } ``````
Last edited on
I used memset to write zeroes to all of the values in the array, but you can just use a for loop to accomplish the same thing. Once you do so, that should fix your problem.

By the way, main should return int always.
Dude I'm telling you my output it not the same it is just 26 numbers like 23312
Why would it not work the same? I don't understand. Yeah that's so funny you mention the int main my teacher uses void main and everyone on here hates it! I don't care about the 0's right now I just want the frequency of each letter to be right. I'm mean it is exactly the same why is it not working ahhh so close so frustrating. Dude thanks
When the os allocates space for 'freqcount,' it inherits whatever garbage is already present in memory. This is a problem now because we are incrementing those garbage values, instead of "setting" them like you were doing before (which is why it wasn't a problem).

Example of what 'freqcount' could look like when created:
[1251, 1321579, 392, 0, 23, 1230, 2, 19384, ...]

What it would look like after calling 'fillFrequency' (without setting all those junk values to zero first)
[1253, 1321579, 392, 0, 23, 1230, 2, 19386, ...]

What it would look like if you set all of the values to zero before calling 'fillFrequency':
[2, 0, 0, 0, 0, 0, 0, 2, ...]

The program is behaving how it should, but the results are skewed because you don't initialize 'freqcount'. In C++ it's important to initialize variables to some value, or else problems like the above can show up.
Last edited on
So are you telling me initialize the array freq count?
My output is this 26 times over. "-858993460"
It doesn't even give me what you just suggested.
I initialized it to 0 worked dude thanks so much :)
Topic archived. No new replies allowed.