| | |
Automatic Shuffle Master
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
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.
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?
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)
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.
C++ Syntax (Toggle Plain Text)
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?
pokerponcho
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)
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 */ };
pokerponcho
•
•
•
•
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.
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)
// 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!
pokerponcho
![]() |
Similar Threads
- My First Website (HTML and CSS)
- Disable Automatic Windows Update (Windows tips 'n' tweaks)
- want to install new Primary Master W2K (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: Reusing ifstream
- Next Thread: Timer Countdown
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference simple string strings studio system temperature template templates test text text-file tree unix url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets





