Perfect Shuffle on the values of an array

Got Problems on the output

Page 1 of 1

4 Replies - 1914 Views - Last Post: 27 March 2010 - 05:24 AM Rate Topic: -----

#1 GREEN  Icon User is offline

  • New D.I.C Head

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

Perfect Shuffle on the values of an array

Post icon  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;
	
	read(a, 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  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • 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];
   
}


Was This Post Helpful? 1
  • +
  • -

#3 GREEN  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 :P
Was This Post Helpful? 0
  • +
  • -

#4 Guest_nabil*


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 ..
Was This Post Helpful? -2

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3714
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1