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
#include <iostream>
#include <iomanip>
#include <fstream>
usingnamespace 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;
}
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...
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 ?
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
int mostFrequent(constint aTable[], constint aTable_size)
{
int most = 0;
for (int i = 0; i < aTable_size; i++){
if (aTable[i] > aTable[most]) {
most = i;
}
}
return most;
}
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.
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