473,569 Members | 2,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

derangement: code review request

A derangement is a mapping of a set onto itself leaving no element fixed. I
realized that that was what I was programming when I was asked to randomly
determine who buys presents for whom this year in my wife's family.
Obviously, one does not buy for himself. The code is followed by some
questions.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define fam_size 20
//must be between 2 and randmax minus one

int main(void)
{
int i,t,a[fam_size],b[fam_size],notdone1,notdo ne2;
int m,j;
time_t timer;
long counter, top_num;

//determine good random numbers
srand(time(&tim er));
top_num=RAND_MA X-(RAND_MAX%fam_s ize)-1;

//initialize arrays

for (i=0;i<fam_size ;i++){
a[i]=b[i]=0;
}
//main control structures
counter=0;
notdone1=1;

while(notdone1)
{
//ignore this first while loop
//this will ultimately play with
//any given derangement to see if it's
//suitable
for(j=0;j<fam_s ize;j++)

{
notdone2=1;
while(notdone2) {

++counter;
t=rand();
m=t%fam_size;
if ((b[m]==0)&&(m!=j)&&( t<=top_num)){
a[j]=m;
b[m]=1;
notdone2=0;
}
else {
notdone2=1;
}
/* bad luck checker
starts us over except for counter*/
if ((j==fam_size-1)&&(b[j]==0)){
for (i=0;i<fam_size ;i++){
a[i]=b[i]=0;
}
notdone2=0;
j=-1;
}
//end notdone2 while
}
//end j loop
}

for(i=0;i<fam_s ize;i++)
printf("%d %d\n",i,a[i]);
printf("counter = %d\n",counter);
notdone1=0;
//end outer while
}
return 0;
}

Q1) Is this code ANSI-compliant and C99-compliant?

Q2) The obvious style shortcomings are mostly a product of my IDE looking
different than what is copy/pasted (no Mr. Mair, I am not ready to be weened
off the tit). What style shortcomings do you see that don't involve
spacing?

Q3) There's at least one major design flaw. It follows the remark "bad luck
checker" and covers the event that the final element can only be mapped to
itself, which happens 1 out of every fam_size times that the program is run.
Any ideas how to make that less hideous?

++thanks. MPJ
----------------------------------
*OT* I had been teaching my nine-month-old during feedings:

"My big eater
hates Derek Jeter"

I guess I'll have to find new material :-) *end OT*
Nov 14 '05
136 5389
On Fri, 22 Oct 2004 16:25:44 -0500, in comp.lang.c , "Merrill & Michele"
<be********@com cast.net> wrote:
I've got to figure something out for future posts. What I see and what I
post are radically different.


Your news software is converting tabs into spaces. Don't use tabs in posts
to usenet.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >
Nov 14 '05 #21
Keith Thompson <ks***@mib.or g> writes:
Tim Rentsch <tx*@alumnus.ca ltech.edu> writes:
[...]
A couple of the names use abbreviations as part of the name (eg,
'fam_size'). I recommend using the whole word, and always avoiding
abbreviations. It takes a certain amount of intestinal fortitude to
do this, as the temptation to make exceptions is strong (but don't
give in!). The more I follow this practice the more I see the
benefits. For function local variables, it's common to allow single
letters to be used as "words"; these aren't abbreviations in the
sense meant here.
That's mostly good advice, but it shouldn't be applied universally.
Some abbreviations are so obvious that it doesn't make sense to expand
them, such as "IO" rather than "input_outp ut". (And if I were writing
code that handled PCMCIA devices, I wouldn't even know how to expand
the abbreviation; all I can remember is "People Can't Memorize
Computer Industry Acronyms".)

The set of abbreviations that are sufficiently obvious depends
strongly on the domain and the expected audience. In my work, "GPT"
and "GSI" are obvious; most people who don't know what they stand for
are unlikely to want to read my code anyway.


Sorry, I should have made this more clear in my original writing. I
distinguish between using "abbreviations" , by which I mean a
shortening of a word, and using "acronyms", by which I mean a "word"
formed out of initial letters of a longer phrase. So using "tbl" for
"table" would be an abbreviation, whereas "IO" and "PCMCIA" (and
probably GPT and GSI if I knew what they stood for) would be acronyms.
Lots of acronyms are "words" for most practical purposes, and of
course some have even made their way into standard dictionaries -
sonar, radar, and now even DSL (if I'm not mistaken). Standard
acronyms often are acceptable as "words", abbreviations almost always
not. In any case the rules for how to treat abbreviations and how
to treat acronyms should treat the two cases distinctly.

Unfortunately, most people tend to err in the direction of
abbreviating too much.

HTH, HAND, YMMV.


Humorous footnote - I've seen the acronym HTH at the end of countless
articles by now, but I still don't know what it means. :)

(And yes, I'm sure I could find a meaning online if I needed to, but
somehow it just hasn't seemed important enough yet...)
Nov 14 '05 #22

[...] >Time Rentsch:
A couple of the names use abbreviations as part of the name (eg,
'fam_size'). I recommend using the whole word, and always avoiding
abbreviations. It takes a certain amount of intestinal fortitude to
do this, as the temptation to make exceptions is strong (but don't
give in!). The more I follow this practice the more I see the
benefits. For function local variables, it's common to allow single
letters to be used as "words"; these aren't abbreviations in the
sense meant here. Keith Thompson <ks***@mib.or g> writes:
That's mostly good advice, but it shouldn't be applied universally.
Some abbreviations are so obvious that it doesn't make sense to expand
them, such as "IO" rather than "input_outp ut". (And if I were writing
code that handled PCMCIA devices, I wouldn't even know how to expand
the abbreviation; all I can remember is "People Can't Memorize
Computer Industry Acronyms".)

The set of abbreviations that are sufficiently obvious depends
strongly on the domain and the expected audience. In my work, "GPT"
and "GSI" are obvious; most people who don't know what they stand for
are unlikely to want to read my code anyway.

"Tim Rentsch wrote:
Sorry, I should have made this more clear in my original writing. I
distinguish between using "abbreviations" , by which I mean a
shortening of a word, and using "acronyms", by which I mean a "word"
formed out of initial letters of a longer phrase. So using "tbl" for
"table" would be an abbreviation, whereas "IO" and "PCMCIA" (and
probably GPT and GSI if I knew what they stood for) would be acronyms.
Lots of acronyms are "words" for most practical purposes, and of
course some have even made their way into standard dictionaries -
sonar, radar, and now even DSL (if I'm not mistaken). Standard
acronyms often are acceptable as "words", abbreviations almost always
not. In any case the rules for how to treat abbreviations and how
to treat acronyms should treat the two cases distinctly.

Unfortunately, most people tend to err in the direction of
abbreviating too much.

HTH, HAND, YMMV.


Humorous footnote - I've seen the acronym HTH at the end of countless
articles by now, but I still don't know what it means. :)

(And yes, I'm sure I could find a meaning online if I needed to, but
somehow it just hasn't seemed important enough yet...)


I have been trying for a month now to figure out what OP means, and for the
sake of language clarity, I would like to know whether it is the Original
Post or the Original Poster. The former is an 'it' while the latter has the
attribute of sex (and therefore gender). If I were not substituting a
question for an answer, I would say "Happy To Help." MPJ
Nov 14 '05 #23
"Merrill & Michele" <be********@com cast.net> writes:

I have been trying for a month now to figure out what OP means, and for the
sake of language clarity, I would like to know whether it is the Original
Post or the Original Poster. The former is an 'it' while the latter has the
attribute of sex (and therefore gender). If I were not substituting a
question for an answer, I would say "Happy To Help." MPJ


OP Original Poster
HTH Hope this helps
HAND Have a nice day

For more info see
http://www.utdallas.edu/ir/tcs/techsupp/acronyms.htm
Nov 14 '05 #24
Edmund Bacon <eb****@no.wher e> writes:
OP Original Poster
HTH Hope this helps
HAND Have a nice day

For more info see
http://www.utdallas.edu/ir/tcs/techsupp/acronyms.htm ^^^^^^^^
Right! Acronyms!

"Tim Rentsch wrote:
Sorry, I should have made this more clear in my original writing. I
distinguish between using "abbreviations" , by which I mean a
shortening of a word, and using "acronyms", by which I mean a "word"
formed out of initial letters of a longer phrase. So using "tbl" for
"table" would be an abbreviation, whereas "IO" and "PCMCIA" (and
probably GPT and GSI if I knew what they stood for) would be acronyms.

Nov 14 '05 #25
Da*****@cern.ch (Dan Pop) writes:
In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes: [...]
%CC-I-ANACHRONISM, The "=-" operator is an obsolete form,
and may not be portable.

[...] I guess many other people have been bitten by that and bitterly complained
until DEC introduced this *badly* needed warning in VAX C, otherwise the
nicest pre-ANSI C compiler I have ever used.


Probably so.

Strictly speaking it's an informational message ("-I-"), which is
weaker than a warning ("-W-"), which I think makes it easier to
suppress, but that shouldn't matter unless you're foolish enough to
turn off diagnostics.

The best solution for anyone still programming on VMS^H^H^H OpenVMS is
probably to use the newer DECC rather than VAXC.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #26
Da*****@cern.ch (Dan Pop) writes:
In <Rs************ ********@comcas t.com> "Merrill & Michele"
<be********@com cast.net> writes: [...] In real world implementations , it is the value of RAND_MAX that is chosen
according to the properties of type int (among other criteria), and not
the other way round.
Agreed, with emphasis on "among other criteria". (Solaris has 32-bit
ints, but RAND_MAX==32767 .)
The standard requires both INT_MIN and RAND_MAX to be at least 32767.
However, the type of RAND_MAX need not be int, only its value must be
in the range of the type int (it is defined as an integer constant
expression).


I suppose an implementation could define RAND_MAX with a type other
than int, but I can't think of any reason to do so other than sheer
perversity. I would expect it to be an integer-constant or some
simple expression, and it has to be within the range of type int.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #27
Tim Rentsch <tx*@alumnus.ca ltech.edu> writes:
[...]
Sorry, I should have made this more clear in my original writing. I
distinguish between using "abbreviations" , by which I mean a
shortening of a word, and using "acronyms", by which I mean a "word"
formed out of initial letters of a longer phrase. So using "tbl" for
"table" would be an abbreviation, whereas "IO" and "PCMCIA" (and
probably GPT and GSI if I knew what they stood for) would be acronyms.


There are two competing definitions for the word "acronym". One
(which I prefer and consider to be more correct) says that an acronym
is an abbreviation formed from the initial letter or letters of
several words *that is pronounced as a word*. The other doesn't
require that it be pronounced as a word. (I suppose "IO" is an
ambiguous case.)

None of which affects the main point, of course.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #28

"Merrill & Michele"
[...] In real world implementations , it is the value of RAND_MAX that is chosen according to the properties of type int (among other criteria), and not
the other way round.

"Keith Thompson"
Agreed, with emphasis on "among other criteria". (Solaris has 32-bit
ints, but RAND_MAX==32767 .)
The standard requires both INT_MIN and RAND_MAX to be at least 32767.
However, the type of RAND_MAX need not be int, only its value must be
in the range of the type int (it is defined as an integer constant
expression).


I suppose an implementation could define RAND_MAX with a type other
than int, but I can't think of any reason to do so other than sheer
perversity. I would expect it to be an integer-constant or some
simple expression, and it has to be within the range of type int.


It's generally not wise to snip the question and then beg it. MPJ
Nov 14 '05 #29
Keith Thompson <ks***@mib.or g> writes:
Tim Rentsch <tx*@alumnus.ca ltech.edu> writes:
[...]
Sorry, I should have made this more clear in my original writing. I
distinguish between using "abbreviations" , by which I mean a
shortening of a word, and using "acronyms", by which I mean a "word"
formed out of initial letters of a longer phrase. So using "tbl" for
"table" would be an abbreviation, whereas "IO" and "PCMCIA" (and
probably GPT and GSI if I knew what they stood for) would be acronyms.
There are two competing definitions for the word "acronym". One
(which I prefer and consider to be more correct) says that an acronym
is an abbreviation formed from the initial letter or letters of
several words *that is pronounced as a word*. The other doesn't
require that it be pronounced as a word. (I suppose "IO" is an
ambiguous case.)


Right. Historically I think the "pronouncea ble" definition came
first. These days I think more people are used to acronym meaning any
string of initial letters that is used as a word (i.e., without regard
to whether the word is pronounceable or not); for example, I think
most people would say TLA is itself a three letter acronym. Acronyms
that are pronounced/used as regular words eventually become just
regular words and we tend to forget their acroynm-ness. "Laser" is an
example of that, or "spool" (and in fact I used the word spool for
quite a while before finding out that it is an acronym). So being an
acronym in that earlier sense eventually becomes more of an
etymological footnote than anything else.

None of which affects the main point, of course.


Roger that.
Nov 14 '05 #30

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.