Thread: frequency distribution of a charactes in a file

  1. #1
    Unregistered
    Guest

    frequency distribution of a charactes in a file

    Dear experienced programmers,

    I was working on program that counts the frequency distribution of a characters in a file. the algorithm is as follows :

    1.open a file

    2.read the first character and put it in an array that counts the single character called singles[]

    3. read the second charcter and put it in array that counts the double called doubles[].

    4. then read the rest of the file and update the relevant array

    5. finally display the statistic of singles,doubles and triples.

    here is my problem when I run the program
    it only displays the singles result, but the program doesn't execute the second character loop and the loop that does the rest of the charcter read(3rd while loop).

    I am loosing the head why it doesn't excute that
    portion of the code.

    the array singles[] counts characaters that are single
    example, if the program reads the letter "a" once at program display

    the array doubles [] keeps track the characters read twice from the file. and the array triples keeps track characters read three times.


    I HUMBLY NEED A GUIDE SO I CAN SOLVE THE PROBLEM.

    THANKS IN ADVANCE

    #include<iostream.h>
    #include <fstream.h> /* for file functions */
    #include <stdio.h>
    #include <ctype.h> /* for exit() */
    #include <string.h>

    #define MAX 26
    #define Def_percent 0.005 /* starting percentage */

    static int singles[ MAX ];
    static int doubles[ MAX ][ MAX ];
    static int triples[ MAX ][ MAX ][ MAX ];


    int main(int argc, char *argv[])
    {
    char ch;
    int first, last, next;
    ifstream infile;
    int letterCount;
    int i , j, k;
    double percentage;
    double lowerpercent = Def_percent;

    if (argc != 2)
    {
    cerr<<"\noops!! no file name specified " <<endl;
    exit(-1);
    }

    infile.open( argv[1] );
    if ( !infile )
    {
    cerr<<"\n cann't open " << argv[1] <<endl ;
    exit(-1);
    }

    /* read first letter */

    while( infile )
    {


    infile.get(ch);
    first = tolower(ch);

    if (( first >= int ('a') ) && ( first <= int ('z')))
    {

    ++singles[ first -= int ('a')];


    }

    }


    /* process the second chracter */

    while ( infile )
    {


    infile.get(ch);
    last = tolower(ch);
    if (( last >= int ('a') ) && ( last <= int ('z')))
    {

    ++doubles[first][last -= int ('a')];
    ++singles[last];


    }

    }

    /* process the rest */
    letterCount = 2; /* number of characters counted characters so far */
    while ( infile.get(ch) != 0 )
    {
    //infile.get(ch);
    next = tolower(ch);
    if (( next < int ('a') ) || ( next > int ('z')))
    {

    continue;
    /* process the current character */

    ++singles[next -= int ('a')];
    ++doubles[last][next];
    ++triples[first][last][next];
    first = last;
    last = next;
    ++letterCount;

    }

    }

    /* Print the statistics */
    cout << " the file contains" << letterCount <<" letters";
    cout <<"\n\n the singles:\n" << endl ;
    cout << " -------------------------------------------------------"<<endl;

    for( i = 0 ; i < MAX ; i++)
    {
    cout <<char( i + int ('a')) <<" "<< double(singles[i]/letterCount) << "\t"
    << singles[i] << endl;



    cout <<"\nDOUBLE LETTERS: \n"<< endl;
    cout <<"----------------------------------------------------------"<< endl;
    for ( i = 0; i < MAX; i++)
    for( j = 0; j < MAX; j++)

    if ( (percentage = double (doubles[i][j]) / (letterCount - 1)) >= lowerpercent )

    cout << char(i + int ('a')) <<" "<< char (j + int ('a')) <<" "<< percentage << "\t"
    << doubles[i][j]<< endl;


    cout <<"\n TRIPLE LETTERS: \n"<< endl;
    cout <<"------------------------------------------------------------------"<< endl;
    for ( i = 0; i < MAX; i++)
    for( j = 0; j < MAX; j++)
    for ( k = 0; k < MAX; k++)

    if( (percentage = double (triples[i][j][k]) / (letterCount -2)) >= lowerpercent)

    cout<< char(i + int ('a')) <<" "<< char (j + int ('a')) <<" "<< char (k + int ('a')) << "\t"
    <<percentage << triples[i][j][k] << endl;







    }



    }

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    please use [ code ] [ /code ] tags next time.

    I didn't look at your code except the beginning but if I understand the problem correctly (tell me if I'm wrong) but you need to read a file and count the number of times the letters appear in that file. If that is the case why don't you just use one integer array (one element per letter) and just increment the element that corresponds to the letter and then parse the array and print out the info that way. I just don't see how you can use a 2d & 3d array for counting letter.
    "The most common form of insanity is a combination of disordered passions and disordered intellect with gradations and variations almost infinite."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM