need some help with this histogram of words program........

B

Ben Bacarisse

Rather then just say "use a do loop" I've tried to show you can be
thinking about your programs be showing you a sequences of small
improvements.
read:

Rather then just saying "use a do loop" I've tried to show you how you
could be thinking about your programs by showing you a sequences of small
improvements (that gets you to the same endpoint).
 
C

Ceriousmall

read:

Rather then just saying "use a do loop" I've tried to show you how you
could be thinking about your programs by showing you a sequences of small
improvements (that gets you to the same endpoint).

..
..
..
Thanks Ben very helpful and insightful info. It's amazing what a
difference in perspective can do...........
 
C

Ceriousmall

"io_x" <[email protected]> ha scritto nel messaggio







note that if i read ok the routine in K&R2 not count the last word
..
..
..
At this stage I can't say why this is happening but from what I've
learnt so far I'm thinking this has something to do with the way the
end of input is being handled. You however have got a bug free version
of this program so my question to you is how does your code address
this issue?

_______________________________________________
Ceriously
I'm really beginning to C the code now. . . . .
 
C

Ceriousmall

"io_x" <[email protected]> ha scritto nel messaggio






the K&R2 routine about that, is ok


well I've modified that code since then and the result is as
follows...........

while (at_start != EOF) {
c = getchar();
at_end = c == EOF;

if (c >= '0' && c <= '9'){
nchar = 0;
not_a_word = TRUE;
}
else if (c == ' ' || c == '\n' || c == '\t' || c == EOF) {
if (nchar > 0 && nchar < LIMIT)
++wordlength[nchar];
else if (nchar >= LIMIT)
++wordlength[LIMIT];
nchar = 0;
not_a_word = FALSE;
}
else if (not_a_word == FALSE)
++nchar;
if (at_end)
at_start = EOF;
 
B

Ben Bacarisse

Ceriousmall said:
well I've modified that code since then and the result is as
follows...........

while (at_start != EOF) {
c = getchar();
at_end = c == EOF;

if (c >= '0' && c <= '9'){
nchar = 0;
not_a_word = TRUE;
}
else if (c == ' ' || c == '\n' || c == '\t' || c == EOF) {
if (nchar > 0 && nchar < LIMIT)
++wordlength[nchar];
else if (nchar >= LIMIT)
++wordlength[LIMIT];
nchar = 0;
not_a_word = FALSE;
}
else if (not_a_word == FALSE)
++nchar;
if (at_end)
at_start = EOF;

I am assuming this is the end of the loop.

There's no point in having both at_end and at_start. The purpose of
one seems to be to set the other and noting else.

Can you summarise what you consider to be a word? You make a special case of
digits use an extra variable to exclude some cases that I would have
accepted as words. For example, "2-dimensional" seems to be at least as
much a word as "+-+-+". You will count the second but not the first.

Why do you write "if (not_a_word == FALSE)" but then "if (at_end)"? I'd
stick to one, though I'd strongly recommend not testing Booleans
against constants like TRUE and FALSE.
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top