frequencyy list

This is a discussion on frequencyy list within the C Programming forums, part of the General Programming Boards category; I wanted to start on a huffman program and the first thing is to read from stdin and generate a ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    9

    frequencyy list

    I wanted to start on a huffman program and the first thing is to read from stdin and generate a simple frequency list. I read that it's nice to generate an additional pseduo-EOF and give it frequency of 1 so that later when you encode/decode you know when to stop. I've been able to generate the table but I haven't been able to add this extra character and I was wondering if anyone knew how to add this terminating character so that the program knows when to stop encoding/decoding.

    Code:
    #include <stdio.h>
    #include <limits.h>
    
    int main()
    {
    
         int ch;
         unsigned long array[256];
         //while ( (ch = getchar()) != EOF)
         ++array[ch];
    
         for(int i = 0; i < 256; i++)
            printf("%d\t%lu\n", i, array[i]);
    
    }
    This only gives me the chars that appear in the text and their frequency but I haven't been able to generate the extra EOF char with freq = 1.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    EOF is NOT a character. Strings in C are terminated by the null '\0' escape sequence. Read in as much as you can and then set the next index to '\0'. If you want to read in EOF from the keyboard, the proper keys are ^Z (control+Z), I believe, but as you can see, it's quite annoying to ask for that keypress. You'd be better off taking the newline character.
    Last edited by SlyMaelstrom; 05-03-2006 at 01:48 PM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    9
    Yes you're right, its not really the EOF character, but I was told to just generate a new character that will will have frequency of 1 and that will represent that the file is over. I wasn't sure how to do that or how I would hard code that value in.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Your while loop explicitly prevents array[EOF] from ever being accessed.

    EOF is a negative value (usually -1), so you'll get array[-1] or array[65535] or something. Not what you want. Just use the EOF as a pseudo-character that appears at the end of the file.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    >> I was told to just generate a new character that will will have frequency of 1 and that will represent that the file is over

    you would probably have to reserve a character (which means it can't appear in the precompressed stream) or else tweak the tree so that the 'EOF' node is the farthest leaf. storing the size of the compressed data (in the compression stream, perhaps) would be much easier to implement, IMO.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > unsigned long array[256];
    You also need to initialise this to zeros, say with
    Code:
    unsigned long array[256] = { 0 };
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 08:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 04:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21