# Perfect Shuffle on the values of an array

Page 1 of 1

## 4 Replies - 1914 Views - Last Post: 27 March 2010 - 05:24 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=29131&amp;s=cd208857b2efc9f8cc7a2fc8f3e98e32&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 GREEN

Reputation: 0
• Posts: 4
• Joined: 27-April 07

# Perfect Shuffle on the values of an array

Posted 08 June 2007 - 01:39 AM

Hello everyone, I am a beginner of the C++

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 the array{11,22,33,44,55,66,77} with the array {11,44,22,55,33,66,77}.

In other words, if 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. 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.

```#include <iostream>
using namespace std;

const int MAXSIZE = 100;

void display( const int list[], int n );
void read( int list[], int &n );
void  shuffle(int a[], int n);

int main()
{
int a[MAXSIZE] = { 0 }, size;

cout << "The array has " << size << " elements: " << flush;
display(a, size);

if (size > 1) shuffle(a,size);
display(a, size);

system("pause");
return 0;
}//end main

void display( const int list[], int n )
{
for( int i = 0; i < n; i++ )
cout << list[i] << " ";
cout << endl;
}

void read( int list[], int &n )
{
cout << "Enter integers (-1 to finish): " << endl;
n = 0;
do
{
cout << "list[" << n << "]: ";
cin >> list[n];
}
while( list[n++] != -1 );
--n;  // don't count the last element
}

// Shuffle the n first values of the array a[]

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

int variable2 = 0;
for ( int counter2 = a[n/2]; counter2 < n/2; counter2++ )
{
temp[variable2] = a[counter2];
variable2 = variable2+2;
}

for( int i = 0; i < n; i++ )
a[i] = temp[i];
}

```

But it doesn't work properly, can anyone give me some clues to fix it?? Thanks a lot

Is This A Good Question/Topic? 0

## Replies To: Perfect Shuffle on the values of an array

### #2 rameshg87

Reputation: 3
• Posts: 34
• Joined: 06-June 07

## Re: Perfect Shuffle on the values of an array

Posted 08 June 2007 - 05:16 AM

Hey, there is a small mistake here. This should put things alright.

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

int variable2 = 1;
cout << "temp array\n";
//You made a small mistake in the initialization of counter2
for ( int counter2 = n/2; counter2 < n; counter2++ )
{

temp[variable2] = a[counter2];
variable2 = variable2+2;
}
//You need not touch last element if number of elements is odd
if(n%2==1)n=n-1;

for( int i = 0; i < n; i++ )
a[i] = temp[i];

}

```

### #3 GREEN

Reputation: 0
• Posts: 4
• Joined: 27-April 07

## Re: Perfect Shuffle on the values of an array

Posted 08 June 2007 - 05:24 AM

Oh, yes, that's right, i made a mistake, i was so careless, sorry

Thank you so much rameshg87, you are really helpful

Reputation:

## Re: Perfect Shuffle on the values of an array

Posted 27 March 2010 - 05:13 AM

guys can you post the full code of this ?? plsss !! thanks ..

### #5 CTphpnwb

• D.I.C Lover

Reputation: 3714
• Posts: 13,469
• Joined: 08-August 08

## Re: Perfect Shuffle on the values of an array

Posted 27 March 2010 - 05:24 AM

Do your own work. This site is for helping people who are trying to learn. It is not about giving code away.