Velocity Reviews > Histogram of character frequencies

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

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");
}

Johannes Bauer
Guest
Posts: n/a

 12-01-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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

santosh
Guest
Posts: n/a

 12-01-2007
(E-Mail Removed) 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

> {
> 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.

Martin Ambuhl
Guest
Posts: n/a

 12-01-2007
(E-Mail Removed) 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

> void main()

(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
of programming.

Default User
Guest
Posts: n/a

 12-01-2007
(E-Mail Removed) wrote:

> void main()

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

*plonk*

Brian

rajash@thisisnotmyrealemail.com
Guest
Posts: n/a

 12-01-2007
Johannes Bauer wrote:
> (E-Mail Removed) schrieb:
>
> > int x[256]; // frequencies

>
> Global.

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

>
> > void main()

>
> Illegal.

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\$(E-Mail Removed)>

Guest
Posts: n/a

 12-01-2007
On Dec 1, 2:02 pm, (E-Mail Removed) wrote:
> Johannes Bauer wrote:
> > (E-Mail Removed) schrieb:

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

>
> > Global.

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

>
> > Illegal.

>
> 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\$(E-Mail Removed)>

Your program won't even compile on my machine.

[(E-Mail Removed) ~]\$ 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");

}
[(E-Mail Removed) ~]\$ 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.

[(E-Mail Removed) ~]\$ 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'
[(E-Mail Removed) ~]\$

Johannes Bauer
Guest
Posts: n/a

 12-01-2007
(E-Mail Removed) 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.

>
> 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

>> 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

CBFalconer
Guest
Posts: n/a

 12-01-2007
(E-Mail Removed) wrote:
> Johannes Bauer wrote:
>> (E-Mail Removed) schrieb:
>>

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

>>
>> Illegal.

>
> 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>

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

James Kuyper
Guest
Posts: n/a

 12-02-2007
(E-Mail Removed) wrote:
> Johannes Bauer wrote:
>> (E-Mail Removed) 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.

> 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.

 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 OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Martin Sørensen Digital Photography 14 12-19-2007 09:41 PM robert Python 2 11-18-2006 05:41 PM abhisheknag@gmail.com VHDL 5 06-23-2006 12:45 PM (PeteCresswell) Wireless Networking 2 01-01-2006 08:16 PM Melv Computer Support 7 11-29-2003 03:34 AM