Thread: frequencyy list

  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,079
    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,057
    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,708
    >> 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:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > 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.

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, 06:55 PM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09: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, 05:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM