Hi, I am new to c++. I am writing a program that reads in a text file and gives a count of how many times each letter appeared in the file. I got it to read the text file and do the letter count. But my sort seems to be incorrect. It is telling me that
X = 102
Y = 126
Z = 165
etc...
THAT IS WORNG
The sample output should be
E = 165
T = 126
A = 102
O = 93
etc...
I got it to sort from lowest to highest for the frequency, but cant seem to get the appropriate letter assigned to it. Does anybody know what's going on?
void SortArray(int iArray[], int numElements);
void PrintArray(int total[], int i);
int main()
{
int total[26] = {0};
ifstream infile; //Declares variable to associate with file
string filename = "gettysburg.txt";
int index;
char c;
infile.open(filename); //Opens file
if (!infile.fail()) //Indicates if something went wrong while attempting to utilize
{ //file
while(!infile.eof()) //Indicates that end of file has been reached during input
{
while(infile.get(c)) //Reads each character one at a time
{
cout.put(c); //Prints each character to screen
if (isalpha(c)) //Checks for both upper and lower case Alphabetical characters
{
c = toupper (c); //Converts all alphabetical charactters to upper case
index = c - 'A'; //ASCII code for all uppercase characters is 65 - 90.
//This subtraccts 'A' to bring index to 0 thorugh 25.
total[index]++; //Total's index
}
}
}
cout << endl;
cout << endl;
cout << "Letter Frequency Table:" << endl;
cout << endl;
//Add here for swap
int temp;
temp = iArray[curIndex];
iArray[curIndex] = iArray[smallestIndex];
iArray[smallestIndex] = temp;
}
}
Put the code you need help with here.
void SortArray(int iArray[], int numElements) //Sorts Array
{
for(int curIndex = 0; curIndex <= numElements - 2; curIndex++)
{
int smallest = iArray[curIndex];
int smallestIndex = curIndex;
Please look up how to use code tags when you post in this forum, otherwise the formatting of your code is lost.
Do you need to use this particular definition of SortArray? It is useless for this problem. You need to deal with two arrays. I would modify it so to return the indices of the original array. For example, if input is {21,10,12,4} you should return {3,1,2,0}. That way you can keep track of the letter.
if I use 2 arrays, then I would have to change my entire program wouldn't I? I just use this definition of SortArray because I had used it previously. If there is another way I could do the SortArray, that would be helpful.
I don't see how you can use your sort array. Like I said, you have a pair of arrays (key,value) that you want to sort by value. If you just sort the value array, without keeping track of the keys, you will not get any meaningful result
I just use this definition of SortArray because I had used it previously. If there is another way I could do the SortArray, that would be helpful.
Your sorting function is not sufficiently generic to reuse for this particular problem (although you could reuse the logic.)
There are a number of ways you can approach this problem. You could use an array of objects that contain a count and the letter each element represents, and sort that array. You could use a second array of indices into total and sort the indices based on the element they represent in total to get the order you want. You could use an associative container like std::map with the count as the key and the letter as the data which you could initialize from the array after you've finished filling it.
One thing is certain, though. Your current approach cannot work. The letter is associated with a particular position in the array total. Sorting total does not change which letter is associated with which position; all it does is move the totals around.