M
Merrill & Michele
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,notdone2;
int m,j;
time_t timer;
long counter, top_num;
//determine good random numbers
srand(time(&timer));
top_num=RAND_MAX-(RAND_MAX%fam_size)-1;
//initialize arrays
for (i=0;i<fam_size;i++){
a=b=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_size;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=b=0;
}
notdone2=0;
j=-1;
}
//end notdone2 while
}
//end j loop
}
for(i=0;i<fam_size;i++)
printf("%d %d\n",i,a);
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*
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,notdone2;
int m,j;
time_t timer;
long counter, top_num;
//determine good random numbers
srand(time(&timer));
top_num=RAND_MAX-(RAND_MAX%fam_size)-1;
//initialize arrays
for (i=0;i<fam_size;i++){
a=b=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_size;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=b=0;
}
notdone2=0;
j=-1;
}
//end notdone2 while
}
//end j loop
}
for(i=0;i<fam_size;i++)
printf("%d %d\n",i,a);
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*