Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I want to make this program more quickly

I was made this program
This program makes array which each digit is different
but it takes so much time

void generateArr() {
    int randomNum;
    int sameNum = 0;
    for (int i = 0; i < arrScale; i++) {
        while (true)
        {
            sameNum = 0;
            srand((unsigned int)time(NULL));
            randomNum = rand() % 10;
            for (int j = 0; j < arrScale; j++) {
                if (numArr[j] == randomNum) {
                    sameNum = 1;
                }
            }
            if (sameNum == 0) {
                break;
            }
        }
        numArr[i] = randomNum;
    }
}

numArr and arrScale is made in front

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can have a separate buffer t of size 10 and whenever you draw x, set t[x] to 1. So you can evaluate t[x] == 1 to check if x is already in your array and don’t have to loop over it.

Note that your program won’t work if your array has size greater than 10.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading