我很难从课本中理解这个代码示例。我希望有人能比我的书更好地解释这段代码的一部分。我很抱歉在这个节目中没有评论,所以我会尽我最大的努力在我的要求非常具体。好的,有标题表明这个程序是一个“随机排列生成”。我在代码中感到困惑的地方是在名为bldPerm()的函数中
函数bldPerm():

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

    for (int i = 0 ; i < ARY_SIZE; i++)
    {
        do
        {
            oneRandno = rand() % ARY_SIZE;
        } while (haveRand[oneRandno] == 1);
        haveRand[oneRandno] = 1;
        randNos[i] = oneRandno;
    }
    return;
}

我不明白do while循环如何以及为什么通过将数组与1进行比较来检查数组。那么让我困惑的是while(haveRand[oneRandno]) == 1;被设置为该元素中的一个。然后将元素“1”设置为haveRand[oneRandno] = 1;并返回除1之外的其他数字。这个程序在我的书中运行,并打印出其他数字然后1,但我只是看不到它是如何工作的。我一直在试图扭曲我的头围绕这一点,我相信这是一件简单的事情。。。但我不明白。所以我的问题是,有谁能详细解释这个函数中发生了什么以及它是如何工作的?
完整代码:
#define _CRT_SECURE_NO_WARNINGS
#define ARY_SIZE 20
#include <stdio.h>
#include <stdlib.h>

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

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

    printf("Begin Random Permutation Generation\n");
    bldPerm(randNos);
    printData(randNos, ARY_SIZE, 10);

    return 0;
}


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

    for (int i = 0 ; i < ARY_SIZE; i++)
    {
        do
        {
            oneRandno = rand() % ARY_SIZE;
        } while (haveRand[oneRandno] == 1);
        haveRand[oneRandno] = 1;
        randNos[i] = oneRandno;
    }
    return;
}
void printData(int data[], int size, int linesSize)
{
    int numPrinted = 0;

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

最佳答案

我浏览了代码,并用代码写了一些评论,希望能澄清这一点。

void bldPerm(int randNos[]) // here you give an array of random numbers that is empty
{
    // declare an int to store the randomly generated number
    int oneRandno;

    // initialize an array to store random numbers of size ARY_SIZE
    int haveRand[ARY_SIZE] = { 0 };

    // now loop while i is smaller then ARY_SIZE and increment i
    for (int i = 0 ; i < ARY_SIZE; i++)
    {
        // here do generate the random number
        do
        {
            // oneRandno will ALWAYS be between -1 and ARY_SIZE
            // or i.o.w. from 0 to ARY_SIZE - 1
            oneRandno = rand() % ARY_SIZE;
        }
        // because the numbers have to be unique, if oneRandno
        // was already generated loop again
        while (haveRand[oneRandno] == 1);

        // Set to 1 because the next iteration oneRandno could have the same value,
        // however very unlikely because rand() has a period of at least 2^32 which
        // gives you enough room to have it generate a whole lot of numbers before
        // it generates the same number again. So basically the while loop will
        // never loop and act like a glorified if statement.
        // and that the values in randNos should unique, if oneRandno has the
        // same value the do while loop will iterate once more
        // and generate another oneRandno
        haveRand[oneRandno] = 1;

        // finally store the uniquely generated number
        randNos[i] = oneRandno;

        //go to the next iteration
    }
    return;
}

关于c - 随机排列生成,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22874434/

10-11 00:43