Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Histogram of character frequencies

Reply
Thread Tools

Histogram of character frequencies

 
 
rajash@thisisnotmyrealemail.com
Guest
Posts: n/a
 
      12-01-2007
Hello everyone,

Thanks again for all the suggestions, though I think some people are a
bit fussy in their answers.

Here is a solution to Exercise 1.14. It deals well with control
characters too.

// make histogram of character frequencies

int x[256]; // frequencies

void main()
{
char c;
int i, y=0, z;
while(! feof(stdin) )
if(++x[c=getchar()]>y)
y=x[z=c];
do {
for(i=0; i<256; i++)
if(x[i]>0)
printf("%s", x[i]>y ? " * " : " ");
printf("\n");
} while(y--);
for(i=0; i<256; i++)
if(x[i]>0)
if(i>32)
printf(" %c ", i);
else
printf("%02x ", i);
printf("\n");
}
 
Reply With Quote
 
 
 
 
Johannes Bauer
Guest
Posts: n/a
 
      12-01-2007
schrieb:

> int x[256]; // frequencies


Global.

> void main()


Illegal.

Either you are pretty dumb or you actually do not read ANY of the
answers which are given to you. I vote for number one.

Greetings anyways,
Johannes

--
"Viele der Theorien der Mathematiker sind falsch und klar
Gotteslästerlich. Ich vermute, dass diese falschen Theorien genau
deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka
HJP in de.sci.mathematik <4740ad67$0$3811$>
 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      12-01-2007
wrote:

> Hello everyone,
>
> Thanks again for all the suggestions, though I think some people are a
> bit fussy in their answers.
>
> Here is a solution to Exercise 1.14. It deals well with control
> characters too.
>
> // make histogram of character frequencies
>
> int x[256]; // frequencies
>
> void main()


You have been told multiple times already that the above is most likely
unportable and pretty bad form.

> {
> char c;
> int i, y=0, z;
> while(! feof(stdin) )


You have told multiple times that you test for end-of-file or error
*after* an I/O operation has returned EOF or someother error status.

> if(++x[c=getchar()]>y)


getchar() is undefined since you have failed to include the relevant
header (stdio.h). Even if you had done so the above call is still wrong
since getchar() returns an int so that it can return the out of band
value EOF upon end-of-file or error. The character with value zero is a
null character, which you do not normally encounter on interactive
input. You should assign the return value of getchar() to an int object
and test it against EOF before proceeding.

> y=x[z=c];


You seem to prefer obfuscation and incorrect code.

> do {
> for(i=0; i<256; i++)
> if(x[i]>0)
> printf("%s", x[i]>y ? " * " : " ");
> printf("\n");
> } while(y--);
> for(i=0; i<256; i++)
> if(x[i]>0)
> if(i>32)
> printf(" %c ", i);
> else
> printf("%02x ", i);
> printf("\n");
> }


As the other respondent notes you either do not bother to take not of
helpful advice or you are a troll. Unless you show some willingness to
learn from your mistakes you are going to find yourself quickly ignored
by most the group's participants.

 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      12-01-2007
wrote:
> Hello everyone,
>
> Thanks again for all the suggestions, though I think some people are a
> bit fussy in their answers.


You need to redefine "fussy". Your code is broken from the very
beginning. Not including header files or otherwise providing required
declarations of variadic functions (printf), and misusing feof() as the
control condition for your loop is bad enough, but the illiterate

> void main()


is a sure way to signal your incompetence to anyone reading your code
(and possibly to the compiler as well).

You have been told about these things and refuse to fix them. This
smacks of an obstinacy in error and inability to learn that suggests you
take up Business Administration or some other brainless pursuit instead
of programming.
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      12-01-2007
wrote:

> void main()


I've decided that you're trolling. Or stupid. Either way, a waste of my
time.

*plonk*




Brian
 
Reply With Quote
 
rajash@thisisnotmyrealemail.com
Guest
Posts: n/a
 
      12-01-2007
Johannes Bauer wrote:
> schrieb:
>
> > int x[256]; // frequencies

>
> Global.


It's completely acceptable to have variables defined at file scope in
C!

>
> > void main()

>
> Illegal.


Why does everyone have this hangup about this? I took a class in C a
while back and my teacher always used void main() { ... }. I can
confirm that it works fine with both MicroSoft compiler and BorLand.

>
> Either you are pretty dumb or you actually do not read ANY of the
> answers which are given to you. I vote for number one.


I read the answers but mostly people only comment on trivial things
that aren't even errors! I'll be glad to have substantial comments on
my code.

>
> Greetings anyways,
> Johannes


Greets.

>
> --
> "Viele der Theorien der Mathematiker sind falsch und klar
> Gotteslästerlich. Ich vermute, dass diese falschen Theorien genau
> deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka
> HJP in de.sci.mathematik <4740ad67$0$3811$>

 
Reply With Quote
 
Chad
Guest
Posts: n/a
 
      12-01-2007
On Dec 1, 2:02 pm, raj...@thisisnotmyrealemail.com wrote:
> Johannes Bauer wrote:
> > raj...@thisisnotmyrealemail.com schrieb:

>
> > > int x[256]; // frequencies

>
> > Global.

>
> It's completely acceptable to have variables defined at file scope in
> C!
>
>
>
> > > void main()

>
> > Illegal.

>
> Why does everyone have this hangup about this? I took a class in C a
> while back and my teacher always used void main() { ... }. I can
> confirm that it works fine with both MicroSoft compiler and BorLand.
>
>
>
> > Either you are pretty dumb or you actually do not read ANY of the
> > answers which are given to you. I vote for number one.

>
> I read the answers but mostly people only comment on trivial things
> that aren't even errors! I'll be glad to have substantial comments on
> my code.
>
>
>
> > Greetings anyways,
> > Johannes

>
> Greets.
>
>
>
> > --
> > "Viele der Theorien der Mathematiker sind falsch und klar
> > Gotteslästerlich. Ich vermute, dass diese falschen Theorien genau
> > deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka
> > HJP in de.sci.mathematik <4740ad67$0$3811$54022...@news.sunrise.ch>



Your program won't even compile on my machine.

[ ~]$ more freq.c
int x[256]; // frequencies

void main()
{
char c;
int i, y=0, z;
while(! feof(stdin) )
if(++x[c=getchar()]>y)
y=x[z=c];
do {
for(i=0; i<256; i++)
if(x[i]>0)
printf("%s", x[i]>y ? " * " : " ");
printf("\n");
} while(y--);
for(i=0; i<256; i++)
if(x[i]>0)
if(i>32)
printf(" %c ", i);
else
printf("%02x ", i);
printf("\n");

}
[ ~]$ gcc --version
gcc (GCC) 4.1.1 20061011 (Red Hat 4.1.1-30)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There
is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

[ ~]$ gcc -g freq.c -o freq
freq.c: In function 'main':
freq.c:7: error: 'stdin' undeclared (first use in this function)
freq.c:7: error: (Each undeclared identifier is reported only once
freq.c:7: error: for each function it appears in.)
freq.c:13: warning: incompatible implicit declaration of built-in
function 'printf'
freq.c:19: warning: incompatible implicit declaration of built-in
function 'printf'
freq.c:4: warning: return type of 'main' is not 'int'
[ ~]$
 
Reply With Quote
 
Johannes Bauer
Guest
Posts: n/a
 
      12-01-2007
schrieb:

>>> int x[256]; // frequencies

>> Global.

>
> It's completely acceptable to have variables defined at file scope in
> C!


Believe me, I know. But it's very bad style to do so if not absolutely
necessary. Even more so, if you need to use global variables, you should
declare them "static" if they're module-private.

And the comment you used "//" is *not* valid C, it's C++. So this simple
line of code contains lots of mistakes, each and every one should be
avoided in order to create portable code.

>>> void main()

>> Illegal.

>
> Why does everyone have this hangup about this? I took a class in C a
> while back and my teacher always used void main() { ... }. I can
> confirm that it works fine with both MicroSoft compiler and BorLand.


So what? What does this tell us? Only that your teacher is an idiot. The
reference for C is not your teacher, whoever he or she may be, but the
*official* C standard. And the standard says: it's wrong, period.

Did your drivers ed teacher tell you "It's okay to run stop signs if
nobody's watching!" or "You only need to care about speed limits when
police is around!"? Did your math teacher tell you "Go ahead and divide
by zero, that's alright" or maybe your English teacher "Screw those
apostrophes, they're useless anyways"?

I sure hope that your stupid programming teacher is the only one who
spreads false "knowledge".

>> Either you are pretty dumb or you actually do not read ANY of the
>> answers which are given to you. I vote for number one.

>
> I read the answers but mostly people only comment on trivial things
> that aren't even errors! I'll be glad to have substantial comments on
> my code.


You code contains errors. Why do you post it here? To learn something?
Then accept the hints you're given. You'll be grateful at some point in
time, although you're currently obviously far too stubborn to realize
what you're doing.

If you need somebody to pet you and tells you your code is great, this
is probably the wrong place.

Greetings,
Johannes

--
"Viele der Theorien der Mathematiker sind falsch und klar
Gotteslästerlich. Ich vermute, dass diese falschen Theorien genau
deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka
HJP in de.sci.mathematik <4740ad67$0$3811$>
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      12-01-2007
wrote:
> Johannes Bauer wrote:
>> schrieb:
>>

.... snip ...
>>
>>> void main()

>>
>> Illegal.

>
> Why does everyone have this hangup about this? I took a class in C
> a while back and my teacher always used void main() { ... }. I can
> confirm that it works fine with both MicroSoft compiler and BorLand.


Because they are gross errors, and not allowed in portable code by
the ISO C standard. Which, in turn, is the basis for all code
discussed in this newsgroup. If you want to discuss Microsoft
software (junk), use a Microsoft newsgroup. If you want to discuss
Borland software, use a Borland newsgroup. However, if you want to
discuss C, come here.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      12-02-2007
wrote:
> Johannes Bauer wrote:
>> schrieb:
>>
>>> int x[256]; // frequencies

>> Global.

>
> It's completely acceptable to have variables defined at file scope in
> C!


What's acceptable is not always a good idea. Global objects have many
disadvantages; they should be avoided except when necessary; they aren't
necessary in this case.

> Why does everyone have this hangup about this? I took a class in C a
> while back and my teacher always used void main() { ... }. I can
> confirm that it works fine with both MicroSoft compiler and BorLand.


That doesn't make it legal. A conforming implementation of C is allowed
to reject a program which declares main() that way.

....
> I read the answers but mostly people only comment on trivial things
> that aren't even errors! I'll be glad to have substantial comments on
> my code.


Several of the "trivial" things people have commented on ARE errors, and
serious ones - you don't seem to understand how serious. Most
importantly, #inclusion of the appropriate standard headers is
absolutely essential for your code to even compile, at least under most
implentations of C. If what you've given us is the complete text of your
program, and if you are using a compiler which accepts your code as
written, junk it - it's teaching you some very bad habits.

Also, you're using feof() incorrectly, and until you understand why the
way that you're using it is incorrect, I would not recommend relying
upon any of your programs to function properly.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
D80 histogram vs histogram on computer Martin Sørensen Digital Photography 14 12-19-2007 09:41 PM
numpy: frequencies robert Python 2 11-18-2006 05:41 PM
Arbitrary Clock Frequencies From Base Clock abhisheknag@gmail.com VHDL 5 06-23-2006 12:45 PM
B vs G: Frequencies Used? (PeteCresswell) Wireless Networking 2 01-01-2006 08:16 PM
Cordless keyboard mouse frequencies Melv Computer Support 7 11-29-2003 03:34 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57