Re: K&R2 1.6 Arrays, exercise 1-13 (vertical histogram)



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;
long lengtharr[MAXWORDLEN + 1];
<snip>

for(thisidx = 0; thisidx <= MAXWORDLEN; thisidx++)
{
lengtharr[thisidx] = 0;
}
This, of course, could have been done by initialising on declaration
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
.



Relevant Pages

  • Re: K&R2 1.6 Arrays, exercise 1-13 (vertical histogram)
    ... | int main ... This, of course, could have been done by initialising on declaration as you know, I assume you did not because that has not been covered at that point in K&R? ... I certainly would not bother initialising i or j since they are used as simple loop counters and initialised in the loop. ...
    (comp.lang.c)
  • question on alternate function declaration syntax
    ... int[] findRootsOf ... Is there a reason why this isn't allowed? ...
    (comp.lang.java.programmer)
  • Re: How to represent the biggest number in a system
    ... biggest say int in a system? ... long int biggest = 0x7FFFFFFFFFFFFFFF? ... declaration, except that the initial value is 0x7FFFFFFFFFFFFFFF ... exists for a reason. ...
    (comp.lang.c)
  • Re: A couple of questions re. the function prototype
    ... > I read a good thread on here regarding the reason why we use function ... > prototype if the type the function returned was not an int - I was ... > would assume it was an int. ... That applies to a function *declaration*. ...
    (comp.lang.c)
  • Re: newbie (dropping numbers) new post
    ... Either use a #define or a constant int here. ... Please note that the above loop is only an example. ... entry error was detected on stdin. ... Reason number 2 for the array idea for scores. ...
    (alt.comp.lang.learn.c-cpp)