I`m trying to write a shuffle function to shuffle a sorted array with an even number of elements but it won`t print past the first 2 elements as shown below.
When i print the temp[] array it only prints the first 2 elements also. So i presume the loop is is stopping after 2 iterations.
Can anyone see my error?.
/*This is the output i get.
Array a[] is: 22 33 44 55 66 77 88 99
Array a[] is: 22 66 0 0 0 0 0 0 after shuffle
it should be
Array a[] is: 22 66 33 77 44 88 55 99 after shuffle*/
When i print the temp[] array it only prints the first 2 elements also. So i presume the loop is is stopping after 2 iterations.
Can anyone see my error?.
Code:
#include<iostream>
using namespace std;
void print(int a[],int n);
void shuffle(int a []);
int main()
{
int a[8]={22,33,44,55,66,77,88,99};
cout<<"\nArray a[] is: ";
print(a,8);
shuffle( a);
cout<<"\nArray a[] is: ";
print(a,8);
cout<<"after shuffle";
cout<<"\n\n\n";
system("pause");
return 0;
}
void shuffle(int a[])
{ int SIZE=8;
int temp[8]={0};
const int half=SIZE/2;
for(int i=0;i< SIZE;i++)// i have tried using 'half' ILO 'SIZE' also deleting i++
{ temp[2*i]=a[i];// and revising this line to temp[2*i++] to no avail
temp[2*i+1]=a[half+i];
for(i=0;i<SIZE;i++)
a[i]=temp[i];
}
}
void print(int a[],int n)
{
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
}
Array a[] is: 22 33 44 55 66 77 88 99
Array a[] is: 22 66 0 0 0 0 0 0 after shuffle
it should be
Array a[] is: 22 66 33 77 44 88 55 99 after shuffle*/
Comment