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