Help with most frequent least frequent letter.

Hi guys so I am writing a program that reads text from a file, then determines which alphabetic character occurs most frequently and which appears least frequently then displays both characters along with the number of occurrences for example output would read:
A is the most common letter with 150 occurrences
B is the least common letter with 0 occurrences.
I am not asking to write the code for me just help me think through my next step. I have got as far as opening the file running my error check,the running through the file character by character and counting everything up. I've displayed my results with a c out. Now I just need to figure how to display most and least only and I should be golden. Except I am also supposed to use one function that uses one or more reference variables. Any help would be appreciated. Thanks
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
  #include <iostream>
#include <iomanip>
#include <fstream> 

using namespace std;

int main()
{
	char letter;
	int total[26] = {0};
	ifstream inFile;                     //open and check for error
	inFile.open("letter_count.txt");
	if (inFile.fail())
	{
		cerr << "Error opening file" << endl;
		exit(1);
	}

	
	while (inFile.get(letter))
	{
		if (isalpha(letter))               //Has to be a letter
		{

			letter = toupper(letter);     //Convert to upper so will read upper and lower
			int i = letter - 'A';         //Keeps track of letter count by subtracting 'A'
			total[i]++;
		}
		
	}

	for (int i = 0; i < 26; i++)           // c out statement to show results
	{
		cout << "  "  << char(i + 'A') << " Appears " << total[i] << " times" << endl;
	}




	return 0;
}




Last edited on
closed account (48T7M4Gy)
Try writing some pseudo-code. Just start by writing down how you would go through the array and get the largest and smallest frequency.

Believe me, by writing the code you have so far you won't have any trouble with this part. It's only a couple of lines. 😀
Last edited on
closed account (37oyvCM9)
maybe think about storing the amount of each character in an array so array[0] = number of A's you have and so on. then use std::sort or write your own sort method to go from smallest item in the array to the highest. then you can reference your array to print out the first element for lowest and the last element for the highest...

just how id go about it :) and im still a newbie
To keep track of the most frequent letter, have two variables,
1
2
char most_frequent_ch;
int most_occurrences = 0;

Then go through each letter, if the occurrence of i'th character is more than most_occurences, then set most_frequent_ch to 'A'+i, and most_occurrence to it's occurrence. At the end of the loop you would've found the answer. Do you see how this logic works ?
Now you could extend this to account for minimum too. By the way, if two or more characters have, say 0 occurrence, what do you print then ?
closed account (37oyvCM9)
good point same for the top as well
OK so I thought I needed to write a sort but didn't know where to go from there, then I read a k n's comment and now I am utterly confused.
Oh and if the frequency of the letter is the same it can print either result
SO so far I have filled and counted the array and need to use reference variables along with functions so like void mostUsed(int total[], int & most); // most contains the index of the most // used letter (4 for E, etc).
void leastUsed(int total[], int & least);
now just need to build these functions, my teacher gave me the above advice
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
int mostFrequent(const int aTable[], const int aTable_size)
{
    int most = 0;
    
    for (int i = 0; i < aTable_size; i++){
        if (aTable[i] > aTable[most]) {
            most = i;
        }
    }
    return most;
}
instead of cont int, I wonder if that function could use a reference variable?
Well that didn't work lol. Also on the function written above are you taking table size, from the array I filed above? How are you defining table size I guess is what I am asking.
or is it just aTable_size is = 26 ? I know i need to take in my array total[] and table_size as arguments to functions mostFrequent and leastFrequent but I am still confused for some reason. Sorry I am feeling extra dumb right now. Thank you for all of your help everybody.
Last edited on
closed account (48T7M4Gy)
You need to do two things:
1. learn/revise arrays and functions - practice with a few simple examples.
2. write some code for this problem and run it.

Good luck. :)
Yah I did that, I understand how arrays work and how functions work. Right now its looking like I am just going to fail, I am way past the fuck it point, thanks anyways
closed account (48T7M4Gy)
So where is your revised code?
Topic archived. No new replies allowed.