That "part" is this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
void Initialize(int array[][4])
{
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
Re:
int num = rand() % 16 + 1; // random number
array[i][j] = num; // put inside array
for (int m = 0; m < 4; m++) {
for (int n = 0; n < 4; n++) {
if (num == array[m][n]) {
if (m != i && n != j) {
goto Re;
}
}
}
}
}
}
}
|
We can probably assume that the array is uninitialized before the function call.
Therefore, we don't know what values the array has.
Yet, already on the very first iteration the inner loops read value of every element.
Values that do not exist. The result is,
by definition,
undefined behaviour.
Logically you did attempt:
for each element E in array
do
num = random [1..16]
E = num
while ( any element in array other than E == num ) |
This would have been more defined
for each element E in array
repeat
num = random [1..16]
if none of currently initialized elements == num
E = num
break the repeat
|
However, when you do reach the last element, all but one valid value have already been assigned and thus:
* there is only 1/16 chance to roll the missing value
* you have to check 15 elements for clashes
In other words this approach has to do most work when correct solution would be trivial.