Samstag, 10. Dezember 2011

C program : Generate a random number permutation

// rand_per,.c
//generate a randon number permutation

#include
#include

#define ARY_SIZE 10

void bldPerm (int randNos[]);
void printData (int data[], int size, int lineSize);

int main(void)
{
int randNos[ARY_SIZE];

printf("Begin Random Permutation\n");

bldPerm(randNos);
printData(randNos, ARY_SIZE, 10);

return 0;

}

//

void bldPerm (int randNos[])
{
int oneRandNo;
int haveRand[ARY_SIZE] = {0};
int i ;

for (i=0; i {
do {
oneRandNo = rand() % ARY_SIZE;
// haveRand[oneRandNo] =1;
printf("%d %d\n", oneRandNo, haveRand[oneRandNo]);
//randNos[i] = oneRandNo;
} while (haveRand[oneRandNo] ==1);
printf("---\n");

haveRand[oneRandNo] =1;
randNos[i] = oneRandNo;
}

return;
}


void printData (int data[], int size, int lineSize)
{

int numPrinted =0;

printf("\n");
for (int i=0; i< size; i++)
{
numPrinted++;
printf("%2d ", data[i]);
if (numPrinted >= lineSize)
{

printf("\n");
numPrinted =0;
}

} // for

printf("\n");
return;

}




Keine Kommentare: