Thread: problm o........ing ASCII frequencies

  1. #1
    Temporal Apparition qubit67's Avatar
    Join Date
    Jan 2007
    Posts
    85

    problm o........ing ASCII frequencies

    I have written this :
    Code:
    #include <stdio.h>
    
    #define ASCII_VALUES 256
    
    int
    main(int argc, char **argv) {
    	int i, ch;
    	int frequency[ASCII_VALUES];
    	
    	for (i = 0; i < ASCII_VALUES; i++)
    	{
    		frequency[i] = 0;
    	}
    	
    	
    	printf("Enter some text to count the frequency of the characters: ");
    	
    	while ((ch=getchar()) != EOF)
    	{
    			if (frequency[ch]!=0)
    			{
    				frequency[ch] += 1;	
    			}
    			 else 
    			{
    				frequency[ch] = 1;
    			}
    	}
    	printf("Character  Frequency");
    	
    	for (i = 0; i < ASCII_VALUES; i++)
    	{
    		if (frequency[i] != 0 )
    		{
    			printf("%c		%d\n", i, frequency[i]);	
    		}
    	}
    	return 0;
    }
    I cant see what is wrong with it, but it isn't counting the frequencies...

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    			if (frequency[ch]!=0)
    			{
    				frequency[ch] += 1;	
    			}
    			 else 
    			{
    				frequency[ch] = 1;
    			}
    Why not
    Code:
    frequency[ch] ++;
    Code:
    	int frequency[ASCII_VALUES];
    	
    	for (i = 0; i < ASCII_VALUES; i++)
    	{
    		frequency[i] = 0;
    	}
    Why not
    Code:
    int frequency[ASCII_VALUES] = {0};
    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.

  3. #3
    Temporal Apparition qubit67's Avatar
    Join Date
    Jan 2007
    Posts
    85
    Ok, I made those changes, and the program still is unable to count the frequencies properly. Complies and runs, prints 1's for all letters input. I think it is something to do with iterating the value.. please help!

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    printf("&#37;c		%d\n", i, frequency[i]);
    You might want to make sure isprint(i) from ctype.h returns 1 before you go printing i.

    Post your updated code . . . if you used
    Code:
    frequency[ch] ++;
    then I can't see what could be wrong with it, though. Have you re-compiled your program?

    [edit] Instead of using 256 you could use UCHAR_MAX from <limits.h>. [/edit]
    Last edited by dwks; 03-31-2007 at 07:34 PM.
    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
    Temporal Apparition qubit67's Avatar
    Join Date
    Jan 2007
    Posts
    85
    yes, I did recompile it :
    Code:
    #include <stdio.h>
    
    #define ASCII_VALUES 256
    
    int
    main(int argc, char **argv) {
    	int i, ch;
    	int frequency[ASCII_VALUES] = {0};
    
    	printf("Enter some text to count the frequency of the characters: ");
    	
    	while ((ch=getchar()) != EOF)
    	{
    			if (frequency[ch]==0)
    			{
    				frequency[ch] = 1;	
    			}
    			else
    			{
    				frequency[ch] ++;
    			}
    	}
    	printf("Character  Frequency");
    	
    	for (i = 0; i < ASCII_VALUES; i++)
    	{
    		if (frequency[i] != 0 )
    		{
    			printf("&#37;c		%d\n", i, frequency[i]);	
    		}
    	}
    	return 0;
    }
    for : HELLO WORLD
    prints:
    Code:
    Enter some text to count the frequency of the characters: HELLO WORLD
    Character  Frequency
                    1
                    1
    D               1
    E               1
    H               1
    L               1
    O               1
    R               1
    W               1
    im not sure how to use isprint. I'm kicking myself because i had the answer worked out last night but erased the file

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Erhm, I still don't get this code. The code in the else would suffice.
    Code:
    			if (frequency[ch]==0)
    			{
    				frequency[ch] = 1;	
    			}
    			else
    			{
    				frequency[ch] ++;
    			}
    I don't know why it's messing up.

    Try just this:
    Code:
    frequency[ch] ++;
    ++ works when the variable is zero, you know.

    im not sure how to use isprint.
    Code:
    #include <ctype.h>
    
    if(isprint(c)) putchar(c);
    else putchar('-');
    > I'm kicking myself because i had the answer worked out last night but erased the file
    Backups, my dear Watson. (Guess why I'm out of hard disk space? )
    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.

  7. #7
    Temporal Apparition qubit67's Avatar
    Join Date
    Jan 2007
    Posts
    85
    ok ill give it a shot...

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Also add something like
    Code:
    printf("got here\n")
    to make sure your code is re-compiling. That seems like the most likely culprit to me. (Just as a guess.)
    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.

  9. #9
    Temporal Apparition qubit67's Avatar
    Join Date
    Jan 2007
    Posts
    85
    it certainly maximised the program :
    Code:
    #include <stdio.h>
    
    #define ASCII_VALUES 256
    
    int
    main(int argc, char **argv) {
    	int i, ch;
    	int frequency[ASCII_VALUES] = {0};
    
    	printf("Enter some text to count the frequency of the characters: ");
    	
    	while ((ch=getchar()) != EOF)
    	{
    			frequency[ch] ++;
    	}
    	printf("Character  Frequency");
    	
    	for (i = 0; i < ASCII_VALUES; i++)
    	{
    		if (frequency[i] != 0 )
    		{
    			printf("&#37;c		%d\n", i, frequency[i]);	
    		}
    	}
    	return 0;
    }
    but no luck , all 1's still ?

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Like I said, there's nothing wrong with your code. Here's what I get when I run it.
    Code:
    Enter some text to count the frequency of the characters: hello world
    ^Z
    Character  Frequency
                    1
                    1
    d               1
    e               1
    h               1
    l               3
    o               2
    r               1
    w               1
    You're just not re-compiling it or something. Copy the source file to another name and try compiling that. It may help.
    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.

  11. #11
    Temporal Apparition qubit67's Avatar
    Join Date
    Jan 2007
    Posts
    85
    hey, my bad -> I was working on ASCII2.c and compiling ASCII.c AAARRRHHHHH!!!!!
    Thank you very much for your patience and sorry bout the runaround. The program works a charm now

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by qubit67 View Post
    hey, my bad -> I was working on ASCII2.c and compiling ASCII.c AAARRRHHHHH!!!!!
    Thank you very much for your patience and sorry bout the runaround. The program works a charm now
    I remember someone do that to code I had written. They were stating my code didn't work when they were busy compiling and testing another source with a similar name.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  3. Office access in C/C++ NOT VC++!! :)
    By skawky in forum C++ Programming
    Replies: 1
    Last Post: 05-26-2005, 01:43 PM
  4. ascii values for keys
    By acid45 in forum C Programming
    Replies: 2
    Last Post: 05-12-2003, 07:13 AM
  5. Checking ascii values of char input
    By yank in forum C Programming
    Replies: 2
    Last Post: 04-29-2003, 07:49 AM