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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include<iomanip>
#include <string>
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.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include<iomanip>
#include <string>
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.