lundi 21 janvier 2019

I am new to programming and I cant seem to figure out how to check to make sure there are no repeats when generating 6 random numbers?

I can get the random numbers into an array but I can seem to figure out how to check to make sure that they aren't repeating. I print out the code but there are no numbers in the array (prints out nothing). Please help. Thanks in advance.

            //puts random numbers into an array
            i = 0, j = 0;
            srand(time(NULL));
            for (i = 0; i < arrSize; i++)
            {
                    randArr[i] = randNums(1,50);
            }

            i = 0;
            for(i = 0; i < arrSize; i++)
            {
                    printf("%d ", randArr[i]);
            }

            printf("\n\n");

            //checks to make sure there are no duplicates
            i = 0, j = 0, k = 0, temp = 0;
            for (i = 0; i < arrSize; i++)
            {
                    for (j = 1; j <= arrSize;)
                    {
                            if (randArr[j] == randArr[i])
                            {
                                    for (k = j; k <= arrSize; k++)
                                    {
                                            temp = randNums(1,50);
                                            randArr[k + 1] = temp;
                                    }
                            arrSize--;
                            }
                            else
                            j++;
                    }
            }


//generates random numbers between the inputed max and min
int randNums(int min, int max)
{
        int result = 0, low = 0, high = 0;
        if (min < max)
        {
                low = min;
                high = max + 1;
        }
        else
        {
                low = max + 1;
                high = min;
        }


    result = (rand() % (high - low)) + low;

    return (result);
}




Aucun commentaire:

Enregistrer un commentaire