Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Histogram of character frequencies (http://www.velocityreviews.com/forums/t556585-histogram-of-character-frequencies.html)

 rajash@thisisnotmyrealemail.com 12-01-2007 05:21 PM

Histogram of character frequencies

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 12-01-2007 05:25 PM

Re: Histogram of character frequencies

rajash@thisisnotmyrealemail.com 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 12-01-2007 05:42 PM

Re: Histogram of character frequencies

rajash@thisisnotmyrealemail.com 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 12-01-2007 07:18 PM

Re: Histogram of character frequencies

rajash@thisisnotmyrealemail.com 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 12-01-2007 07:42 PM

Re: Histogram of character frequencies

rajash@thisisnotmyrealemail.com wrote:

> void main()

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

*plonk*

Brian

 rajash@thisisnotmyrealemail.com 12-01-2007 10:02 PM

Re: Histogram of character frequencies

Johannes Bauer wrote:
> rajash@thisisnotmyrealemail.com 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

Re: Histogram of character frequencies

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.

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

[cdalten@eecs.berkeley.edu ~]\$ 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");

}
[cdalten@eecs.berkeley.edu ~]\$ 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.

[cdalten@eecs.berkeley.edu ~]\$ 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'
[cdalten@eecs.berkeley.edu ~]\$

 Johannes Bauer 12-01-2007 10:43 PM

Re: Histogram of character frequencies

rajash@thisisnotmyrealemail.com 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 12-01-2007 11:39 PM

Re: Histogram of character frequencies

rajash@thisisnotmyrealemail.com wrote:
> Johannes Bauer wrote:
>> rajash@thisisnotmyrealemail.com 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 12-02-2007 12:02 AM

Re: Histogram of character frequencies

rajash@thisisnotmyrealemail.com wrote:
> Johannes Bauer wrote:
>> rajash@thisisnotmyrealemail.com 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.

All times are GMT. The time now is 03:30 PM.