Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,117 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,779 people online right now. Registration is fast and FREE... Join Now!




[HELP!PLEASE!]Perfect Shuffle on the values of an array

 
Reply to this topicStart new topic

[HELP!PLEASE!]Perfect Shuffle on the values of an array, Got Problems on the output

GREEN
8 Jun, 2007 - 12:39 AM
Post #1

New D.I.C Head
*

Joined: 27 Apr, 2007
Posts: 4


My Contributions
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.

CODE


#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
User is offlineProfile CardPM
+Quote Post

rameshg87
RE: [HELP!PLEASE!]Perfect Shuffle On The Values Of An Array
8 Jun, 2007 - 04:16 AM
Post #2

New D.I.C Head
Group Icon

Joined: 6 Jun, 2007
Posts: 34


Dream Kudos: 50
My Contributions
Hey, there is a small mistake here. This should put things alright.

CODE

.
.
.
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];
  
}

User is offlineProfile CardPM
+Quote Post

GREEN
RE: [HELP!PLEASE!]Perfect Shuffle On The Values Of An Array
8 Jun, 2007 - 04:24 AM
Post #3

New D.I.C Head
*

Joined: 27 Apr, 2007
Posts: 4


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

Thank you so much rameshg87, you are really helpful tongue.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:56PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month