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?

What is the structure of teammates?

The teammates was just an illustration. I'm actually making a game that has eight different kind of spaces represented by an unsigned integer.

struct space {
	unsigned int piece:3;
	/* Three bits for eight different marks
	 * 000 | 0 | V - Vacuum
	 * 001 | 1 | S - Start
	 * 010 | 2 | D - Death
	 * 011 | 3 | R - Shifting Right
	 * 100 | 4 | L - Shifting Left
	 * 101 | 5 | B - Back to Start
	 * 110 | 6 | T - Transport
	 * 111 | 7 | F - Finish
	 */
};

I don't think you can use the *(array_ptr + k) syntax. I think you will have to use array_ptr[k]->piece syntax instead.

I don't think you can use the *(array_ptr + k) syntax. I think you will have to use array_ptr[k]->piece syntax instead.

That makes much more sense. Thank you.

Bye the way, I edited and apparently added a couple of bugs with my first algorithm. Sorry. This version should work:

// If this doesn't work, then shoot me.
// I commented out the stuff I used for debugging.
// Could come in handy though so it's still there.

void shuffle(int *array_ptr, int min, int max, int def) {
	int i = min;
	int temp;
	int len = max - min + 1;
	srand((unsigned)time(0)); // set an srand
	//std::cout << "\nRANDOMIZING\n";
	while (i <= max) { // while not done shuffling
		temp = rand() % len; // get a random integer between 0 and length
		//std::cout << "Random Position: " << temp << '\n';
		if (*(array_ptr + temp) == def) { // if nothing in the element yet
			// put i into element
			//std::cout << "Empty!\n";
			*(array_ptr + temp) = i;
			++i; // incrament i
		}/* else {
			std::cout << "Occupied.\n";
		}*/
		//I need it to sleep apparently so I don't get repeated values.
		sleep(rand() % 20 + 90);
	}
	/*std::cout << "\nDONE!\n";
	for (i = 0; i < len; ++i) {
		std::cout << "Element " << i << ": " << *(array_ptr + i) << '\n';
	}*/
}

I am using the wrong syntax for pointers. I totally forgot about the -> operator. Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.