943,865 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2787
  • C++ RSS
Nov 23rd, 2005
0

Automatic Shuffle Master

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  1.  
  2. void shuffle(int *array_ptr, int len, int def, int min) {
  3. int i = 0;
  4. int temp;
  5. for (i = min; i < len; ++i) {
  6. *(array_ptr + i) = def; // 'zero' the array
  7. }
  8. srand((unsigned)time(0)); // set an srand
  9. while (i < len) { // while not done shuffling
  10. temp = rand() % len + min; // get a random integer between min and len
  11. if (*(array_ptr + temp) == def) { // if nothing in the element yet
  12. // put i into element
  13. *(array_ptr + temp) = i;
  14. ++i; // incrament i
  15. }
  16. }
  17. }

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.

C++ Syntax (Toggle Plain Text)
  1.  
  2. void shuffle(teamMates *array_ptr, int len, int def, int min) {
  3. int i = 0;
  4. int temp;
  5. for (i = min; i < len; ++i) {
  6. *(array_ptr + i) = def; // 'zero' the array
  7. }
  8. srand((unsigned)time(0)); // set an srand
  9. while (i < len) { // while not done shuffling
  10. temp = rand() % len + min; // get a random integer between min and len
  11. if (*(array_ptr + temp) == def) { // if nothing in the element yet
  12. // put i into element
  13. *(array_ptr + temp) = i;
  14. ++i; // incrament i
  15. }
  16. }
  17. }

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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pokerponcho is offline Offline
10 posts
since Nov 2005
Nov 23rd, 2005
0

Re: Automatic Shuffle Master

What is the structure of teammates?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
HyperEngineer is offline Offline
16 posts
since Apr 2005
Nov 23rd, 2005
0

Re: Automatic Shuffle Master

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

C++ Syntax (Toggle Plain Text)
  1.  
  2. struct space {
  3. unsigned int piece:3;
  4. /* Three bits for eight different marks
  5. * 000 | 0 | V - Vacuum
  6. * 001 | 1 | S - Start
  7. * 010 | 2 | D - Death
  8. * 011 | 3 | R - Shifting Right
  9. * 100 | 4 | L - Shifting Left
  10. * 101 | 5 | B - Back to Start
  11. * 110 | 6 | T - Transport
  12. * 111 | 7 | F - Finish
  13. */
  14. };
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pokerponcho is offline Offline
10 posts
since Nov 2005
Nov 23rd, 2005
0

Re: Automatic Shuffle Master

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
HyperEngineer is offline Offline
16 posts
since Apr 2005
Nov 23rd, 2005
0

Re: Automatic Shuffle Master

Quote originally posted by HyperEngineer ...
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:

C++ Syntax (Toggle Plain Text)
  1.  
  2. // If this doesn't work, then shoot me.
  3. // I commented out the stuff I used for debugging.
  4. // Could come in handy though so it's still there.
  5.  
  6. void shuffle(int *array_ptr, int min, int max, int def) {
  7. int i = min;
  8. int temp;
  9. int len = max - min + 1;
  10. srand((unsigned)time(0)); // set an srand
  11. //std::cout << "\nRANDOMIZING\n";
  12. while (i <= max) { // while not done shuffling
  13. temp = rand() % len; // get a random integer between 0 and length
  14. //std::cout << "Random Position: " << temp << '\n';
  15. if (*(array_ptr + temp) == def) { // if nothing in the element yet
  16. // put i into element
  17. //std::cout << "Empty!\n";
  18. *(array_ptr + temp) = i;
  19. ++i; // incrament i
  20. }/* else {
  21. std::cout << "Occupied.\n";
  22. }*/
  23. //I need it to sleep apparently so I don't get repeated values.
  24. sleep(rand() % 20 + 90);
  25. }
  26. /*std::cout << "\nDONE!\n";
  27. for (i = 0; i < len; ++i) {
  28. std::cout << "Element " << i << ": " << *(array_ptr + i) << '\n';
  29. }*/
  30. }

I am using the wrong syntax for pointers. I totally forgot about the -> operator. Thanks!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pokerponcho is offline Offline
10 posts
since Nov 2005
Nov 23rd, 2005
0

Re: Automatic Shuffle Master

Reputation Points: 10
Solved Threads: 0
Newbie Poster
pokerponcho is offline Offline
10 posts
since Nov 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Reusing ifstream
Next Thread in C++ Forum Timeline: Timer Countdown





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC