Hi guys, hope you can help me here.
I have to write a program which does a complete shuffle of an array.
For example, it would replace the array {11,22,33,44,55,66,77,88} with the array {11,55,22,66,33,77,44,88} or 12 23 34 45 56 67 78 89 90 with 12 56 23 67 34 78 45 89 90. notice how the first and last elements remain fixed. given with an odd number of n elements, it should be clear that the first element of the 2nd half is the one at position n/2 (position 4 with value 56 in the example). Hence, in the case of odd number, the last number does not move...
At current I am bound to only passing in two variable; a[]- being my array and int n being my lenght of the array. Here's a copy of my code so far but withobvious compilation errors. Cheers!

``````void  shuffle(int a[], int n)
{
int temp;
int variable= 0;
for (int counter= 0; counter < a[n/2]; counter++)
{
temp[variable]=a[counter] ;
variable=variable+2;
}
int variable2= 0;
for (int counter2= a[n/2]; counter2 < n; counter2++)
{
temp[variable2]= a[counter2];
variable2= variable2+2;
}
for(int i=0; i<n; i++)
a[i]= temp[i];
}``````

## All 2 Replies >For example, it would replace the array {11,22,33,44,55,66,77,88} with the array {11,55,22,66,33,77,44,88} or 12 23 34 45 56 67 78 89 90 with 12 56 23 67 34 78 45 89 90. notice how the first and last elements remain fixed. given with an odd number of n elements, it should be clear that the first element of the 2nd half is the one at position n/2 (position 4 with value 56 in the example). Hence, in the case of odd number, the last number does not move...

Huh? Why such a silly criteria?

hi,

try replacing counter<a[n/2] with counter<n/2

and also int counter2=a[n/2] with counter2=n/2

hope this helps!

Hi guys, hope you can help me here.
I have to write a program which does a complete shuffle of an array.
For example, it would replace the array {11,22,33,44,55,66,77,88} with the array {11,55,22,66,33,77,44,88} or 12 23 34 45 56 67 78 89 90 with 12 56 23 67 34 78 45 89 90. notice how the first and last elements remain fixed. given with an odd number of n elements, it should be clear that the first element of the 2nd half is the one at position n/2 (position 4 with value 56 in the example). Hence, in the case of odd number, the last number does not move...
At current I am bound to only passing in two variable; a[]- being my array and int n being my lenght of the array. Here's a copy of my code so far but withobvious compilation errors. Cheers!

``````void  shuffle(int a[], int n)
{
int temp;
int variable= 0;
for (int counter= 0; counter < a[n/2]; counter++)
{
temp[variable]=a[counter] ;
variable=variable+2;
}
int variable2= 0;
for (int counter2= a[n/2]; counter2 < n; counter2++)
{
temp[variable2]= a[counter2];
variable2= variable2+2;
}
for(int i=0; i<n; i++)
a[i]= temp[i];
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.