From: Joerg B. <jo...@sq...> - 2000-10-20 08:02:56
|
Hi Jim ! Jim Sheetz wrote: > > Thanks Joerg, > > : Hi Jim ! > : > : As usual, there are many ways - this is just what comes to my mind: > : > : 1) Assume some realistic limit. "Screen" indicates 24 lines, so anything > : much larger might change the way the program can be used. > > That was my original plan, and probably what the exercise intended, but my > training in other languages points out, 'never trust user data', especially > when you are thinking of an arbitrary 'reasonable' number. The motto 'never trust user data' is a wise one - keep to it! I now realize this is not a problem to solve but an exercise in C programming. In that case, I would assume some fixed limit 1) depending on the way your program is to be used - if you are to draw a histogram on a 24-line terminal, any value above 24 would ruin the appearance 2) implemented via a '#define', so that it is easy to change - only in one place and check each word length against the limit: If it is larger, then simply incerement the count of "out of bounds" words and store the length in a variable "maximum_length_encountered". > > : 2) Assume some arbitrary large limit - say 1,000. > > Went there too, and really don't want to allocate an array of 1000 elements > when the incoming data might only use 5, but could conceivably use more than > 1000. What is your programming environment ? If you do that on a PC or larger, 1000 elements of 4 byte each means 4 kB, so you should not have problems. (Gates' 640 kB limit is dead ! Things would be different on a micro-controller, I admit.) > > : 3) Check the maximum possible line length of your input, > : probably words can not get larger than a single line. > > The input is coming from a file. It could be thousands of characters in one > large word. Depends on the way you handle the input - if you use line-by-line functions, these will impose a limit. > > : 4) Define the array of unknown size, use "malloc" to get storage > : for some initial size (100 ?). > : Access it via the pointer returned by "malloc". > : If you encounter a word that is longer than the currently valid > : array definition, use "realloc" to grow it. > > This one is promising, but way out of my league at the moment. I don't start > using malloc for another 5 chapters. In C without 'malloc', AFAIK there is no way of dynamically extending an array (or any other structure). The tree proposed in some other answer would probably also require 'malloc'. > > : 4) Use Perl or any other language that has dynamically growing arrays. > > I assume you meant this to be 5) :-) > It's tempting, but the point of these exercises is to learn C.... Yes - I inserted the new 4 later and forgot to renumber. As this is C programming, the approach can not be used - I did not know that in my first answer. (Off topic: I came to appreciate Perl's dynamically growing structures, several tasks get a lot easier that way.) > > : Waiting for the other proposals ... > : Joerg Bruehe > > Also waiting for other proposals, but beginning to adjust to the fact that > arrays just don't behave the way I expected in C. What did you expect ? I do not know any compiled language where arrays can grow dynamically (except by programmed re-sizing using dynamically allocated storage, the 'realloc' approach above). All in all, this turns to a general C question, so a C language group and its FAQ might have further hints. Regards, Joerg Bruehe -- Joerg Bruehe, SQL Datenbanksysteme GmbH, Berlin, Germany (speaking only for himself) mailto: jo...@sq... |