Thread: about program that counts a character frequency

  1. #1
    Unregistered
    Guest

    Unhappy about program that counts a character frequency

    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.

    I AM NOT EXPECTING SOMEONE TO DO IT FOR ME BUT
    DESREVE TO BE GUIDED SLOWLY TO SOLVE THE PROBLEM.

    THANKS IN ADVANCE

  2. #2
    Registered User DeadArchDown's Avatar
    Join Date
    Apr 2002
    Posts
    28
    No way to help you out without seeing some code first. Also, if your just computing statistics of the file, it's not necessary to read the characters into an array; in fact, your just wasting space. Read them one at a time into a char variable and then update your stats on each read.

    After re-reading your post, I'm still a little unclear on what your program does.

    "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[]. "

    What is the double it's counting? whats the triple?

    Def. going to need to see some code.

    I AM NOT EXPECTING SOMEONE TO DO IT FOR ME BUT
    DESREVE TO BE GUIDED SLOWLY TO SOLVE THE PROBLEM.
    You don't "desreve" anything. People are here to help, but they don't owe you anything. You are the one asking for help. I would say if anyone deserves anything, it's the people who will be helping you with this problem. These people deserve a courtous post, that's detailed, to the point, and only asked after you have tried to solve the problem yourself. If you think about your post before you submit it, you'll do away with these- "I need more info", "ok, here it is", "I also need this info"-sort of threads. Think of your problem, clearly state it, post any relevant code (using the [code] tags), and state how you have gone about trying to solve the problem. This puts you in the best posistion to get a good, well thought out reply, which I'm sure is what you would prefer.
    Last edited by DeadArchDown; 04-22-2002 at 09:07 AM.
    -------------------
    "Exception"

  3. #3
    Unregistered
    Guest

    Unhappy about frequency distribution program

    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.

    I AM NOT EXPECTING SOMEONE TO DO IT FOR ME BUT
    DESREVE TO BE GUIDED SLOWLY TO 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;







    }



    }

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    I have merged your two threads. Please try to use the Reply option rather than starting a new thread when you want to continue with the same subject.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Registered User DeadArchDown's Avatar
    Join Date
    Apr 2002
    Posts
    28
    You still have me confused with your triples and doubles...
    But, what your doing is using a while loop to read through the entire file. At this point, your 'get' cursor is at the end of the file. You then try to read through thr file again with another while loop...to do this you will need to reset the 'get' cursor. I think the member function you'll need is seekp().
    -------------------
    "Exception"

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    206

    Angry DeadArchDown

    You want to know who deserves nothing? you. how long have you been in this forum now? 1, 2 days? first off you have no right to have any opinion towards people you dont know on a board you dont know. maybe he has done something for us? Do you know? did you make an effort to find out? I dont think so. everyone deserves some help. so you can stfu and apologize to him or just leave the forum. yeah i know i am not a senority either but ive been here at least 6 months longer than u so i have a right to tell u that u are wrong

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Denethor2000: You really need to start drinking decaf.
    DeadArchDown: Please at least show some of that courtesy that you were talking about.
    Unregistered: We're here to help you, but we don't have to. The way you stated your post implied otherwise, please choose your words more carefully in the future.

    >I AM NOT EXPECTING SOMEONE TO DO IT FOR ME BUT
    >DESREVE TO BE GUIDED SLOWLY TO SOLVE THE PROBLEM.
    Sentences in all caps are very annoying, but to help you solve your problem I'll offer an algorithm and code. You can ignore the code if you wish, but I've found that one can learn more quickly from a good example than an explanation.

    To check the frequency of one two and three letter words, read one word at a time and check the length of that word. If the length is one, two, or three then you can proceed to check the word for invalid characters. If the word contains invalid characters you do nothing and read another word, otherwise increment a simple counter. At the end of the program print the values of the three counters, one for each word size. The frequency from this point can easily be calculated.
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    bool goodStr ( std::string& s )
    {
      for ( int i = 0; i < s.length(); i++ )
        if ( s[i] >= 'a' && 'z' >= s[i] || s[i] >= 'A' && 'Z' >= s[i] )
          continue;
        else
          return false;
      return true;
    }
    
    int main ( void )
    {
      std::string s;
      int singles = 0, doubles = 0, triples = 0;
      std::ifstream IN ( "test.txt" );
      while ( IN.good() ) {
        IN>>s;
        switch ( s.length() )
        {
        case 1: if ( goodStr ( s ) ) ++singles; break;
        case 2: if ( goodStr ( s ) ) ++doubles; break;
        case 3: if ( goodStr ( s ) ) ++triples; break;
        }
      } 
      std::cout<<"Singles = "<< singles <<"\nDoubles = "
               << doubles <<"\nTriples = "<< triples <<"\n";
      IN.close();
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  8. #8
    Registered User DeadArchDown's Avatar
    Join Date
    Apr 2002
    Posts
    28
    Wo! You people sure jump off the wagon quickly. What I said has nothing to do with the amount of time I have been here...it is common board courtesy anywhere. And I was in no way rude or disrespectful...I let him know that he didn't deserve anything...as he does not. I then pointed out the optimal way to get a good response. Furthermore, I went on to try and help him. Prelude...from the post I've read so far, you seem to be seldom wrong and pretty level headed...in this case though, I beleive your wrong. I was not out of line in any form, and showed all the courtesy I would have liked to receive myself...which I have not received since Ive been here.

    Also, I have surfed this board for months now. I just never decided to register. I read mostly and did a little posting. I decided that I would join and try to answer some of the more basic posts as the more experianced programers tend to ignore those. However, don't worry...with this reception, I wont be around long...and you can go back to flaming each other and not me.
    Last edited by DeadArchDown; 04-22-2002 at 11:46 AM.
    -------------------
    "Exception"

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I was not out of line in any form, and showed all the courtesy I would have liked to receive myself...which I have not received since Ive been here.
    This I understand, however, I interpreted your post as more condescending than helpful. In my opinion your response was inappropriate for what was obviously a bad choice of words by the OP and nothing more. If the OP erred in such a way that deserved a harsh response then so be it, otherwise it's better to accept that mistakes will be made and respect that.

    However, some mistakes can be avoided by simply following directions in the stickies or reading the FAQs and previous posts. These are the mistakes that we respond to in a less than civil manner because they shouldn't have been made in the first place.

    On these boards it is very important to choose your words carefully, lest they be misinterpreted. Denethor's very inappropriate response is a good example of this.

    >However, don't worry...with this reception, I wont be around
    >long...and you can go back to flaming each other and not me.
    This is your choice, but please keep in mind that this is a rough and tumble place. We don't make any effort at all to make people feel good, it's all about the flow of information. Very rarely will you get fanfare and good will in your first few posts, quite a few of the members of these boards feel that you have to earn courtesy from them.

    It's harder to create a well written post that doesn't offend anyone and many members will join, post a few times, and then never come back so most of the time it really isn't worth the effort. We've found that we can help more people by forgoing with endless courtesies.

    -Prelude
    My best code is written with the delete key.

  10. #10
    Registered User DeadArchDown's Avatar
    Join Date
    Apr 2002
    Posts
    28

    It's harder to create a well written post that doesn't offend anyone and many members will join, post a few times, and then never come back so most of the time it really isn't worth the effort. We've found that we can help more people by forgoing with endless courtesies.

    -Prelude [/B]
    Of course...and as far as I know the average poster is not looking for courtesies...what he is looking for is not to be unfairley harassed...a very simple, legitmate desire.

    What that poster got was a bit condescending. Sometimes this can't be avoided. He said he deserved a response a certain way, and did so in caps, commonly understood to be shouting. Mabye he did all this innocently...if so, I give him the same response. It is a fair one. I didn't surgarcoat my answer...Ill direct you to your own post as to why not...I informed him how the give and take of information like this occurs...especially when he was currently the taker.

    I personally consider the announcement that you deserve an answer WRITTEN IN CAPS to be a mistake that shouldn't be made in the first place. I was still very civil in my response though
    Last edited by DeadArchDown; 04-22-2002 at 12:52 PM.
    -------------------
    "Exception"

  11. #11
    Registered User
    Join Date
    Dec 2001
    Posts
    206
    first off, noone deserves to be told they "dont deserve anything".
    If you came up to me in real life and told me that i would knock you out. You say you were .. what was it? CIVIL? telling someone they deserve nothing is not civil its BS.



    secondly, drop the act, you are wrong, you were not being civil, and acting civil after the fact does not make u civil.

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    27

    subject

    Well, after getting totally off subject (perhaps you two can send private messages to eachother)...

    Your code looked overly compex. why not just have a 3 1D arrays one to keep the single count, one to keep the double, one to keep triples. then initalize all the arrays to zero. run a while to get the charecters from the file and have a switch function that checks the letter's value. if you get an 'a' add one to the 'a' count of the array. then for doubles just keep the previous letter in a variable and check the current letter against the prevous. for triples do the same...


    Gr3g
    Chance favors the prepared mind.

    Vis. C++ 6.0

  13. #13
    Registered User DeadArchDown's Avatar
    Join Date
    Apr 2002
    Posts
    28
    Originally posted by Denethor2000
    first off, noone deserves to be told they "dont deserve anything".
    Think about that. He came, he wanted help, he didn't deserve help. It's the context. I didn't mean he was such a terrible little person that he deserved nothing...obviously. I ment he didn't deserve the answer put forth exactely how he said he did. You don't deserve things like that. Your lucky that you get help at all. It's all a moot point really though. Im done with you. You don't need to associate with me anymore, if i could ask that favor.
    -------------------
    "Exception"

  14. #14
    Registered User
    Join Date
    Mar 2002
    Posts
    27

    Again let not fight

    It doesnt matter what everyone said, lets just forget it... And in the future perhaps we can stick to the subject of c++.

    Gr3g
    Chance favors the prepared mind.

    Vis. C++ 6.0

  15. #15
    Registered User
    Join Date
    Dec 2001
    Posts
    206
    you dont "deserve" to be left alone... butok :P

    ive just been looking for a fight... since id get my ass kicked at school i decided to pick it here lol.(subconciously of course) sry ^_^


    ,.,.,.,.,., ,.,.,.,.,.,. ,.,.,.,.,.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. frequency character counter
    By hasanah in forum C Programming
    Replies: 4
    Last Post: 04-15-2009, 01:28 AM
  2. adjusting character counts for utf8
    By MK27 in forum C Programming
    Replies: 32
    Last Post: 02-03-2009, 07:07 PM
  3. Character handling help
    By vandalay in forum C Programming
    Replies: 18
    Last Post: 03-29-2004, 05:32 PM
  4. Replies: 5
    Last Post: 11-20-2003, 01:27 AM
  5. How to complete program. Any probs now?
    By stehigs321 in forum C Programming
    Replies: 7
    Last Post: 11-19-2003, 04:03 PM