## RE: [Mingw-users] Another question

 RE: [Mingw-users] Another question From: Earnie Boyd - 2000-10-20 19:20:48 --- Igor Pierscinski wrote: > Hi all, > > I know this discussion is a bit out of topic, but since it > is quite interesting and practical subject I hope to be excused ;-) > You're excused. > Most solutions presented so far are something that I would > describe as simple "static" solutions to a problem of dynamic data. This situation requires simple "static" solutions because it is a training execise. > Such solutions like with a larger array and some arbitrary limits > are usually quite practical but if you are trying to learn a good > programming practice it is not the way to go. This is correct except for a classroom or textbook example where one is learning the basics. I would assume the textbook to review this example at a later date with more advanced practices. Cheers, ===== Earnie Boyd mailto:earnie_boyd@... --- ; --- --- Cygwin: POSIX on Windows ; --- --- Minimalist GNU for Windows ; --- __________________________________________________ Do You Yahoo!? Yahoo! Messenger - Talk while you surf! It's FREE. http://im.yahoo.com/

 Re: [Mingw-users] Another question From: Joerg Bruehe - 2000-10-19 17:54:09 Hi Jim ! Jim Sheetz wrote: > > OK, I've got another one. I have data coming in to a program from a file. > The program finds the length of all the words in the file, and prints a > histogram to the screen of how many words are of each length. Given that in > good programming practice, you have to assume the data can be anything, I > can't assume a length of the longest word. Here's the problem. I think the > most efficient way of dealing with this is to stuff all these numbers into > an array with length[1] being the count of words that are 1 char long. > length[2] is the count of words that are 2 chars long, etc. Until I go all > the way through the data, I don't know how long the longest word is, so I > can't declare the array because I don't know how many elements there are. > But I can't put the data in the array without declaring it first. In other > languages I've worked with, it is encouraged to declare and dimension an > array before using it, but you can add elements to an array without doing > so. I can't find any method of doing that in C. Am I just not looking far > enough, or is there a better solution? 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. 2) Assume some arbitrary large limit - say 1,000. 3) Check the maximum possible line length of your input, probably words can not get larger than a single line. 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. 4) Use Perl or any other language that has dynamically growing arrays. Waiting for the other proposals ... Joerg Bruehe -- Joerg Bruehe, SQL Datenbanksysteme GmbH, Berlin, Germany (speaking only for himself) mailto: joerg@...
 Re: [Mingw-users] Another question From: serg konovalov - 2000-10-19 21:23:38 Hi, all! Hi, Jim! >... >I can't assume a length of the longest word... No problem! Try to use RedBlackTrees ([Thomas Niemann. Sorting and Searching Algorithms:A Cookbook] c-sources there: http://odin.ee.uwa.edu.au/~morris/Year2/PLDS210/niemann/s_man.htm) with next {_node}: ... _node { ... int word_length; /* unsigned long long, may be? */ int word_counter; ... }; int curr_word_length; /* unsigned long long, may be? */ ... do {/******************************************************************/ ... curr_word_length=... if ((ptr_curr_node=find_node (curr_word_length, root)) == NULL) { ptr_curr_node=insert_node (curr_word_length, root); ptr_curr_node->word_counter=1; } else { ptr_curr_node->word_counter++; } ... }/******************************************************************/ while (!EOF); /* write obxod_tree () yourself */ obxod_tree (root); /* count nodes and get sorted list there... */ print_histogram (...); ... wbr, abn
 RE: [Mingw-users] Another question From: Igor Pierscinski - 2000-10-20 09:47:29 Hi all, I know this discussion is a bit out of topic, but since it is quite interesting and practical subject I hope to be excused ;-) Most solutions presented so far are something that I would describe as simple "static" solutions to a problem of dynamic data. Such solutions like with a larger array and some arbitrary limits are usually quite practical but if you are trying to learn a good programming practice it is not the way to go. The correect solution to your problem is to use a dynamic data structure. In this case the simpliest solution that I can think of (if you do not care about efficiency) is a single linkrd list. A more advanced and efficient solution would be some sort of a tree (I would think of AVL tree). If you are interested read classical "Algorithms + Data Structures = Programs" by N. Wirth - it has very good and simple to read overview of simple dynamic data structures. But in this case you will not escape malloc and pointers ;-) . Regards to all Igor -----Original Message----- From: Jim Sheetz [mailto:HeyJim@...] Sent: Thursday, October 19, 2000 11:08 PM To: MinGW Mailing List Subject: Re: [Mingw-users] Another question 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. : 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. : 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. : 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. : 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.... : 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. Jim. _______________________________________________ MinGW-users mailing list MinGW-users@... You may change your MinGW Account Options at: http://lists.sourceforge.net/mailman/listinfo/mingw-users
 RE: [Mingw-users] Another question From: Earnie Boyd - 2000-10-20 19:20:48 --- Igor Pierscinski wrote: > Hi all, > > I know this discussion is a bit out of topic, but since it > is quite interesting and practical subject I hope to be excused ;-) > You're excused. > Most solutions presented so far are something that I would > describe as simple "static" solutions to a problem of dynamic data. This situation requires simple "static" solutions because it is a training execise. > Such solutions like with a larger array and some arbitrary limits > are usually quite practical but if you are trying to learn a good > programming practice it is not the way to go. This is correct except for a classroom or textbook example where one is learning the basics. I would assume the textbook to review this example at a later date with more advanced practices. Cheers, ===== Earnie Boyd mailto:earnie_boyd@... --- ; --- --- Cygwin: POSIX on Windows ; --- --- Minimalist GNU for Windows ; --- __________________________________________________ Do You Yahoo!? Yahoo! Messenger - Talk while you surf! It's FREE. http://im.yahoo.com/
 Re: [Mingw-users] Another question From: Paul Garceau - 2001-01-05 02:34:14 Hi folks, On 4 Jan 2001, at 13:56, the Illustrious Kittenberger, Axel wrote: > Okay 'nother question. > > Beside that it seems I'm not being able to compile the runtime > myself. Theoretically the precompiled windows runtime should work > just as fine for me. > > However my gcc keeps claiming about that it searches for a crt1.o > however in the runtime I downloaded from sourceforge only a > crt2.o exists... Which runtime are you talking about? There's actually several different versions there. The latest version (12/4/2000) of Mingw runtime has both crt1.o and crt2.o. Peace, Paul G. Nothing real can be threatened. Nothing unreal exists.
 Re: [Mingw-users] Another question From: Jim Sheetz - 2000-10-19 21:08:20 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. : 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. : 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. : 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. : 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.... : 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. Jim.
 Re: [Mingw-users] Another question From: Franco Bez - 2000-10-20 03:38:00 If You consider C++ not plain C, the best solution is to use a STL map. // ****************************************** #include #include using namespace std; // map is a hashtable that hold a pair of values // key and value. use the key for the length and the // value for the count typedef map mymap; int main (){ mymap themap; mymap::iterator it; // ...... // Open file etc... while (1){ // ..... // determine new word length it=themap.find( newlen ); if(it != themap.end() ){ ++(it->second); // increment count } else { themap[newlen] = 1; // insert a new entry } // break if EOF // .... } // report output for ( it=themap.begin() ; it != themap.end() ; ++it){ cout << it->second << " Words with " cout << it->first << " characters length" << endl; } return 0; } // ****************************************** Ciao, Franco
 Re: [Mingw-users] Another question From: Joerg Bruehe - 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: joerg@...
 Re: [Mingw-users] Another question From: Bruce Dodson - 2000-10-23 16:59:56 When building a program, think about delivering value, and aim to do things the easy way. I would count the number of words of each length up to some arbitrary size (e.g. 100 letters), and also count how many words are longer than that arbitrary size (e.g. count with >100 letters). This lets you do it in a single pass, it probably tells the user everything they want to know, and it still does a reasonable thing even when passed unreasonable data. ----- Original Message ----- From: "Jim Sheetz" To: "MinGW Mailing List" Sent: Thursday, October 19, 2000 6:08 PM Subject: Re: [Mingw-users] Another question > 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. > > : 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. > > : 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. > > : 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. > > : 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.... > > : 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. > > Jim. > > > _______________________________________________ > MinGW-users mailing list > MinGW-users@... > > You may change your MinGW Account Options at: > http://lists.sourceforge.net/mailman/listinfo/mingw-users >