Thread: A General Shuffle Array Function

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103

    A General Shuffle Array Function

    I wrote a general array shuffle function which has a signature:

    Code:
    void shuffle(void * ptr, unsigned int elem_size, size_t size)
    and I want to know if its proper to cast the void * ptr to a character pointer inside the shuffle function when I shuffle the elements?

    Here's a complete example of the shuffle function with two different arrays.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <stdbool.h>
    #include <string.h>
    
    #define ARR_SIZE 10
    
    void set_seed() {
      srand(time(NULL));
    }
    
    void shuffle(void * ptr, unsigned int elem_size, size_t size) {
      static bool set_seed_called = false;
      size_t counter = size - 1;
      char temp[elem_size];
    
      if (!set_seed_called) {
        set_seed();
        set_seed_called = true;
      }
    
      for (int i = 0; i < size; ++i) {
        int pos = rand() % (counter + 1);
        memcpy(temp, ((char*)ptr) + (elem_size * counter), elem_size);
        memcpy(((char*)ptr) + (elem_size * counter), ((char*)ptr) + (elem_size * pos), elem_size);
        memcpy(((char*)ptr) + (elem_size * pos), temp, elem_size);
        --counter;
      }
    }
    
    int main(int argc, char ** argv) {
      int i_arr[ARR_SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    
      for (int i = 0; i < ARR_SIZE; ++i) {
        fprintf(stdout, "%d, ", i_arr[i]);
      }
      fputs("\n", stdout);
      
      shuffle(i_arr, sizeof(*i_arr), ARR_SIZE);
      
      for (int i = 0; i < ARR_SIZE; ++i) {
        fprintf(stdout, "%d, ", i_arr[i]);
      }
      fputs("\n", stdout);
      
      char* s_arr[ARR_SIZE] = {
        "aaaaa",
        "bbbbb",
        "ccccc",
        "ddddd",
        "eeeee",
        "fffff",
        "ggggg",
        "hhhhh",
        "iiiii",
        "jjjjj"
      };
      
      for (int i = 0; i < ARR_SIZE; ++i) {
        fprintf(stdout, "%s, ", s_arr[i]);
      }
      fputs("\n", stdout);
      
      shuffle(s_arr, sizeof(*s_arr), ARR_SIZE);
      
      for (int i = 0; i < ARR_SIZE; ++i) {
        fprintf(stdout, "%s, ", s_arr[i]);
      }
      fputs("\n", stdout);
     
      return 0;
    }
    Note: The shuffle functions works but I'd like to know if the casting is proper.
    Last edited by G4143; 12-05-2022 at 06:37 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Shuffle rows in an array
    By Justin Time in forum C Programming
    Replies: 2
    Last Post: 04-23-2015, 07:39 PM
  2. What wrong with this shuffle function?
    By defjamvan in forum C Programming
    Replies: 6
    Last Post: 09-09-2013, 07:33 AM
  3. String Array Shuffle
    By omGeeK in forum C Programming
    Replies: 3
    Last Post: 02-14-2011, 07:48 PM
  4. shuffle 2-dimensional array
    By patkhor in forum C Programming
    Replies: 5
    Last Post: 11-26-2008, 03:51 PM
  5. Array Shuffle
    By ypramesh in forum C Programming
    Replies: 2
    Last Post: 04-08-2006, 11:01 AM

Tags for this Thread