Re: K&R2 1.6 Arrays, exercise 1-13 (vertical histogram)
- From: Flash Gordon <spam@xxxxxxxxxxxxxxxxxx>
- Date: Thu, 05 Apr 2007 18:57:28 +0100
Richard Heathfield wrote, On 05/04/07 01:47:
Flash Gordon said:
<snip>
Comments on Richard's code...
| int main(void)
| {
| int c;
I thought you always initialised variables on declaration, or am I
thinking of someone else ;-)
Firstly, I know you're just yanking my chain. But you want to hear it rattle, so I'll keep going.
Indeed I was.
Secondly, you're looking at code that is almost a decade old.
I did not register that it was that old.
Thirdly, my decision to adopt blanket initialisation was made after many years of *not* doing it, and even nowadays I find that old habits die hard. But, if it helps, I regard the above code as containing a bug.
I would not consider it a bug as such, just less than optimal. You can, of course, improve your code on the Wiki.
<snip>
int inspace = 0;<snip>
long lengtharr[MAXWORDLEN + 1];
for(thisidx = 0; thisidx <= MAXWORDLEN; thisidx++)This, of course, could have been done by initialising on declaration
{
lengtharr[thisidx] = 0;
}
as you know, I assume you did not because that has not been covered at
that point in K&R?
Either that or because, when I first wrote the code (which was many years ago), it's entirely possible that I *didn't* know the = {0} thing.
(If the latter reason is true, learning it would have done no good here, because Reason 1 would still have applied.)
Yes, I agree.
while(done == 0)If you had initialised c to something other than EOF you could have
used:
while (c!=EOF)
thus saving yourself an extra state variable.
Yes, I could have done that. Some people may even consider it a good idea. In my view, however, the state variable makes it easier for a newbie to read the code.
Readability would be a valid reason whether it was for the newbie audience or for serious code to be maintained by highly skilled professionals. I would not have bothered with the comment if I was reviewing the code professionally, I only commented on it at all because of your comment above the code about adding the extra state variable.
In a review I would have been more likely to suggest replacing the while loop with a do-while loop since it is explicitly a "do at lest once" loop.
Of course, there is also the option of using a break instead of a state variable. However, I would not consider this to be an improvement, merely a change which some have valid reasons to dislike.
The most serious complaints I have on your code are that you are not checking for overflow on your counts, passing a long to printf having told it to expect an int!
| long thisval = 0;
<snip>
| printf("%4d | ", thisval);
Your output will also be messed up if you have too many of a given word length.
Now, if Arnuld has followed this discussion of your code (my main reason for commenting on it was so that he could follow the discussion) he should have learnt a couple of additional things.
--
Flash Gordon
.
- Follow-Ups:
- Re: K&R2 1.6 Arrays, exercise 1-13 (vertical histogram)
- From: Richard Heathfield
- Re: K&R2 1.6 Arrays, exercise 1-13 (vertical histogram)
- From: Flash Gordon
- Re: K&R2 1.6 Arrays, exercise 1-13 (vertical histogram)
- References:
- K&R2 1.6 Arrays, exercise 1-13 (vertical histogram)
- From: arnuld
- Re: K&R2 1.6 Arrays, exercise 1-13 (vertical histogram)
- From: Flash Gordon
- Re: K&R2 1.6 Arrays, exercise 1-13 (vertical histogram)
- From: Richard Heathfield
- K&R2 1.6 Arrays, exercise 1-13 (vertical histogram)
- Prev by Date: Re: K&R2 1.6 Arrays, exercise 1-13 (vertical histogram)
- Next by Date: Re: K&R2 1.6 Arrays, exercise 1-13 (vertical histogram)
- Previous by thread: Re: K&R2 1.6 Arrays, exercise 1-13 (vertical histogram)
- Next by thread: Re: K&R2 1.6 Arrays, exercise 1-13 (vertical histogram)
- Index(es):
Relevant Pages
|