You created a bunch of random numbers, but say you don't want a bunch of random numbers exactly. Say you have ten members of a team and you want to give them a random position represented by 0 to 9. So you create your array and you fill it with a random number after checking and seeing if the random number is taken.
This program places each person into a random place. It makes more sence because you know you are going to put Larry, Curly, Moe to work... it's just a question of where!
So I used pointers and I came up with this which has many applications. Hopefully someone will find it useful.
void shuffle(int *array_ptr, int len, int def, int min) {
int i = 0;
int temp;
for (i = min; i < len; ++i) {
*(array_ptr + i) = def; // 'zero' the array
}
srand((unsigned)time(0)); // set an srand
while (i < len) { // while not done shuffling
temp = rand() % len + min; // get a random integer between min and len
if (*(array_ptr + temp) == def) { // if nothing in the element yet
// put i into element
*(array_ptr + temp) = i;
++i; // incrament i
}
}
}
BUT, say instead of shuffling integers I REALLY DO want to place Larry, Curly, and Moe in their places? Instead of an array of integers, I use an array of people. This is where I'm stuck.
void shuffle(teamMates *array_ptr, int len, int def, int min) {
int i = 0;
int temp;
for (i = min; i < len; ++i) {
*(array_ptr + i) = def; // 'zero' the array
}
srand((unsigned)time(0)); // set an srand
while (i < len) { // while not done shuffling
temp = rand() % len + min; // get a random integer between min and len
if (*(array_ptr + temp) == def) { // if nothing in the element yet
// put i into element
*(array_ptr + temp) = i;
++i; // incrament i
}
}
}
Generates Errors:
error C2676: binary '==' : 'struct teamMates' does not define this operator or a conversion to a type acceptable to the predefined operator
error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
Does anyone know how to convert this stuff or what is the next step? Do I need to overload operators?