Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
economizing with functions that do the same thing
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Your Uncle, post: 2470753"] About a month ago, Heathfield posted the peudosource for random permuting from TAOCP. It was all of maybe five lines. You needed to be able to do two things: be able to get a random number in a range and swap. I remembered that Dan Pop taught me to write the swap as a macro. With forum improvements, this became: #define SWAP(m, n) (tmp = (m), (m) = (n), (n) = tmp) The random number comes, again with forum improvements: int rand_in_range(int m, int n) { /*seed srand in main */ /* [m, n] is range */ int roll_again_threshold, divisor, result, tmp, offset, num_results; if (m>n) SWAP(m, n); offset = m; num_results = n - m + 1; if (num_results == 1) { return m; } roll_again_threshold = RAND_MAX - RAND_MAX%num_results; divisor = roll_again_threshold/num_results; do { result = rand(); } while (result >= roll_again_threshold); result /= divisor; return offset + result; } But then I starting thinking, and that is, of course, where the trouble began. I posted elsewhere, and I don't think you need much of a language background to get the gist of it: Ich habe zwei Funktionen bei file scope erklaert: void permute_string(char * m, int n); void permute_int(int * m, int n); Sie tun das genau Gleiche, allerdings erstere mit chars und letztere mit ints. Wie werden diese Funktionen Eine? Basically, how do I take 2 functions that differ only in the type they operate on, and make them one? I was advised to use: void permute (void *data, int n, size_t elsize); that could be called for an array a with permute(a, sizeof a / sizeof a[0], sizeof a[0]); Is this going to work? If it isn't, then the idea is mine. If it will then it's Erich Fruehstueck's. cheers, furunculus [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
economizing with functions that do the same thing
Top