C++ || Snippet – Custom “Shuffle” Function For Arrays
The following is a custom “Shuffle” function which shuffles the contents of an array. This function rearranges each item within the array by swapping the value of a given item with that of some other randomly selected item.
The code demonstrated on this page is different from the STL random_shuffle function in that this implementation is meant to work mainly on standard arrays.
This implementation revolves around using a series of void pointers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
// ============================================================================ // Author: K Perkins // Date: July 17, 2013 // Taken From: http://programmingnotes.org/ // File: Shuffle.cpp // Description: Demonstrates the use of a custom "Shuffle" function // which rearranges/shuffles the contents of an array. // ============================================================================ #include <iostream> #include <ctime> #include <cstdlib> #include <cstring> #include <string> using namespace std; // function prototype void Shuffle(const void* arry, int arrySize, size_t sizeType); int main() { // declare variables char charArry[] = "This is a simple C-String"; char* twoD_CharArry[] = {"This","is","a","2-D","C-String","array"}; int intArry[] = {1,2,3,4,5,6,7,8,9,10}; string str = "This is a simple std::string"; string strArry[] = {"This","is","a","std::string","array"}; // char array Shuffle(charArry, strlen(charArry), sizeof(char)); cout<<charArry<<endl<<endl; // int array Shuffle(intArry, 10, sizeof(int)); for(int x = 0; x < 10; ++x) { cout<<intArry[x]<<" "; } cout<<endl<<endl; // 2-D char array Shuffle(twoD_CharArry, 6, sizeof(char*)); for(int x = 0; x < 6; ++x) { cout<<twoD_CharArry[x]<<" "; } cout<<endl<<endl; // std::string Shuffle(str.c_str(), str.length(), sizeof(char)); cout<<str<<endl<<endl; // std::string array Shuffle(strArry, 5, sizeof(string)); for(int x = 0; x < 5; ++x) { cout<<strArry[x]<<" "; } cout<<endl<<endl; return 0; }// end of main /** * FUNCTION: Shuffle * USE: Rearranges each item within the array by swapping the value of a given * item with that of some other randomly selected item * @param arry - the array being shuffled * @param arrySize - the total number of items in the array (the array length) * @param sizeType - the number of bytes required to represent the data-type * of the individual items in the array * @return - the shuffled array */ void Shuffle(const void* arry, int arrySize, size_t sizeType) { srand(time(NULL)); for(unsigned x = 0; x < (unsigned)arrySize; ++x) { int r = rand() % arrySize; // get the value of two items in the array void* a = ((char*)arry)+(x*sizeType); void* b = ((char*)arry)+(r*sizeType); // swap the value of the two items void* tmp = malloc(sizeType); memcpy(tmp, a, sizeType); memcpy(a, b, sizeType); memcpy(b, tmp, sizeType); free(tmp); } }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Once compiled, you should get this as your output
1 2 3 4 5 6 7 8 9 |
sesnisai g S-tmirp TlhiC 2 5 6 4 10 9 1 8 3 7 C-String array a 2-D is This i:gdsmsa: i ilnptti es Trshs is array std::string This a |
Leave a Reply