Thread: Sort indexes of frequency array?

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    1

    Question Sort indexes of frequency array?

    I'm trying to write a program that reads in a text file and then
    calculates the frequency of each of the alphabet characters that
    appears in the file. The characters are read into an array that maps them to their upper-case equivalent in ACSII like this:

    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9950, 1830,
    2606, 5025, 14960, 2698, 2420, 8731, 8511, 110, 1272, 5847, 4253, 8297, 11218, 2016, 220, 7777, 8379, 11863, 4343, 1222, 3132, 179, 3204, 72

    This is the output it produces so far:

    The file "hamlet.txt" has following letter frequencies:
    a: 9950 b: 1830 c: 2606 d: 5025 e: 14960 f: 2698
    g: 2420 h: 8731 i: 8511 j: 110 k: 1272 l: 5847
    m: 4253 n: 8297 o: 11218 p: 2016 q: 220 r: 7777
    s: 8379 t: 11863 u: 4343 v: 1222 w: 3132 x: 179
    y: 3204 z: 72

    What I want to do now is simply print a list of the alphabet
    characters from most frequent to least frequent, e.g.

    e, t, o, a, h, i, s, n, r, l, d, u, m, y, w, f, c, g, p, b, k, v, q,
    x, j, z

    It's easy to print out the single most frequent (or least frequent)
    character but I'm really struggling to think of a way to print them
    all out, from most frequent to least frequent.

    Any help with this problem would be most appreciated.

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

    using namespace std;

    int main()
    {
    string filename;
    cout << "enter name of input file: ";
    cin >> filename;
    ifstream input;
    input.open(filename.c_str());

    if (input.fail() )
    { cout << "could not open file " << filename << endl;
    exit(1);
    }
    string word;
    const int SIZE = 91;
    int lines = 0, words = 0, freq[SIZE] = {0}, len;
    char c;
    while (input >> word)
    { ++words;
    input.get(c);
    if (c == '\n') ++lines;
    len = word.length();
    for (int i = 0; i < len; i++)
    { c = word[i];
    if (c >= 'a' && c <= 'z') c+= 'A' - 'a'; // capitalise c
    if (c >= 'A' && c <= 'Z') ++freq[c]; // count c
    }
    }
    cout << "The input had " << lines << " lines, " << words
    << " words, \nand the following letter frequencies:\n";
    for (int i = 65; i<SIZE; i++)
    { cout << '\t' << char(i) << ": " << freq[i];
    if (i > 0 && i%8 == 0) cout << endl;
    }
    return 0;
    }

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Check this post for a couple of solutions.

    And in the future, please use code tags when you post source ( especially that much of it ). If your not sure what they are, you just type [ code ] at the start and [ /code ] at the end ( but you don't put spaces between the brackets and the words like I have here.

  3. #3
    Unregistered
    Guest
    an interesting way to be absolutely clear about what you are doing is to use structs to handle this problem.

    //declare struct
    struct _letter
    {
    char letter;
    int frequency;
    }

    //declare an array of struct
    _letter letters[26];

    //initialize array elements to capital letters for letters[i].letter and
    //all zeros for frequency
    int i;
    char ch = 'A';
    for(i = 0, i < 26; i++)
    {
    letters[i].letter = ch++;
    letters[i].frequency = 0;
    }

    now read in your file and increment letters[i].frequiency where letters[i].letter = charReadIn.

    When file completely read, then sort file by letters[i].frequency, and print the list of letters in order by printing letters[i].letter.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using unix command line to sort random array
    By lostmyshadow in forum C Programming
    Replies: 4
    Last Post: 12-11-2007, 07:14 PM
  2. New Sort an Array of Classes
    By ajpeters in forum C++ Programming
    Replies: 11
    Last Post: 09-06-2005, 06:23 PM
  3. Sorting
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-10-2003, 05:21 PM
  4. Replies: 2
    Last Post: 05-06-2003, 08:34 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM