<< < 3 4 5 6 7 8 9 10 11 12 13 > >>   Sort: Rank

Now I am trying to sort an array of structures with qsort ...
Now I'm trying to sort an array of structures with qsort. My comparison function takes pointers to structures, but the compiler complains that the function is of the wrong type for qsort. How can I cast the function pointer to shut off the warning? The conversions must be in the comparison function,...
2015-08-10, 1193👍, 0💬

How can I sort a linked list?
How can I sort a linked list? Sometimes it's easier to keep the list in order as you build it (or perhaps to use a tree instead). Algorithms like insertion sort and merge sort lend themselves ideally to use with linked lists. If you want to use a standard library function, you can allocate a tempora...
2015-08-10, 1096👍, 0💬

How can I sort more data than will fit in memory?
How can I sort more data than will fit in memory? You want an ``external sort,'' which you can read about in Knuth, Volume 3. The basic idea is to sort the data in chunks (as much as will fit in memory at one time), write each sorted chunk to a temporary file, and then merge the files. Your operatin...
2015-08-07, 1062👍, 0💬

How can I get the current date or time of day in a C program?
How can I get the current date or time of day in a C program? Just use the time, ctime, localtime and/or strftime functions. Here is a simple example: #include &lt;stdio.h> #include &lt;time.h> int main() { time_t now; time(&amp;now); printf("It's %s", ctime(&amp;now)); return 0; } C...
2015-08-07, 1039👍, 0💬

I know that the library function localtime will convert ...
I know that the library function localtime will convert a time_t into a broken-down struct tm, and that ctime will convert a time_t to a printable string. How can I perform the inverse operations of converting a struct tm or a string into a time_t? ANSI C specifies a library function, mktime, which ...
2015-08-05, 1008👍, 0💬

How can I add N days to a date? How can I find the difference between two dates?
How can I add N days to a date? How can I find the difference between two dates? The ANSI/ISO Standard C mktime and difftime functions provide some (limited) support for both problems. mktime accepts non-normalized dates, so it is straightforward to take a filled-in struct tm, add or subtract from t...
2015-08-05, 1047👍, 0💬

Did C have any Year 2000 problems?
Did C have any Year 2000 problems? No, although poorly-written C programs might have. The tm_year field of struct tm holds the value of the year minus 1900; this field therefore contains the value 100 for the year 2000. Code that uses tm_year correctly (by adding or subtracting 1900 when converting ...
2015-08-03, 1112👍, 0💬

I need a random number generator.
I need a random number generator. The Standard C library has one: rand. The implementation on your system may not be perfect, but writing a better one isn't necessarily easy, either. Here is a portable C implementation of the ``minimal standard'' generator : #define a 16807 #define m 2147483647 #def...
2015-08-03, 1574👍, 0💬

How can I get random integers in a certain range?
How can I get random integers in a certain range? The obvious way, rand() % N /* POOR */ (which tries to return numbers from 0 to N-1) is poor, because the low-order bits of many random number generators are distressingly non-random. A better method is something like (int)((double)rand() / ((double)...
2015-07-29, 1117👍, 0💬

Each time I run my program, I get the same sequence of numbers back from rand
Each time I run my program, I get the same sequence of numbers back from rand It's a characteristic of most pseudo-random number generators (and a defined property of the C library rand) that they always start with the same number and go through the same sequence. (Among other things, a bit of predi...
2015-07-29, 1182👍, 0💬

I need a random true/false value ...
I need a random true/false value, so I'm just taking rand() % 2, but it's alternating 0, 1, 0, 1, 0... Poor pseudorandom number generators (such as the ones unfortunately supplied with some systems) are not very random in the low-order bits. (In fact, for a pure linear congruential random number gen...
2015-07-24, 1019👍, 0💬

How can I return a sequence of random numbers which dont repeat at all?
How can I return a sequence of random numbers which dont repeat at all? What you're looking for is often called a ``random permutation'' or ``shuffle.'' One way is to initialize an array with the values to be shuffled, then randomly interchange each of the cells with another one later in the array: ...
2015-07-24, 1116👍, 0💬

How can I generate floating-point random numbers?
How can I generate floating-point random numbers? drand48 is a Unix System V routine which returns floating point random numbers (presumably with 48 bits of precision) in the half-open interval [0, 1). (Its companion seed routine is srand48; neither is in the C Standard.) It's easy to write a low-pr...
2015-07-22, 1178👍, 0💬

How can I generate random numbers with a normal or Gaussian distribution?
How can I generate random numbers with a normal or Gaussian distribution? There are a number of ways of doing this. 1. Exploit the Central Limit Theorem (``law of large numbers'') and add up several uniformly-distributed random numbers: #include &lt;stdlib.h> #include &lt;math.h> #define NSU...
2015-07-22, 1121👍, 0💬

I am porting this program, and it calls a routine drand48, which my library doesnt have. What is it?
I am porting this program, and it calls a routine drand48, which my library doesnt have. What is it? drand48 is a Unix System V routine which returns floating point random numbers (presumably with 48 bits of precision) in the half-open interval [0, 1). (Its companion seed routine is srand48; neither...
2015-07-20, 1092👍, 0💬

Is exit(status) truly equivalent to returning the same status from main?
Is exit(status) truly equivalent to returning the same status from main? Yes and no. The Standard says that a return from the initial call to main is equivalent to calling exit. However, a return from main cannot be expected to work if data local to main might be needed during cleanup; A few very ol...
2015-07-20, 1089👍, 0💬

What is the difference between memcpy and memmove?
What is the difference between memcpy and memmove? memmove offers guaranteed behavior if the memory regions pointed to by the source and destination arguments overlap. memcpy makes no such guarantee, and may therefore be more efficiently implementable. When in doubt, it's safer to use memmove. It se...
2015-07-16, 1266👍, 0💬

I am trying to port this old program. Why do I get undefined external errors for some library functions?
I am trying to port this old program. Why do I get undefined external errors for some library functions? Some old or semistandard functions have been renamed or replaced over the years; if you need:/you should instead: index use strchr. rindex use strrchr. bcopy use memmove, after interchanging the ...
2015-07-16, 1057👍, 0💬

I keep getting errors due to library functions being undefined, but I am including all the right header files.
I keep getting errors due to library functions being undefined, but I am including all the right header files. In the general case of calling code in an external library, using #include to pull in the right header file(s) is only half of the story; you also have to tell the linker to search the exte...
2015-07-14, 1127👍, 0💬

I am still getting errors due to library functions being undefined
I'm still getting errors due to library functions being undefined, even though I'm explicitly requesting the right libraries while linking. Many linkers make one pass over the list of object files and libraries you specify, and extract from libraries only those modules which satisfy references which...
2015-07-14, 1222👍, 0💬

Why is my simple program, which hardly does more than print
Why is my simple program, which hardly does more than print ``Hello, world!'' in a window, compiling to such a huge executable (several hundred K)? Should I #include fewer header files? What you're seeing is the current (poor) state of the ``art'' in library design. As run-time libraries accrete mor...
2015-07-06, 1122👍, 0💬

What does it mean when the linker says that _end is undefined?
What does it mean when the linker says that _end is undefined? That message is a quirk of the old Unix linkers. You get an error about _end being undefined only when other symbols are undefined, too--fix the others, and the error about _end will disappear.
2015-07-06, 1155👍, 0💬

My compiler is complaining that printf is undefined ...
My compiler is complaining that printf is undefined! How can this be? It's the world's most popular C function... Allegedly, there are C compilers for Microsoft Windows which do n ot support printf, on the argument that printf is for printing to old-fashioned terminals, while under Windows the right...
2015-07-03, 1128👍, 0💬

When I set a float variable to, say, 3.1, why is printf printing it as 3.0999999?
When I set a float variable to, say, 3.1, why is printf printing it as 3.0999999? Most computers use base 2 for floating-point numbers as well as for integers, and just as for base 10, not all fractions are representable exactly in base 2. It's well-known that in base 10, a fraction like 1/3 = 0.333...
2015-07-03, 1173👍, 0💬

<< < 3 4 5 6 7 8 9 10 11 12 13 > >>   Sort: Rank