Giving the histogram a shot...

A

Arthur J. O'Dwyer

Nit #1: Functions haven't been covered at this point in the book (K&R2 p24);
they are introduced in the next section.


Nit #2: Exercise 1-13 asks for a histogram of the *lengths* of the words in
the input and Exercise 1-14 asks for a histogram of the frequencies of
different *characters* in the input. Neither needs the number of words.

Anti-nit: What do you suppose the Y-axis of the histogram measures?
Suppose I have 5 words of length 1 and 3 words of length 2; how many
words do I have all together? I think writing a word-counting function
is an excellent way to start this project.
To the OP: There is a lot of good advice in this thread about how to approach
the problem. Another thing to keep in mind is how you are going the *test*
your program to make sure that does what you intended. Sometimes this can be
more difficult than writing the actual code.

Definitely!

-Arthur
 
T

Tim Hagan

Arthur J. O'Dwyer said:
Anti-nit: What do you suppose the Y-axis of the histogram measures?

Ooops. I realized this after re-examining my own solution to that exercise.
Suppose I have 5 words of length 1 and 3 words of length 2; how many
words do I have all together?

Why do you care what the total is? We only need the number of words of each
length to draw the histogram.
I think writing a word-counting function
is an excellent way to start this project.

Yes, but see Nit #1. :)
 
M

Mike Wahler

Alex said:
Indeed. But you may potentially take a horrible performance hit. :)

I doubt it, but as indicated elsethread, this was
really just 'joke' code.

-Mike
 
A

Alex

I doubt it, but as indicated elsethread, this was
really just 'joke' code.

Notice the ':)'.

In fact, IIRC there was a period when C did not have logical
operators and bitwise operators were used for these things.
That is, a bitwise OR would have the same effect as your '+'.

Alex
 
P

pete

Alex wrote:
In fact, IIRC there was a period when C did not have logical
operators and bitwise operators were used for these things.
That is, a bitwise OR would have the same effect as your '+'.

C documents I've seen, going back to 1974, all have logical operators.
 
M

Malcolm

The real OS2 guy said:
Why is making a variable size buffer and calli ng function on that
easier as to handle a stram of chars? What can be easier as to have a
simple stram where one char ges flagged or a flag gets tested when the
time comes?

The OP has quitely found the best solution available for the problem.
The problem is that, once you've processed the stream, its gone and it might
not be easy to reopen the file. This means the solution won't scale up.
The string solution is also more likely to be re-usable, and it is easier to
construct test cases.
 
T

The real OS2 guy

The problem is that, once you've processed the stream, its gone and it might
not be easy to reopen the file. This means the solution won't scale up.
The string solution is also more likely to be re-usable, and it is easier to
construct test cases.
Where is the need to read a stream more than once when you've
processed anything?

If you needs the stram tiled in words, then do it, if you needs the
stream tiled in numbers, words, lines, char, you can do it without
touching a chare again after you've processed it.

Does you really think it is better to copy a number of characters from
here to there to there to there to process it only to copy it from
there to there again?

Does you like a very complex sample how to read a stram once, scan it
multiple times, check for highly different things - but having the
straem reading once?

try a look into bison or yacc. You can use them for any parsing you
may ever need - but as you have seldom really complex syntax rules it
is more often enough to write your parser youself. Breaking a line in
its kown components is easy, if you can't trust the soure (like that
what can come from a keyboard) you may have the need for more
extensive testing. But anyway you can work straight forward. Means
when you gets confused you can simply read away anything until you
gets a known sync point, such as a seaparator char (';' or '\n' or
something elsy you knows it is) and restart from phase 1 with it.

If your parsing needs to read ahead more than one char then it is not
too complex to write a wrapper around getc() and ungetc() to write
more than one char back into the stream. You needs the wrapper because
ungetc() can only write back one char.

But at least you saves the overhead of dynamic allocated and extended
input buffers. Even as often you may need dynamic sized output fields.

You can easy tokenise the stream without using strtok. You can easy
convert any kind of number to binary on the fly, you can easy separate
strings from another on the fly whereas you needs nothing than some
kinds of separators (whitre spaces, commas, single or double quotes,
colons, semicolons.....). Some of them may be only seperators in some
cases (e.g. decimal points during read of a float, '-'/'+' sing in
some positions......

You should know that C works with streams. A stream is nothing more
than a serial sequence of characters. Even as some functions in the
standard library concencates a number of chars to a buffer, the stream
itself lefts always as a pure sequence of chars.

So, always when you have to read an untrusted stream, it is always the
best to read it char by char to save any overhead.

When you doesn't hack but you're programming you'll find this idea
wounderful.

--
Tschau/Bye

Herbert Rosenau
http://www.pc-rosenau.de eComStation Reseller in Germany
eCS 1.1 german is in beta testing
 
A

Alex

Not quite. There was a time when C (or "proto-C") made no differentiation
in appearance between the BITWISE &| and the LOGICAL &|. That was long
before C89, and also long, long before my time, so this is all third-hand
information; but I understand that in those days, the compiler would keep
track somehow of whether the current context was "integral" or "boolean"
in nature, and apply the correct semantics depending on that context.

<snip example>

I was misinformed, this was decades before my time. Thanks.

Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top