I am having a difficult time trying to display frequency of letter used in asced
ID: 3912529 • Letter: I
Question
I am having a difficult time trying to display frequency of letter used in asceding order. I have firgured out how to display the values alone in ascending order but I cannot correctly associate them with the letter that frequency value belongs to. Below shows my code (you can follow the code easily by looking at the number line it is next to) and copy and pasted version of the prompted data that is displayed to my .txt file. Can someone please share with me a bit what bit of c++ code to add inorder to achieve this goal.
Statistics of words file word3.txt
number of words: 113809
number of letters: 902905
Frequency of Letters:
a: 68582
b: 17798
c: 34287
d: 34552
e: 106758
f: 12714
g: 27848
h: 20200
i: 77412
j: 1780
k: 9370
l: 47011
m: 24741
n: 60513
o: 54542
p: 25789
q: 1632
r: 64965
s: 86547
t: 57059
u: 31161
v: 9186
w: 8535
x: 2700
y: 13473
z: 3750
Frequency of Letters asceding order:
1632
1780
2700
3750
8535
9186
9370
12714
13473
17798
20200
24741
25789
27848
31161
34287
34552
47011
54542
57059
60513
64965
68582
77412
86547
106758
1632
Frequency of words with respect to word length:
Number of words with word length of 0 Letters: 0
Number of words with word length of 1 Letters: 0
Number of words with word length of 2 Letters: 85
Number of words with word length of 3 Letters: 908
Number of words with word length of 4 Letters: 3686
Number of words with word length of 5 Letters: 8258
Number of words with word length of 6 Letters: 14374
Number of words with word length of 7 Letters: 21727
Number of words with word length of 8 Letters: 26447
Number of words with word length of 9 Letters: 16658
Number of words with word length of 10 Letters: 9199
Number of words with word length of 11 Letters: 5296
Number of words with word length of 12 Letters: 3166
Number of words with word length of 13 Letters: 1960
number of letter words above 13 characters:
Explanation / Answer
I would suggest you have a struct containing the letter and its frequency like this
struct LetterFrequency
{
char letter;
int frequency;
};
In main(), create array of this struct rather than the ints to keep track of the frequency. Don't forget to initialize the array elements like this
LetterFrequency letterFrequencies[26];
char ch = 'a';
for(int i = 0; i < 26; i++, ch++)
{
letterFrequencies[i].letter = ch;
letterFrequencies[i].frequency = 0;
}
Now when you read the file, update the frequency at the correct index position like this
if(isalpha(c))
{
letterFrequencies[tolower(c) - 'a'].frequency++;
}
Now in your sort code, sort the array of structs rather than array of ints. So your temp variable used while swapping has to be declared as
LetterFrequency temp; and the comparison would be
if(letterFrequencies[j].frequency > letterFrequencies[j+1].frequency)
{
//.....do the swapping
}
If you have not learnt structs, the alternate is to use 2 parallel arrays, one for letter name, and other is its frequency... In this case, when you sort.... you will have to swap both the letter and its frequency together to keep them related.
Hope it helped.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.