Letter Frequency count

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?

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

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;

infile.close(); //Closes file

//Prints sorted array
SortArray(total, 26);
PrintArray(total, 0);
cout << endl;
}
else
{
cout << "Failed to open file '" << filename << "'!" << endl;
}

return 0;
}

void PrintArray(int total[], int i) //Prints Array
{
for(i = 0; i < 26; i++)
{
cout << char(i + 'A') << " = " << total[i] << endl;
}
cout << endl;
}

void SortArray(int iArray[], int numElements) //Sorts Array
{
for(int curIndex = 0; curIndex <= numElements - 2; curIndex++)
{
int smallest = iArray[curIndex];
int smallestIndex = curIndex;

for(int checkIndex = curIndex + 1; checkIndex < numElements; checkIndex++)
{
if(iArray[checkIndex] < smallest)
{
smallest = iArray[checkIndex];
smallestIndex = checkIndex;
}
}

//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;

for(int checkIndex = curIndex + 1; checkIndex < numElements; checkIndex++)
{
if(iArray[checkIndex] < smallest)
{
smallest = iArray[checkIndex];
smallestIndex = checkIndex;
}
}

//Add here for swap
int temp;
temp = iArray[curIndex];
iArray[curIndex] = iArray[smallestIndex];
iArray[smallestIndex] = temp;
}
}
Hi!

At first, you'd better format your code with [ code ] tags (see button at the right). It is almost unreadable without it.

You of course could not preserve letters if you only have one array of their counters. Most straightforward idea is to store not simple numbers, but structures (count, letter) in array.

Other variant is to store two arrays - one with counters and other with letters. The letter array should be initialized with A, B, ..., X, Y, Z - and when you do sorting, you need to always swap elements of both arrays. I.e. if you swap 1-st and 6-th element of the counters array, you should also swap 1-st and 6-th element of letters array.

Here is the simplified version of this task which you can use for practice/testing:
http://codeabbey.com/index/task_view/sort-with-indexes
Topic archived. No new replies allowed.