--------------- FIDO MESSAGE AREA==> TOPIC: 239 C LANGUAGE Ref: F5G00180 Date: 04/23/98 From: KALLE SOIHA Time: 04:25pm \/To: TOM TORFS (Read 2 times) Subj: free'ing memory >> The return value of fopen() is actually the return value of the >> malloc() that fopen() calls to allocate the space for the FILE >> struct. > There's nothing that says so. FILE needn't even be a struct. Yeah. Know that now. Lesson: don't try to be smart in things you know only a little of. :-) --- BBBS/LiI v3.42 ToMmIk-4v * Origin: Usagi Tsukino Worship Cult (2:222/222.220) --------------- FIDO MESSAGE AREA==> TOPIC: 239 C LANGUAGE Ref: F5G00181 Date: 04/25/98 From: JANI SAKSA Time: 07:25pm \/To: CAREY BLOODWORTH (Read 2 times) Subj: Re: Read keyboard OS/2 -=> Propably Carey Bloodworth wrote to Alexis Tcach <=- Hello Carey! AT> Does somebody know how to read 2 (or more) keys from the keyboard at the AT>same time ??? Thanks !! CB> CB> That would depend on the system. CB> CB> On some computers, you can directly read the keyboard. CB> CB> However, on most, including the PC, you can only read one key at a CB> time, unless it's one of the control keys, such as shift, ctrl, alt, CB> etc. And unless you program your own keyboard controller! I've done it, and it's pretty easy job... In modern Operating Systems you don't even have to do that. In Win95 and OS/2 there are more elegant ways of doing that. CB> Under the PC (dos, win, os/2, linux, etc.), that's the best you can CB> do. Wan't my piece of code? :) Works just fine for me :) // Sir Robin (jsaksa79@hotmail.com), DooM 2 Master \\ Sir Robin's DooM Castle: http://www.kolumbus.fi/jani.saksa/doom/ // Falling Star Productions: http://www.kolumbus.fi/jani.saksa/fstar/ --- BBBS/2 v3.42 ToMmIk-3v * Origin: BCG-Box 4 (2:222/0) --------------- FIDO MESSAGE AREA==> TOPIC: 239 C LANGUAGE Ref: F5G00182 Date: 04/24/98 From: DAVID NOON Time: 08:50pm \/To: JONATHAN DE BOYNE POLLAR (Read 2 times) Subj: Insertion Or Bubble? In a message dated 04-08-98, Jonathan de Boyne Pollard said to David Noon about Insertion Or Bubble? Hi Jonathan, JP>It has to be said that coding these things in C++ is a lot easier than JP>coding them in C. Agreed. I have always preferred C++ to C, even in CFRONT days. JP>The implementation of qsort() in my standard JP>library uses Quicker Sort, and the machinations that are JP>made necessary by the way that qsort() is passed the data JP>to be sorted and the comparison function obscure the JP>underlying algorithm quite significantly. Tell me about it! I've been going through Ray Gardner's code for Quicksort. I'm glad I wrote my own first. Living with C's limitations and compatibility with the woeful qsort() calling interface combine to be a real pain. The templates I've been posting in C++ have been simplicity itself in comparison [bad pun]. However, the complication arises in suitably overloading to the < and > operators. [But we knew the job was dangerous when we took it. ... :-) ] Regards Dave ___ * MR/2 2.25 #353 * If I only had a nickel for every time Windows crashed ... --- Maximus/2 3.01 * Origin: DoNoR/2,Woking UK (44-1483-717905) (2:440/4) --------------- FIDO MESSAGE AREA==> TOPIC: 239 C LANGUAGE Ref: F5G00183 Date: 04/24/98 From: DAVID NOON Time: 09:28pm \/To: JONATHAN DE BOYNE POLLAR (Read 2 times) Subj: Aaargh! Bubble Sort *Ag In a message dated 04-08-98, Jonathan de Boyne Pollard said to George White about Aaargh! Bubble Sort *aga Hi Jonathan, [snip] JP>Tom's code is, in fact, closer to the JP>"classic" Bubble Sort that most people think of, As we have seen, what most people think is a bubble sort is nothing of the sort [sorry]. JP>since it JP>processes the array in the same direction as the final JP>intended ordering. This does not a bubble sort make. JP>Given that David has previously bemoaned the fact that all that anyone JP>seems to do in this echo is post Bubble Sort, I find it ironic JP>and amusing that neither you nor he can actually recognise JP>it when presented with it. Perhaps echo participants JP>should be encouraged to post Bubble Sort *more often*, so JP>that you two can sharpen your recognition skills. (-: After you've read page 100 of "Algorithms" by Sedgewick, read the following tagline. ... :-) Regards Dave ___ * MR/2 2.25 #353 * Go straight to the docs. Do not pass GO. Do not collect $200! --- Maximus/2 3.01 * Origin: DoNoR/2,Woking UK (44-1483-717905) (2:440/4) --------------- FIDO MESSAGE AREA==> TOPIC: 239 C LANGUAGE Ref: F5G00184 Date: 04/24/98 From: DAVID NOON Time: 09:48pm \/To: JONATHAN DE BOYNE POLLAR (Read 2 times) Subj: Bubble Becomes Zeppelin In a message dated 04-08-98, Jonathan de Boyne Pollard said to David Noon about Bubble Becomes Zeppelin Hi Jonathan, TT>> for (i1=0; i1> for (i2=i1; i2> if (elem[i1]>elem[i2]) { TT>> tmp = elem[i1]; TT>> elem[i1] = elem[i2]; TT>> elem[i2] = tmp; TT>> } TT>> } TT>> } JP>That *is* genine Bubble Sort. The only difference between JP>that and the code that you are used to (which you posted) JP>is the direction in which the array is processed. No, it is a selection sort, with a variant of [unnecessarily] exchanging prior to completing the current selection. A bubble sort only ever compares adjacent elements. See pages 100-101 of Sedgewick's "Algorithms"; he expressly uses the word "adjacent" on page 100. Tom's code anchors the left comparand at i1 and iterates through the array rightwards using i2. The only time adjacent elements are compared is each second iteration of the inner loop, when i2 == i1 + 1. All other comparisons are of non-adjacent elements. The code I posted compares array[j-1] and array[j], which are always adjacent. JP>Look again. I did, and while wearing my contact lenses. ... :-) Regards Dave ___ * MR/2 2.25 #353 * Jogging made my beer too frothy. --- Maximus/2 3.01 * Origin: DoNoR/2,Woking UK (44-1483-717905) (2:440/4) --------------- FIDO MESSAGE AREA==> TOPIC: 239 C LANGUAGE Ref: F5G00185 Date: 04/23/98 From: KEITH WHITELEY Time: 05:11pm \/To: ALL (Read 2 times) Subj: 64 bit integers Hi, I am working on a program that I use (unsigned long) numbers on and the numbers are getting to big to work with. Without resorting to the use of floating point can someone tell me how to work with integers that are 64 bits long in borland c++ 3.1 ??? I have noticed in some code samples I have found that there is __64bit data types. How could I add this to my compiler ? Regards Keith. ... All I need is a Wave and a board to surf it on. --- Blue Wave v2.12 * Origin: Kneptune BBS (+44-161-777-8088) (2:250/109) --------------- FIDO MESSAGE AREA==> TOPIC: 239 C LANGUAGE Ref: F5G00186 Date: 04/24/98 From: GEORGE WHITE Time: 10:01pm \/To: JONATHAN DE BOYNE POLLAR (Read 2 times) Subj: Aaargh! Bubble Sort *ag Hi Jonathan, You wrote to me: JDBP> DN>>> for (i = n - 1; i > 0; --i) JDBP> DN>>> for (j = 1; j <= i; ++j) JDBP> DN>>> if (array[j-1] > array[j]) { /* swap j-1 with j */ } JDBP> TT>> for (i1=0; i1 TT>> for (i2=i1; i2 TT>> if (elem[i1]>elem[i2]) { /* swap i2 with i1 */ } JDBP> GW> It's not a "bubble sort", [...] JDBP>Yes it is. I've elided the exchange code in both Tom's and David's ode, JDBP>which should make it clearer that they are, in essence, the JDBP>same thing, merely processing the array in different JDBP>directions. Tom's code is, in fact, closer to the JDBP>"classic" Bubble Sort that most people think of, since it JDBP>processes the array in the same direction as the final JDBP>intended ordering. No they are _NOT_!!! David's bubble sort code compares an element with its neighbour, and swaps them if they are mis-ordered, which is the way a bubble sort traditionally works. The code then keeps makeing passes through the code, and knowing that the last search put the extreem element at the end of the search can reduce the search by one each time, until there are no exchanges in a search (or, worst case, the end is at the start) when you are finished. In a traditional bubble sort, elements sink down slowly, but rise up quickly. If early exit on no exchanges has been used a modification to process in alternate directions is sometimes used to reduce the total number of passes needed and hence the number of comparisons. It does not change the number of exchanges needed and is beneficial where there is relatively minor mis-ordering. Tom's code searches for elements in turn from one end of the array, looking through the unsorted part of the array to find the next required element to add to the end of the already sorted part, it is a selection for the next element. There is no possible early exit (it always requires 1/2 (n^2 - n) comparisons if you remove his redundant checks). The operation of Tom's code can be speeded up considerably by not exchanging the data each time a prospective new next sorted element is found, but by just saving the offset of the next prospective element, and doing the exchange at the end of the pass. JDBP>Given that David has previously bemoaned the fact that all that anyone see JDBP>to do in this echo is post Bubble Sort, I find it ironic JDBP>and amusing that neither you nor he can actually recognise JDBP>it when presented with it. Perhaps echo participants JDBP>should be encouraged to post Bubble Sort *more often*, so JDBP>that you two can sharpen your recognition skills. (-: But we can... "Straight insertion" (see Knuth Vol 3, page 81), "bubble" (Knuth page 107) and "straight selection" (Knuth page 139) sorts _all_ share the same basic loop structures above. For a sort to be a "bubble sort" it has to compare _ajacent_ pairs of elements, and exchange them as required (it also leaves the array more ordered after each pass). If it does anything else it becomes a different type of sort. So Tom's code is a "selection sort", it selects the next element required from those remaining (and will normally leave the array more _disordered_ after the early passes). George * SLMR 2.1a * Wastebasket: Something to throw things near. --- Maximus/2 3.01 * Origin: DoNoR/2,Woking UK (44-1483-717905) (2:440/4) --------------- FIDO MESSAGE AREA==> TOPIC: 239 C LANGUAGE Ref: F5G00187 Date: 04/24/98 From: GEORGE WHITE Time: 07:24am \/To: JONATHAN DE BOYNE POLLAR (Read 2 times) Subj: Select, Insert, Bubble! Hi Jonathan, JDBP> GW> I've put together a C test harness for timing sorts, and while I allo JDBP> GW> the better sorts (quickersort, shellsort, Jonathans Combsort) [...] JDBP> GW> I've run it with the sorts from SNIPPETS, Jonathan's Combsort [...] JDBP> GW> Sorting the same data set shellsort is always faster than onathan's JDBP> GW> Combsort. JDBP>I'd just like to point out that I lay no claim to Comb JDBP>Sort, and never have. From what I recall, I obtained the JDBP>code from an article in _Byte_ magazine in the mid 1980s. The reference to your name was to indicate the fact that I used your code implementing it as posted in C_Plusplus, rather than anything else (in the same way that the other sorts tested were attributed to the SNIPPETS collection). George * SLMR 2.1a * Wastebasket: Something to throw things near. --- Maximus/2 3.01 * Origin: DoNoR/2,Woking UK (44-1483-717905) (2:440/4) --------------- FIDO MESSAGE AREA==> TOPIC: 239 C LANGUAGE Ref: F5G00188 Date: 04/25/98 From: SIMON AVERY Time: 01:17pm \/To: ROGER SCUDDER (Read 2 times) Subj: Nested loop Hecky Thump, it's Roger Arkwright! SA>> for (count=1; count!=6; count++) RS> Using a test for absolute equality or inequality for the test RS> portion of a loop is not a good idea. It's safer to always use RS> a test for "less than" or "less than or equal to" or the RS> flip side of "greater than" or "greater than or equal to". That's a good point, and one I've tried before. However, whether it was TCC or a problem in my source elsewhere, whenever I've tried that in large loops (upto 4 billion - an unsigned long) it's crashed and burned. A != /always/ hits true, yet sometimes a <= can go badly wrong. RS> It's not an idea I came up with. It's something that was passed RS> on to me by people with many years experience. The idea is also RS> backed up by countless examples in quality instructional text. Fair enough - but in my (limited, I'll grant you!) experience it doesn't work for /me/. Simon Avery ... Look, up in the sky, it's a bird, it's a plane, it's Roger. ... Tag-O-Matic V.13F Under Test... --- FMail/386 1.22 * Origin: TAGLINES_UK Get it from the backbone now! (2:255/90) --------------- FIDO MESSAGE AREA==> TOPIC: 239 C LANGUAGE Ref: F5G00189 Date: 04/25/98 From: DAVID NOON Time: 08:00pm \/To: THOMAS J. PEDERSEN (Read 2 times) Subj: Solving Linear Equations In a message dated 04-24-98, Thomas J. Pedersen said to All about Solving Linear Equations Hi Thomas, TP> I need a program to solve this set of equations: Are you planning on writing this program yourself? I shall assume you are. The reason for this assumption will be revealed further down. TP> a11*x1 + a12*x2 + a13*x3 + - - - - - - -+ a1n*xn = b1 TP> a21*x1 + a22*x2 + a23*x3 + - - - - - - -+ a3n*xn = b2 TP> a31*x1 + a32*x2 + a33*x3 + - - - - - - -+ a3n*xn = b3 TP> - - - - - - - - - TP> - - - - - - - - - TP> - - - - - - - - -- TP> am1*x1 + am2*x2 + am3*x3 + - - - - - -+ amn*xn = bn Ok, this is a conventional linear system of order m by n, unless the elements can be complex numbers, rather than real numbers. You haven't specified the domain. I shall assume real numbers. TP> 1) x = k (one solution) This will only be true for square, non-singular matrices. That is, when m equals n, and the matrix is invertible. TP> 2) x = k + t*a1 + s*a2 + r*a3 (many solutions) What do you mean by "many"? [If the answer to this question were simple, I would not have asked it.] TP> 3) x = Y (no solution) There are plenty of these, I'm afraid, and x does not equal zero. The vector x simply does not exist. Since you have posted this in both C_Echo and C_PlusPlus, in which language do you plan to code your program? Since this is not a very specific question about [maybe C/C++] programming, I shall assume that you want some design considerations for your code. You might need to ask yourself: Do I know m and n at compile time or only at run time? Do I really need to deal with over-specified and under-specified systems of equations? (I.e. when m != n) Do I need to deal with ill-conditioned systems? Since this looks like a college or university assignment [first year numerical analysis, is it?] you need to ask yourself if you are constrained to a particular class of algorithm: elimination or iterative. For the former, elimination, Gauss-Jordan is the "classic" but it is very slow. Gauss's later elimination method is the most commonly used in this class, but you might need a sound pivoting strategy to deal with ill-conditioned systems. The top-rated (for stability) elimination algorithm is based on Gram-Schmidt orthonormalization; this would be my recommended approach for a professional program that is to be used repeatedly. For the latter, iteration, Jacobi's iteration is the "classic", but can suffer convergence problems on ill-conditioned systems. The Gauss-Seidel iteration is more popular, even though it can also suffer with ill-conditioned matrices. All of these algorithms should be available in books in your school's library. It is simply a matter of coding them, with suitable storage management for unknown values of m and n. Moreover, if it is a college/uni. assignment, you will likely be expected to have written your own program. This was certainly the form when I was studying numerical analysis in the early 1970's. Now, what do you want us to do? Regards Dave ___ * MR/2 2.25 #353 * Windows:(n.)2. The Gates of Hell. --- Maximus/2 3.01 * Origin: DoNoR/2,Woking UK (44-1483-717905) (2:440/4)