How can I shuffle a vector with structures? C

Asked

Viewed 32 times

0

I need to shuffle domino pieces on a function, and I’m having trouble.

void baralhar(int domino[])
{
    srand(time(NULL));
    for (int i=1;i<=29;i++)
    {
        int r = rand() % 28;

        int aux = domino[i];
        domino[i] = domino[r];
        domino[r] = aux;
    }
}
  • Are you sure you’re using C? This variable declare structure inside the for, which I remember was not possible in C.

  • Can you clarify what’s not working?

  • This is the output I get if I run my program:[0:0] [0:1] [0:2] [0:3] [0:4] [0:5] [0:6] [1:1] [1:2] [1:3] [1:4] [1:5] [1:6] [2:2] [2:3] [2:4] [2:5] [2:6] [3:3] [3:4] [3:5] [3 [6:2] [6:3] [2:4] [2:0] [1:0] [2:3] [2:4] [2:5] [2:6] [3:3] [3:4] [3:5] [3:6] [4:4] [4:5] [4:6] [5:5] [5:6] [6:6] and should receive the first 28 numbers in order and the second 28 numbers should be Random numbers yet a strange number appears in the middle of the second numbers and I do not know what it is.

  • The size of your "domain" vector is 28? If it is, its is cannot go up to 29. It should go from 0 to 27. That is, it is (int i = 0; i < 28; i++). If so, let me know, I will publish an answer explaining better why these "odd numbers".

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.