To shuffle an array a of n elements C++ -


i have problem shuffle array problem according seed random again same result , not 2-exchange , 2 array same! want 2 result array exchange random

the code 2-exchange

#include <stdio.h> #include <cstdlib> #include <ctime>  void swap(int *a, int *b) {     int temp = *a;     *a = *b;     *b = temp; }  void printarray(int arr[], int n) {     for(int = 0; < n; i++)         printf("%d ", arr[i]);     printf("\n"); }  // function generate random exchange int* randomized(int arr[], int n) {     // use different seed value don't same     // result each time run program      srand(time(null));     // start last element , swap 1 one. don't     // need run first element that's why > 0     for(int = n - 1; > 0; i--)     {         // pick random index 1 i-1         int j = rand() % (i - 1 + 1) + 1;         //int j = rand() % (i+1);          // swap arr[i] element @ random index         swap(&arr[i], &arr[j]);     }      return arr; }  // driver program test above function. int main() {     int *x1, *x2;     int arr[] = {6, 1, 2, 3, 4, 5};     int n = sizeof(arr) / sizeof(arr[0]);     x1 = randomized(arr, n);     x2 = randomized(x1, n);     printarray(x1, n);     printarray(x2, n);     getchar(); } 

obviously, randomized in both cases returning pointer same array that's being passed in (x1 = x2). "randomized" sounds name function should return randomized copy of data, not modify original data.

decide version want , based on that, fix either function or test program.


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -